Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML to PDF conversion using Chrome pdfium

Can anyone suggest how to convert HTML to PDF using chrome pdfium.

Since, I tried pdf.js and few other, but It is not giving me a proper result.

I think chrome pdfium handle them properly. So, I would like to give a try to pdfium for generating PDF file from HTML source.

Thanks.

like image 609
john Avatar asked Oct 24 '14 20:10

john


People also ask

How do I convert my chrome HTML to PDF?

Here's how to convert a Chrome HTML web page to PDF:Browse to the desired web page. Click the More Options button —three vertical dots on the far-left of the browser's top ribbon. Click on the Print option. Change Destination to Save As PDF.

How do I automatically convert HTML to PDF?

On a Windows computer, open an HTML web page in Internet Explorer, Google Chrome, or Firefox. On a Mac, open an HTML web page in Firefox. Click the “Convert to PDF” button in the Adobe PDF toolbar to start the PDF conversion. Enter a file name and save your new PDF file in a desired location.


1 Answers

PDFium (and PDF.js) are merely PDF Viewers, you cannot convert HTML to PDF with them.

The actual PDF generation in Chrome is done by the printing component using Blink (the rendering engine) and Skia (the graphic stack). There is an effort going on to decouple the printing functionality from the printing UI so that third-party projects can use the printing functionality (crbug.com/311308), but it is still work-in-progress.
Chrome 59 and up can be used to convert HTML to PDF, via the --print-to-pdf flag. See crbug.com/603559 for the specific feature itself, and https://chromium.googlesource.com/chromium/src/+/lkgr/headless/README.md for documentation about headless Chromium in general. This example loads example.com and outputs the result to `path/to/file.pdf.

chrome --headless --print-to-pdf=path/to/file.pdf https://example.com 

If you want to know how to use Chrome's built-in PDF generator in JavaScript, follow the following question (which has not been answered yet): Javascript call programmatically the "Save as PDF" feature of Chrome dialog print.

like image 185
Rob W Avatar answered Sep 18 '22 15:09

Rob W