Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare namespace printJS {
fallbackPrintable?: string;
type?: PrintTypes;
documentTitle?: string;
documentTitleHoldMs?: number;
header?: any;
headerStyle?: string;
footer?: any;
Expand Down
1 change: 1 addition & 0 deletions src/js/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default {
frameRemoveDelay: null,
printableElement: null,
documentTitle: 'Document',
documentTitleHoldMs: 3000,
targetStyle: ['clear', 'display', 'width', 'min-width', 'height', 'min-height', 'max-height'],
targetStyles: ['border', 'box', 'break', 'text-decoration'],
ignoreElements: [],
Expand Down
45 changes: 40 additions & 5 deletions src/js/print.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
import Browser from './browser'
import { cleanUp } from './functions'

function guardDocumentTitle (title, holdMs, onDone) {
if (!title) { onDone(); return }

var originalTitle = document.title
document.title = title

var titleEl = document.querySelector('title')
var observer = null

if (titleEl && typeof MutationObserver !== 'undefined') {
observer = new MutationObserver(function () {
if (document.title !== title) document.title = title
})
observer.observe(titleEl, { characterData: true, childList: true, subtree: true })
}

setTimeout(function () {
if (observer) observer.disconnect()
document.title = originalTitle
onDone()
}, holdMs)
}

const Print = {
send: (params, printFrame) => {
// Append iframe element to document body
Expand Down Expand Up @@ -51,6 +74,16 @@ const Print = {
}

function performPrint (iframeElement, params) {
var isPdfWithTitle = params.type === 'pdf' &&
params.documentTitle &&
params.documentTitle !== 'Document'

if (isPdfWithTitle) {
guardDocumentTitle(params.documentTitle, params.documentTitleHoldMs || 3000, function () {
cleanUp(params)
})
}

try {
iframeElement.focus()

Expand All @@ -59,15 +92,15 @@ function performPrint (iframeElement, params) {
try {
iframeElement.contentWindow.document.execCommand('print', false, null)
} catch (e) {
setTimeout(function(){
setTimeout(function () {
iframeElement.contentWindow.print()
},1000)
}, 1000)
}
} else {
// Other browsers
setTimeout(function(){
setTimeout(function () {
iframeElement.contentWindow.print()
},1000)
}, 1000)
}
} catch (error) {
params.onError(error)
Expand All @@ -78,7 +111,9 @@ function performPrint (iframeElement, params) {
iframeElement.style.left = '-1px'
}

cleanUp(params)
if (!isPdfWithTitle) {
cleanUp(params)
}
}
}

Expand Down
32 changes: 31 additions & 1 deletion test/manual/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@
})
}

function printPdfWithDocumentTitle() {
printJS({
printable: '/test/manual/test.pdf',
type: 'pdf',
documentTitle: 'My Custom Report'
})
}

function printPdfWithDocumentTitleAndSimulatedReset() {
// Simulate a framework (React, Vue, etc.) aggressively resetting document.title
var interval = setInterval(function () {
document.title = 'RESET BY FRAMEWORK'
}, 50)

// Stop the simulation after 5s so it doesn't run forever
setTimeout(function () { clearInterval(interval) }, 5000)

printJS({
printable: '/test/manual/test.pdf',
type: 'pdf',
documentTitle: 'Guarded Title Test'
})
}

function printPdfWithModal() {
printJS({
printable: '/test/manual/test.pdf',
Expand Down Expand Up @@ -288,7 +312,13 @@ <h1>Print.js Test Page</h1>
<button onClick='printPdfKioskPrint();'>
Print PDF In --kiosk-printing Chrome frameRemoveDelay
</button>

<button onClick='printPdfWithDocumentTitle();'>
Print PDF with documentTitle
</button>
<button onClick='printPdfWithDocumentTitleAndSimulatedReset();'>
Print PDF with documentTitle (simulated framework reset)
</button>

</p>
<div>
<h2>Form Elements</h2>
Expand Down