UNPKG

@syncfusion/ej2-pdfviewer

Version:
114 lines (113 loc) 6.07 kB
/** * `DirectPrint` module is used to handle the direct print functionality of PDF Viewer. * @hidden */ var DefaultPrint = /** @class */ (function () { /** * @param {PdfViewer} pdfViewer - The PDF Viewer instance. * @param {PdfViewerBase} pdfViewerBase - The PDF Viewer Base instance. * @private */ function DefaultPrint(pdfViewer, pdfViewerBase) { this.pdfViewer = pdfViewer; this.pdfViewerBase = pdfViewerBase; } /** * Creates a request for direct print functionality. * @returns {void} * @private */ DefaultPrint.prototype.createRequestForDirectPrint = function () { this.pdfViewer.printModule.canPrint = true; var jsonObject = this.pdfViewerBase.constructJsonDownload(true); if (this.pdfViewerBase.jsonDocumentId) { jsonObject.documentId = this.pdfViewerBase.jsonDocumentId; } jsonObject.enablePrintRotation = this.pdfViewer.enablePrintRotation; var data = this.pdfViewer.pdfRendererModule.getDocumentAsUint8Array(jsonObject); if (this.pdfViewer.downloadFileName && (this.pdfViewer.downloadFileName !== this.pdfViewerBase.downloadFileName)) { this.pdfViewerBase.downloadFileName = this.pdfViewer.downloadFileName; } this.pdfViewer.firePrintEnd(this.pdfViewer.downloadFileName); if (this.pdfViewer.printMode === 'Default') { this.printPdfDirectlySameWindow(data); } else { this.printPdfDirectlyNewWindow(data, this.pdfViewerBase.downloadFileName); } this.pdfViewerBase.updateDocumentAnnotationCollections(); this.pdfViewer.printModule.canPrint = false; }; DefaultPrint.prototype.createPdfBlobAndUrl = function (fileByteArray) { var pdfBlob = new Blob([fileByteArray], { type: 'application/pdf' }); var blobUrl = URL.createObjectURL(pdfBlob); return { blobUrl: blobUrl }; }; DefaultPrint.prototype.printPdfDirectlySameWindow = function (fileByteArray) { var _this = this; var blobUrl = this.createPdfBlobAndUrl(fileByteArray).blobUrl; var iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = blobUrl; document.body.appendChild(iframe); var hasPrinted = false; var tryPrint = function () { if (hasPrinted) { return; } hasPrinted = true; try { var contentWindow = iframe.contentWindow; if (!contentWindow) { throw new Error('Unable to access iframe content'); } _this.pdfViewerBase.showPrintLoadingIndicator(false); contentWindow.focus(); contentWindow.print(); } catch (error) { console.error('Error while trying to print:', error); } }; iframe.onload = function () { tryPrint(); }; }; DefaultPrint.prototype.printPdfDirectlyNewWindow = function (fileByteArray, fileName) { var _this = this; var blobUrl = this.createPdfBlobAndUrl(fileByteArray).blobUrl; var screenWidth = window.screen.availWidth; var screenHeight = window.screen.availHeight; var windowFeatures = "width=" + screenWidth + ",height=" + screenHeight + ",top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no"; var printWindow = window.open('', fileName, windowFeatures); if (!printWindow) { alert('Please allow pop-ups for this site.'); return; } printWindow.addEventListener('beforeunload', function () { _this.pdfViewerBase.showPrintLoadingIndicator(false); }); var printDocument = printWindow.document; var html = printDocument.createElement('html'); var head = printDocument.createElement('head'); var body = printDocument.createElement('body'); var title = printDocument.createElement('title'); title.textContent = fileName; head.appendChild(title); var style = printDocument.createElement('style'); style.textContent = "\n html, body {\n margin: 0;\n padding: 0;\n height: 100%;\n overflow: hidden;\n }\n iframe {\n width: 100%;\n height: 100%;\n border: none;\n display: block;\n }"; head.appendChild(style); var iframe = printDocument.createElement('iframe'); iframe.id = 'pdfFrame'; iframe.src = blobUrl + "#toolbar=0&navpanes=0&scrollbar=0"; body.appendChild(iframe); var script = printDocument.createElement('script'); script.textContent = "\n const iframe = document.getElementById('pdfFrame');\n iframe.onload = function () {\n const contentWindow = iframe.contentWindow;\n if (!contentWindow) {\n console.error(\"Unable to access iframe content window.\");\n return;\n }\n contentWindow.focus();\n contentWindow.print();\n let focusLost = false;\n contentWindow.onafterprint = function () {\n window.close();\n };\n const checkFocus = setInterval(function () {\n if (document.hasFocus() && focusLost) {\n clearInterval(checkFocus);\n window.close();\n } else if (!document.hasFocus()) {\n focusLost = true;\n }\n }, 500);\n };"; body.appendChild(script); html.appendChild(head); html.appendChild(body); printDocument.documentElement.replaceWith(html); }; return DefaultPrint; }()); export { DefaultPrint };