all files / pdfviewer/print/ default-print.js

12.35% Statements 10/81
0% Branches 0/14
20% Functions 2/10
12.35% Lines 10/81
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104                                                                                                                                                                                            
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var DefaultPrint = (function () {
        function DefaultPrint(pdfViewer, pdfViewerBase) {
            this.pdfViewer = pdfViewer;
            this.pdfViewerBase = pdfViewerBase;
        }
        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;
    }());
    exports.DefaultPrint = DefaultPrint;
});