Skip to content

Instantly share code, notes, and snippets.

@bondgeek
Last active July 7, 2021 13:04
Show Gist options
  • Select an option

  • Save bondgeek/638a973cab8b0c27dc089f9408d92ff0 to your computer and use it in GitHub Desktop.

Select an option

Save bondgeek/638a973cab8b0c27dc089f9408d92ff0 to your computer and use it in GitHub Desktop.
Alternate ways to print from blob or canvas
/*
starting reference: https://developer.mozilla.org/en-US/docs/Web/Guide/Printing
*/
function closePrint () {
document.body.removeChild(this.__container__);
}
function setPrint () {
setTimeout ( () => {
this.contentWindow.__container__ = this;
this.contentWindow.onbeforeunload = closePrint;
this.contentWindow.onafterprint = closePrint;
this.contentWindow.focus();
this.contentWindow.print();
this.contentWindow.close();
}, 100);
}
// for, e.g., pdf.js rendered pdf where each page is a different canvas
// const canvasList = document.querySelectorAll(".ReactPDF__Page canvas") || [];
// this tends to have a lower quality print output -- very blurry
const iFramePrint = (canvasList) => {
const css = '@page { size: letter portrait; margin: 0; }' +
'@media print {' +
'html, body { width: 8in; height: 10in; margin: 0; }' +
'img { margin: 0; width: 100% }' +
'.print-pagebreak_after { break-after: always; page-break-after: always; }' +
'}';
const iFrame = document.createElement("iframe");
iFrame.onload = setPrint;
iFrame.style.visibility = "hidden";
iFrame.style.right = "0";
iFrame.style.bottom = "0";
iFrame.src = "about:blank";
iFrame.id = "reportPrintIFrame";
document.body.appendChild(iFrame);
const iFrameDoc = iFrame.contentWindow.document;
const iFrameStyle = iFrameDoc.createElement("style");
iFrameStyle.type = "text/css";
if (iFrameStyle.styleSheet) {
iFrameStyle.styleSheet.cssText = css;
} else {
iFrameStyle.appendChild(iFrameDoc.createTextNode(css));
}
iFrameDoc.head.appendChild(iFrameStyle);
map( (canvas) => {
const cImg = iFrameDoc.createElement("img");
cImg.src = canvas.toDataURL();
iFrameDoc.body.appendChild(cImg);
}, canvasList);
};
const urlPrint = (blobData) => {
const css = '@page { size: letter portrait; margin: 0; }' +
'@media print {' +
'html, body { width: 8in; height: 10in; margin: 0; }' +
'img { margin: 0; width: 100% }' +
'.print-pagebreak_after { break-after: always; page-break-after: always; }' +
'}';
const dataUrl = window.URL.createObjectURL(blobData);
const iFrame = document.createElement("iframe");
iFrame.onload = setPrint;
iFrame.style.visibility = "hidden";
iFrame.style.right = "0";
iFrame.style.bottom = "0";
iFrame.src = dataUrl;
iFrame.id = "reportPrintIFrame";
document.body.appendChild(iFrame);
const iFrameDoc = iFrame.contentWindow.document;
const iFrameStyle = iFrameDoc.createElement("style");
iFrameStyle.type = "text/css";
if (iFrameStyle.styleSheet) {
iFrameStyle.styleSheet.cssText = css;
} else {
iFrameStyle.appendChild(iFrameDoc.createTextNode(css));
}
iFrameDoc.head.appendChild(iFrameStyle);
};
// this has the poor UX of opening a whole new window -- but it always works, so good for IE
const canvasPrint = (canvasList) => {
const win = window.open('', "Print");
win.document.open();
map((canvas) => {
win.document.write("<br><img src='"+canvas.toDataURL()+"'/>");
}, canvasList);
setTimeout( () => {
win.focus();
win.print();
win.document.close();
win.close();
}, 100);
};
const handlePrint = () => {
const ua = window.navigator.userAgent;
const msie = ua.indexOf('MSIE ');
const trident = ua.indexOf('Trident/');
const canvasList = document.querySelectorAll(".ReactPDF__Page canvas") || [];
if (msie > 0 || trident > 0) {
canvasPrint(canvasList);
} else {
urlPrint(this.state.blobData);
// iFramePrint(canvasList);
}
};
const newWindowPrint = (blobData) => {
const win = window.open('', "Print");
const dataUrl = window.URL.createObjectURL(blobData);
const dataObj = `<object data="${dataUrl}" type="application/pdf" width="100%" height="100%"></object>`;
win.document.open();
win.document.write(`<html><head></head><body>${dataObj}</body></html>`);
setTimeout( () => {
win.focus();
win.print();
win.document.close();
win.close();
}, 100);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment