Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML to PDF with Node.js

I'm looking to create a printable pdf version of my website webpages. Something like express.render() only render the page as pdf

Does anyone know a node module that does that ?

If not, how would you go about implementing one ? I've seen some methods talk about using headless browser like phantom.js, but not sure whats the flow.

like image 483
Michael Avatar asked Jan 27 '13 20:01

Michael


People also ask

How do I create a PDF with node?

There are two major ways to approach creating a PDF file from Nodejs. The first way is to use a PDF generation library that uses straight JavaScript/Nodejs and everything is untemplated or the other way is to use something templated in a language like HTML and then generate the pdf from it.


3 Answers

Extending upon Mustafa's answer.

A) Install http://phantomjs.org/ and then

B) install the phantom node module https://github.com/amir20/phantomjs-node

enter image description here

C) Here is an example of rendering a pdf

var phantom = require('phantom');   

phantom.create().then(function(ph) {
    ph.createPage().then(function(page) {
        page.open("http://www.google.com").then(function(status) {
            page.render('google.pdf').then(function() {
                console.log('Page Rendered');
                ph.exit();
            });
        });
    });
});

Output of the PDF:

enter image description here

EDIT: Silent printing that PDF

java -jar pdfbox-app-2.0.2.jar PrintPDF -silentPrint C:\print_mypdf.pdf

like image 155
Jozzhart Avatar answered Nov 03 '22 04:11

Jozzhart


Phantom.js is an headless webkit server and it will load any web page and render it in memory, although you might not be able to see it, there is a Screen Capture feature, in which you can export the current view as PNG, PDF, JPEG and GIF. Have a look at this example from phantom.js documentation

like image 28
Mustafa Avatar answered Nov 03 '22 03:11

Mustafa


If you want to export HTML to PDF. You have many options. without node even

Option 1: Have a button on your html page that calls window.print() function. use the browsers native html to pdf. use media queries to make your html page look good on a pdf. and you also have the print before and after events that you can use to make changes to your page before print.

Option 2. htmltocanvas or rasterizeHTML. convert your html to canvas , then call toDataURL() on the canvas object to get the image . and use a JavaScript library like jsPDF to add that image to a PDF file. Disadvantage of this approach is that the pdf doesnt become editable. If you want data extracted from PDF, there is different ways for that.

Option 3. @Jozzhard answer

like image 18
Mustafa Avatar answered Nov 03 '22 03:11

Mustafa