New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Directly print PDF #335
Comments
You can use the var doc = new jsPDF("landscape", "mm", "a4");
doc.setFontSize(22);
doc.text(20, 20, 'Example');
doc.autoPrint(); // <<--------------------- !!
doc.output('dataurlnewwindow'); |
You can actually do: This works MUCH better than doing dataurlnewwindow. |
Using Tested in Chrome. |
bloburl works in Chrome in Linux |
Thanks @bschuon. |
@bschuon , url is valid, but new window is opened and instantly closed. |
@bschuon , nevermind, problem was with adblock |
This is an excellent solution but being blocked automatically by Chrome even without an adblocker installed :( |
Many thanks @bschuon |
This works for me! |
I've found a function that opens the printing dialog in the same page, no new tab. Here it is:function closePrint () {
document.body.removeChild(this.__container__);
}
function setPrint () {
this.contentWindow.__container__ = this;
this.contentWindow.onbeforeunload = closePrint;
this.contentWindow.onafterprint = closePrint;
this.contentWindow.focus(); // Required for IE
this.contentWindow.print();
}
function printPage (sURL) {
var oHiddFrame = document.createElement("iframe");
oHiddFrame.onload = setPrint;
oHiddFrame.style.position = "fixed";
oHiddFrame.style.right = "0";
oHiddFrame.style.bottom = "0";
oHiddFrame.style.width = "0";
oHiddFrame.style.height = "0";
oHiddFrame.style.border = "0";
oHiddFrame.src = sURL;
document.body.appendChild(oHiddFrame);
} UsageprintPage(doc.output('bloburl')) But it has problems
Solution to 1This makes it work in Chrome and allows to use another printing method as a fallback for other browsers. const printPage = async sURL => {
var oHiddFrame = document.createElement("iframe");
const printPromise = new Promise((resolve, reject) => {
oHiddFrame.onload = function () {
try {
oHiddFrame.contentWindow.focus(); // Required for IE
oHiddFrame.contentWindow.print();
resolve();
} catch (error) {
reject(error)
}
}
})
oHiddFrame.style.position = "fixed";
oHiddFrame.style.right = "0";
oHiddFrame.style.bottom = "0";
oHiddFrame.style.width = "0";
oHiddFrame.style.height = "0";
oHiddFrame.style.border = "0";
oHiddFrame.src = sURL;
document.body.appendChild(oHiddFrame);
await printPromise;
} UsageprintPage(doc.output('bloburl'))
.catch ( error => {
// Fallback printing method
doc.autoPrint();
doc.output('dataurlnewwindow');
}) The reason for turning it to asynchronous was to be able to catch errors in the iframe.onload event. This code as it is, might work for non SPA pages, where there's no danger for iframes to keep stacking up. Solution to 2I couldn't solve this 😞. I had trouble cleaning that iframe, I couldn't make it work on the onafterprint event, so the ugly ugly workaround I have found so far is to put a setTimeout( ()=>document.body.removeChild(oHiddFrame), 60000) It gives 1 minute of time to the user to decide what to do in the printing dialogue, after that, the iframe is removed, and the printing dialogue closes. |
I managed to print in the same page with Firefox too. There is a bug in Firefox, when the iframe is too small it doesn't open the print dialog https://bugzilla.mozilla.org/show_bug.cgi?id=1618955 So using the doc.autoPrint();
var oHiddFrame = document.createElement("iframe");
oHiddFrame.style.position = "fixed";
oHiddFrame.style.visibility = "hidden";
oHiddFrame.src = doc.output('bloburl');
document.body.appendChild(oHiddFrame); |
Modified some of your code to get a better compatability~ doc.autoPrint();
const hiddFrame = document.createElement('iframe');
hiddFrame.style.position = 'fixed';
// "visibility: hidden" would trigger safety rules in some browsers like safari,
// in which the iframe display in a pretty small size instead of hidden.
// here is some little hack ~
hiddFrame.style.width = '1px';
hiddFrame.style.height = '1px';
hiddFrame.style.opacity = '0.01';
const isSafari = /^((?!chrome|android).)*safari/i.test(window.navigator.userAgent);
if (isSafari) {
// fallback in safari
hiddFrame.onload = () => {
try {
hiddFrame.contentWindow.document.execCommand('print', false, null);
} catch (e) {
hiddFrame.contentWindow.print();
}
};
}
hiddFrame.src = doc.output('bloburl');
document.body.appendChild(hiddFrame); Tested in chrome and firefox even safari~ |
This is the recommended way:
|
This works for me, Chrome on MacOS. Thanks! |
This worked for me. Chrome on windows 10. How can we send a pdf to default printer installed on windows 10 without a print dialogue? |
How can i directly print the generated PDF?
I tried it by generating a PDF and put it into a
iframe
:Next i tried to print from this iframe:
But somehow in Chrome i get this error:
What do i wrong? Or what could i do instead? Thanks!
The text was updated successfully, but these errors were encountered: