Question

Here is the thing. I have a html page having some content. I want to deploy a button so that using that i can make pdf of the contents of that page. i am using nodejs and want to make that pdf using wkhtmltopdf. I am new in web development. so a sample code or demonstration will be great help!

Was it helpful?

Solution

You can use wkhtmltopdf module to convert HTML files or strings to PDFs. Call whtmltopdf function with either a URL or an inline HTML string, and it returns a stream that you can read from or pipe to wherever you like (e.g. a file, or an HTTP response).

Here is the step by step guide on Ubuntu 13.10 Desktop(64 bit).

1.Install the wkhtmltopdf command line tool on your system:

$ sudo apt-get install wkhtmltopdf

2.Create the project directory html2pdf and install the wkhtmltopdf module:

$ mkdir html2pdf && cd html2pdf
$ npm install wkhtmltopdf

3.Edit the app.js file and input the following code:

var wkhtmltopdf = require('wkhtmltopdf');
wkhtmltopdf('<h1>Test</h1><p>Hello world</p>', {output: 'out.pdf'});

4.Run the application and a PDF file named out.pdf will be created in the html2pdf directory.

$ node app.js

=============================================================================

For Windows:

1.Download wkhtmltopdf command line tool. Note: There are two versions of wkhtmltopdf.exe. One is for 32 bit Windows and the other is for 64 bit Windows.

2.Run the downloaded exe file and install wkhtmltopdf to specified directory, e.g., c:\wkhtmltopdf.

3.Create the project directory html2pdf and install the wkhtmltopdf module:

C:\> md html2pdf
C:\> cd html2pdf
C:\html2pdf> npm install wkhtmltopdf

4.Edit the app.js file and input the following code:

var wkhtmltopdf = require('wkhtmltopdf');
wkhtmltopdf.command = 'c:/wkhtmltopdf/bin/wkhtmltopdf.exe';
wkhtmltopdf('<h1>Test</h1><p>Hello world</p>', {output: 'out.pdf'});

5.After running the application, a PDF file named out.pdf will be created in the html2pdf directory.

C:\html2pdf> node app.js
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top