Convert html document to PDF on Ubuntu
wkhtmltopdf, a program capable of converting html documents into PDF documents.wkhtmltopdf is able to put several objects into the output file, an object is either a single webpage, a cover webpage or a table of content. The objects are put into the output document in the order they are specified on the command line, options can be specified on a per object basis or in the global options area.
Step 1> Install Dependencies
Install following Dependencies using command given below:
# apt-get install openssl build-essential xorg libssl-dev
Step 2: Install wkhtmltopdf packages
Install wkhtmltopdf packages using following command:
# apt-get install wkhtmltopdf
Step 3: Convert Document
After installing all required packages now we can try to convert document using following command:
# /usr/bin/wkhtmltopdf http://www.techoism.com techoism.pdf
Note:
If it works, then you are done. If you get the error “Cannot connect to X server” then follow below steps:
Step 4: Install xvfb packages
We need to run it headless on a ‘virtual’ x server. We will do this with a package called xvfb. Follow below command to install package:
# apt-get install xvfb
Step 5: Create script
We need to write a little shell script to wrap wkhtmltopdf in xvfb. Make a file called wkhtmltopdf.sh and add the following:
# vim /usr/local/bin/wkhtmltopdf.sh # xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf "$@"
Step 6: Set Permission
After creating script we have to set permission to that script using following command:
# chmod +x /usr/local/bin/wkhtmltopdf.sh
Step 7: Convert Document using script
After setting permission to script now we can convert document using script.
# /usr/local/bin/wkhtmltopdf.sh http://www.techoism.com techoism.pdf