DEV Community

John Au-Yeung
John Au-Yeung

Posted on • Originally published at thewebdev.info

How to send a PDF file directly to the printer using JavaScript?

To send a PDF file directly to the printer using JavaScript, we create an object URL.

For instance, we write

const dataUrl = window.URL.createObjectURL(file);
const pdfWindow = window.open(dataUrl);
pdfWindow.print();
Enter fullscreen mode Exit fullscreen mode

to call createObjectURL with the PDF file to create the PDF file object URL string.

Then we open the dataUrl in a window wiith window.open.

Finally, we call print to open the print dialog to print the PDF.

Top comments (0)