- Improve print quality
- Print using window object
- Page setup
- See Also
Contact Support
Print in EJ2 JavaScript Document editor control
26 Nov 202312 minutes to read
To print the document, use the print
method from Document Editor instance.
Refer to the following example for showing a document and print it.
var documenteditor = new ej.documenteditor.DocumentEditor({
enablePrint: true
});
documenteditor.appendTo('#DocumentEditor');
var sfdt ={
"sections": [
{
"blocks": [
{
"inlines": [
{
"characterFormat": {
"bold": true,
"italic": true
},
"text": "Hello World"
}
]
}
],
"headersFooters": {
}
}
]
};
documenteditor.open(JSON.stringify(sfdt));
var printButton = new ej.buttons.Button();
printButton.appendTo('#print');
document.getElementById('print').addEventListener('click',function (){
documenteditor.print();
});
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-documenteditor/styles/fabric.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/28.2.3/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<button id="print">Print</button>
<div id="DocumentEditor">
</div>
<div id="DocumentEditor2"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Refer to the following example for creating a document and print it.
var documenteditor = new ej.documenteditor.DocumentEditor({
isReadOnly: false,
enablePrint: true,
enableEditor: true,
enableSelection: true,
enableEditorHistory: true
});
documenteditor.appendTo('#DocumentEditor');
var printButton = new ej.buttons.Button();
printButton.appendTo('#print');
document.getElementById('print').addEventListener('click',function (){
documenteditor.print();
});
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-documenteditor/styles/fabric.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/28.2.3/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<button id="print">Print</button>
<div id="DocumentEditor">
</div>
<div id="DocumentEditor2"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>
Improve print quality
Document editor provides an option to improve the print quality using printDevicePixelRatio
in Document editor settings. Document editor using canvas approach to render content. Then, canvas are converted to image and it process for print. Using printDevicePixelRatio API, you can increase the image quality based on your requirement.
The following example code illustrates how to improve the print quality in Document editor container.
let container: DocumentEditorContainer = new DocumentEditorContainer({ enableToolbar: true, height: '590px',
documentEditorSettings: {
printDevicePixelRatio: 2
}
});
DocumentEditorContainer.Inject(Toolbar);
container.serviceUrl = 'https://services.syncfusion.com/js/production/api/documenteditor/';
container.appendTo('#container');
Note: By default, printDevicePixelRatio value is 1
Print using window object
You can print the document in Document Editor by passing the window instance. This is useful to implement print in third party frameworks such as electron, where the window instance will not be available. Refer to the following example.
import { DocumentEditor, Print } from '@syncfusion/ej2-documenteditor';
DocumentEditor.Inject(Print);
let documenteditor: DocumentEditor = new DocumentEditor({
enablePrint: true, height: '370px'
});
documenteditor.appendTo('#DocumentEditor');
documenteditor.print(window);
Page setup
Some of the print options cannot be configured using JavaScript. Refer to the following links to learn more about the browser page setup:
However, you can customize margins, paper, and layout options by modifying the section format properties using page setup dialog
import { DocumentEditor, Print, PageSetupDialog, Editor, Selection, EditorHistory } from '@syncfusion/ej2-documenteditor';
DocumentEditor.Inject(Print, PageSetupDialog, Editor, Selection, EditorHistory);
let documenteditor: DocumentEditor = new DocumentEditor({
isReadOnly: false,
enablePrint: true,
enablePageSetupDialog: true,
enableEditor: true,
enableSelection: true,
enableEditorHistory: true,
height: '370px'
});
documenteditor.appendTo('#DocumentEditor');
documenteditor.showPageSetupDialog();
By customizing margins, papers, and layouts, the layout of the document will be changed in Document Editor. To modify these options during print operation, serialize the document as SFDT using the serialize
method in Document Editor instance and open the SFDT data in another instance of Document Editor in separate window.
The following example shows how to customize layout options only for printing.
var documenteditor1 = new ej.documenteditor.DocumentEditor({
isReadOnly: false,
enablePrint: true,
enableEditor: true,
enableSelection: true,
enableEditorHistory: true,
enableSfdtExport: true
});
documenteditor1.appendTo('#DocumentEditor');
var documenteditor2 = new ej.documenteditor.DocumentEditor({
isReadOnly: false,
enablePrint: true,
enableEditor: true,
enableSelection: true,
enableEditorHistory: true,
enableSfdtExport: true
});
documenteditor2.appendTo('#DocumentEditor2');
var printButton = new ej.buttons.Button();
printButton.appendTo('#print');
document.getElementById('print').addEventListener('click',function (){
var sfdtData = documenteditor1.serialize();
documenteditor2.open(sfdtData);
//Set A5 paper size
documenteditor2.selection.sectionFormat.pageWidth = 419.55;
documenteditor2.selection.sectionFormat.pageHeight = 595.30;
documenteditor2.print();
});
<!DOCTYPE html><html lang="en"><head>
<title>EJ2 Animation</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Typescript UI Controls">
<meta name="author" content="Syncfusion">
<link href="index.css" rel="stylesheet">
<link href="https://cdn.syncfusion.com/ej2/28.2.3/ej2-documenteditor/styles/fabric.css" rel="stylesheet">
<script src="https://cdn.syncfusion.com/ej2/28.2.3/dist/ej2.min.js" type="text/javascript"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id="container">
<button id="print">Print</button>
<div id="DocumentEditor">
</div>
<div id="DocumentEditor2"></div>
</div>
<script>
var ele = document.getElementById('container');
if(ele) {
ele.style.visibility = "visible";
}
</script>
<script src="index.js" type="text/javascript"></script>
</body></html>