Closed UsmanFarooq-Dev closed 7 months ago
Hi,
You can use the print-js
library (https://printjs.crabbly.com/), as it enables to easily print a pdf file printJS('docs/printjs.pdf')
. The next code is a working example for this pdfjs-viewer
library:
// Create the viewer
let pdfViewer = new PDFjsViewer($('.pdfjs-viewer'));
// Load the PDF document
await pdfViewer.loadDocument("test.pdf");
// ... do things with the document
// Print the data
let pdfData = await pdfViewer.pdf.getData();
let b64Pdf=btoa(String.fromCharCode.apply(null, pdfData));
printJS({printable: b64Pdf, type: 'pdf', base64: true});
Regards.
Awesome, thank you!
Just a little thing.
I was getting "Maximum call stack size exceeded" error while converting pdfData
to base64 string on second last line of your code.
To fix this, just replace
let b64Pdf=btoa(String.fromCharCode.apply(null, pdfData));
With
let uint8Array = new Uint8Array(pdfData);
let CHUNK_SIZE = 0x8000; // Arbitrary chunk size, adjust as needed
let chunks = [];
for (let i = 0; i < uint8Array.length; i += CHUNK_SIZE) {
chunks.push(uint8Array.subarray(i, i + CHUNK_SIZE));
}
let blob = new Blob(chunks, { type: 'application/pdf' });
let b64Pdf = await new Promise((resolve) => {
let reader = new FileReader();
reader.onloadend = function () {
resolve(reader.result.split(',')[1]);
};
reader.readAsDataURL(blob);
});
Regards Usma Farooq
Hi, yes, I just wanted to make it easy for the example. Thank you for the full example. I will include in the new examples. Regards.
Thanks for creating this simple and easy to use library.
The original pdf.js library has print feature.
Is there any possibility to include this feature in this library?