Skip to content

Commit

Permalink
prevent page layout change after print
Browse files Browse the repository at this point in the history
  • Loading branch information
crabbly committed Apr 6, 2018
1 parent 7e61a2c commit f12d60a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 52 deletions.
1 change: 0 additions & 1 deletion src/js/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ const Browser = {
}

export default Browser

32 changes: 12 additions & 20 deletions src/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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.')
}
}
}
10 changes: 3 additions & 7 deletions src/js/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 += '<h1 style="' + params.headerStyle + '">' + params.header + '</h1>'
}
// Check if we are adding a header
if (params.header) htmlData += '<h1 style="' + params.headerStyle + '">' + params.header + '</h1>'

// Build html data
htmlData += jsonToHTML(params)
Expand Down
3 changes: 1 addition & 2 deletions src/js/pdf.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
29 changes: 8 additions & 21 deletions src/js/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion test.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<script src='./dist/print.min.js'></script>
<script type='text/javascript'>
function printPdf() {
printJS('test.pdf');
printJS('/test.pdf');
}

function printHtml() {
Expand Down

0 comments on commit f12d60a

Please sign in to comment.