Skip to content

Commit

Permalink
rename npm print.js package to print-js
Browse files Browse the repository at this point in the history
  • Loading branch information
crabbly committed Oct 5, 2017
1 parent 1762af9 commit af179f8
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 47 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Print.js

[![Build Status](https://travis-ci.org/crabbly/Print.js.svg?branch=master)](https://travis-ci.org/crabbly/Print.js) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE) [![Standard - JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/)
[![Build Status](https://travis-ci.org/crabbly/Print.js.svg?branch=master)](https://travis-ci.org/crabbly/Print.js) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE) [![Standard - JavaScript Style Guide](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) [![npm](https://img.shields.io/npm/dt/print.js.svg)]()

A tiny javascript library to help printing from the web.

Expand All @@ -14,7 +14,7 @@ You can download the latest version of [Print.js on GitHub](https://github.com/c
To install via npm:

```bash
npm install print.js --save
npm install print-js --save
```

## Documentation
Expand Down
2 changes: 1 addition & 1 deletion dist/print.min.js

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "print.js",
"name": "print-js",
"homepage": "http://printjs.crabbly.com",
"description": "A tiny javascript library to help printing from the web.",
"version": "1.0.18",
"version": "1.0.19",
"main": "src/index.js",
"repository": {
"type": "git",
Expand All @@ -22,8 +22,5 @@
"watch-poll": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --watch-poll --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"spm": {
"main": "Print.js"
}
}
1 change: 1 addition & 0 deletions src/css/print.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/js/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Browser = {
},
// Internet Explorer 6-11
isIE: () => {
return !!document.documentMode
return navigator.userAgent.indexOf('MSIE') !== -1 || !!document.documentMode
},
// Edge 20+
isEdge: () => {
Expand All @@ -23,3 +23,4 @@ const Browser = {
}

export default Browser

21 changes: 6 additions & 15 deletions src/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,11 @@ export default {
// Create a new iframe or embed element (IE prints blank pdf's if we use iframe)
let printFrame

if (Browser.isIE() && params.type === 'pdf') {
// Create embed element
printFrame = document.createElement('embed')
printFrame.setAttribute('type', 'application/pdf')

// Hide embed
printFrame.setAttribute('style', 'width:0px;height:0px;')
} else {
// Create iframe element
printFrame = document.createElement('iframe')

// Hide iframe
printFrame.setAttribute('style', 'display:none;')
}
// Create iframe element
printFrame = document.createElement('iframe')

// Hide iframe
printFrame.setAttribute('style', 'display:none;')

// Set element id
printFrame.setAttribute('id', params.frameId)
Expand All @@ -107,7 +98,7 @@ export default {
switch (params.type) {
case 'pdf':
// Check browser support for pdf and if not supported we will just open the pdf file instead
if (Browser.isFirefox() || Browser.isIE() || Browser.isEdge()) {
if (Browser.isFirefox() || Browser.isEdge() || Browser.isIE()) {
console.log('PrintJS currently doesn\'t support PDF printing in Firefox, Internet Explorer and Edge.')
let win = window.open(params.printable, '_blank')
win.focus()
Expand Down
38 changes: 15 additions & 23 deletions src/js/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ const Print = {
// Get iframe element
let iframeElement = document.getElementById(params.frameId)

// If printing pdf in IE
if (Browser.isIE() && params.type === 'pdf') {
finishPrintPdfIe(iframeElement)
// Wait for iframe to load all content
if (params.type === 'pdf' && (Browser.isIE() || Browser.isEdge())) {
iframeElement.setAttribute('onload', finishPrint(iframeElement, params))
} else {
// Wait for iframe to load all content
printFrame.onload = () => {
if (params.type === 'pdf') {
finishPrint(iframeElement, params)
Expand All @@ -38,40 +37,33 @@ const Print = {
}

function finishPrint (iframeElement, params) {
// Print iframe document
iframeElement.focus()

// If IE or Edge, try catch with execCommand
if (Browser.isIE() || Browser.isEdge()) {
// If Edge or IE, try catch with execCommand
if (Browser.isEdge() || (Browser.isIE())) {
try {
iframeElement.contentWindow.document.execCommand('print', false, null)
} catch (e) {
iframeElement.contentWindow.print()
}
} else {
iframeElement.contentWindow.print()
}

// If we are showing a feedback message to user, remove it
if (params.showModal) {
Modal.close()
// Other browsers
if (!Browser.isIE() && !Browser.isEdge()) {
iframeElement.contentWindow.print()
}
}

function finishPrintPdfIe (iframeElement) {
// Wait until pdf is ready to print
if (typeof iframeElement.print === 'undefined') {
setTimeout(() => {
finishPrintPdfIe()
}, 1000)
} else {
Print.send()

// Remove embed (just because it isn't 100% hidden when using h/w = 0)
// 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()
}
}

function loadImageAndFinishPrint (img, iframeElement, params) {
Expand Down

0 comments on commit af179f8

Please sign in to comment.