From f12d60acdde8a20e01e98e189445359232e4a96f Mon Sep 17 00:00:00 2001 From: Rodrigo Vieira Date: Fri, 6 Apr 2018 11:54:06 -0400 Subject: [PATCH] prevent page layout change after print --- src/js/browser.js | 1 - src/js/init.js | 32 ++++++++++++-------------------- src/js/json.js | 10 +++------- src/js/pdf.js | 3 +-- src/js/print.js | 29 ++++++++--------------------- test.html | 2 +- 6 files changed, 25 insertions(+), 52 deletions(-) diff --git a/src/js/browser.js b/src/js/browser.js index e420f77..3f9bbb0 100644 --- a/src/js/browser.js +++ b/src/js/browser.js @@ -23,4 +23,3 @@ const Browser = { } export default Browser - diff --git a/src/js/init.js b/src/js/init.js index 9ff188c..3614c67 100644 --- a/src/js/init.js +++ b/src/js/init.js @@ -40,17 +40,15 @@ export default { // Check if a printable document or object was supplied let args = arguments[0] - if (args === undefined) { - throw new Error('printJS expects at least 1 attribute.') - } + if (args === undefined) throw new Error('printJS expects at least 1 attribute.') + // Process parameters switch (typeof args) { case 'string': params.printable = encodeURI(args) params.fallbackPrintable = params.printable params.type = arguments[1] || params.type break - case 'object': params.printable = args.printable params.fallbackPrintable = typeof args.fallbackPrintable !== 'undefined' ? args.fallbackPrintable : params.printable @@ -79,28 +77,24 @@ export default { throw new Error('Unexpected argument type! Expected "string" or "object", got ' + typeof args) } - if (!params.printable) { - throw new Error('Missing printable information.') - } + // Validate printable + if (!params.printable) throw new Error('Missing printable information.') + // Validate type if (!params.type || typeof params.type !== 'string' || printTypes.indexOf(params.type.toLowerCase()) === -1) { throw new Error('Invalid print type. Available types are: pdf, html, image and json.') } // Check if we are showing a feedback message to the user (useful for large files) - if (params.showModal) { - Modal.show(params) - } - if (params.onLoadingStart) { - params.onLoadingStart() - } + if (params.showModal) Modal.show(params) + + // Check for a print start hook function + if (params.onLoadingStart) params.onLoadingStart() - // To prevent duplication and issues, remove printFrame from the DOM, if it exists + // To prevent duplication and issues, remove any used printFrame from the DOM let usedFrame = document.getElementById(params.frameId) - if (usedFrame) { - usedFrame.parentNode.removeChild(usedFrame) - } + if (usedFrame) usedFrame.parentNode.removeChild(usedFrame) // Create a new iframe or embed element (IE prints blank pdf's if we use iframe) let printFrame @@ -109,7 +103,7 @@ export default { printFrame = document.createElement('iframe') // Hide iframe - printFrame.setAttribute('style', 'visibility: hidden; height: 0; width: 0;') + printFrame.setAttribute('style', 'visibility: hidden; height: 0; width: 0; position: absolute;') // Set element id printFrame.setAttribute('id', params.frameId) @@ -143,8 +137,6 @@ export default { case 'json': Json.print(params, printFrame) break - default: - throw new Error('Invalid print type. Available types are: pdf, html, image and json.') } } } diff --git a/src/js/json.js b/src/js/json.js index 6e85257..854c940 100644 --- a/src/js/json.js +++ b/src/js/json.js @@ -12,17 +12,13 @@ export default { } // Check if properties were provided - if (!params.properties || typeof params.properties !== 'object') { - throw new Error('Invalid properties array for your JSON data.') - } + if (!params.properties || typeof params.properties !== 'object') throw new Error('Invalid properties array for your JSON data.') // Variable to hold the html string let htmlData = '' - // Check print has header - if (params.header) { - htmlData += '

' + params.header + '

' - } + // Check if we are adding a header + if (params.header) htmlData += '

' + params.header + '

' // Build html data htmlData += jsonToHTML(params) diff --git a/src/js/pdf.js b/src/js/pdf.js index 9773985..a21ec4e 100644 --- a/src/js/pdf.js +++ b/src/js/pdf.js @@ -1,10 +1,9 @@ -import Browser from './browser' import Print from './print' export default { print: (params, printFrame) => { // If showing feedback to user, pre load pdf files (hacky) - if (params.showModal || params.onLoadingStart || Browser.isIE()) { + if (params.showModal || params.onLoadingStart) { let req = new window.XMLHttpRequest() req.addEventListener('load', send(params, printFrame)) req.open('GET', params.printable.indexOf('http') !== -1 ? params.printable : window.location.origin + '/' + params.printable, true) diff --git a/src/js/print.js b/src/js/print.js index 8a591f7..a420844 100644 --- a/src/js/print.js +++ b/src/js/print.js @@ -42,41 +42,28 @@ function finishPrint (iframeElement, params) { iframeElement.focus() // If Edge or IE, try catch with execCommand - if (Browser.isEdge() || (Browser.isIE())) { + if (Browser.isEdge() || Browser.isIE()) { try { iframeElement.contentWindow.document.execCommand('print', false, null) } catch (e) { iframeElement.contentWindow.print() } - } - - // Other browsers - if (!Browser.isIE() && !Browser.isEdge()) { + } else { + // Other browsers iframeElement.contentWindow.print() } - // Remove embed on IE (just because it isn't 100% hidden when using h/w = 0) - if (Browser.isIE() && params.type === 'pdf') { - setTimeout(() => { - iframeElement.parentNode.removeChild(iframeElement) - }, 2000) - } - // If we are showing a feedback message to user, remove it - if (params.showModal) { - Modal.close() - } - if (params.onLoadingEnd) { - params.onLoadingEnd() - } + if (params.showModal) Modal.close() + + // Check for a finished loading hook function + if (params.onLoadingEnd) params.onLoadingEnd() } function loadIframeImages (printDocument, params) { let promises = [] - params.printable.forEach((image, index) => { - promises.push(loadIframeImage(printDocument, index)) - }) + params.printable.forEach((image, index) => promises.push(loadIframeImage(printDocument, index))) return Promise.all(promises) } diff --git a/test.html b/test.html index df98168..490d604 100644 --- a/test.html +++ b/test.html @@ -2,7 +2,7 @@