Convert HTML Page To a PDF Using Open Source Tool [ Linux /

Do you need a simple open source cross-platform command line tool that converts web pages and HTML to a PDF file? Look no further, try wkhtmltopdf.From the project home page:

Simple shell utility to convert html to pdf using the webkit rendering engine, and qt. Searching the web, I have found several command line tools that allow you to convert a HTML-document to a PDF-document, however they all seem to use their own, and rather incomplete rendering engine, resulting in poor quality. Recently QT 4.4 was released with a WebKit widget (WebKit is the engine of Apples Safari, which is a fork of the KDE KHtml), and making a good tool became very easy.

Software featuresA note about Debian / Ubuntu Linux user

You can install wkhtmltopdf using apt-get command:$ sudo apt-get install wkhtmltopdf$ sudo ln -s /usr/bin/wkhtmltopdf /usr/local/bin/html2pdfSample outputs:

[sudo] password for vivek:Reading package lists… DoneBuilding dependency treeReading state information… DoneThe following NEW packages will be installed: wkhtmltopdf0 upgraded, 1 newly installed, 0 to remove and 10 not upgraded.Need to get 116 kB of archives.After this operation, 303 kB of additional disk space will be used.Get:1 squeeze/main wkhtmltopdf amd64 0.9.9-1 [116 kB]Fetched 116 kB in 2s (49.4 kB/s)Selecting previously deselected package wkhtmltopdf.(Reading database … 274164 files and directories currently installed.)Unpacking wkhtmltopdf (from …/wkhtmltopdf_0.9.9-1_amd64.deb) …Processing triggers for man-db …Setting up wkhtmltopdf (0.9.9-1) …Download wkhtmltopdf

Visit thispageto grab wkhtmltopdf for Linux / MS-Windows / Apple Mac OS X. You can also use thewget commandas follows:$ wget Sample outputs:

Resolving wkhtmltopdf.googlecode.com… , 2404:6800:4001:c01::52Connecting to wkhtmltopdf.googlecode.com||:80… connected.HTTP request sent, awaiting response… 200 OKLength: application/octet-stream]Saving to: `wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2’100%[======================================>] 1,11,75,276 480K/s in 23s:KB/sInstall wkhtmltopdf under Linux

Type the followingtar command to extract files:$ tar xvf wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2Sample outputs:

wkhtmltopdf-amd64

Install the same in your private~/bin/directory or in/usr/local/bindirectory:$ mv wkhtmltopdf-amd64 ~/bin/ln -s ~/bin/wkhtmltopdf-amd64 ~/bin/html2pdfOR$ sudo mv wkhtmltopdf-amd64 /usr/local/bin/ln -s /usr/local/bin/wkhtmltopdf-amd64 /usr/local/bin/html2pdf

How do I use wkhtmltopdf?

The syntax is as follows:

html2pdf output.pdfhtml2pdf output.pdfhtml2pdf -option1 -option2 output.pdfExample: Simple html to pdf file

In this example, convert outbash for loop pageto a pdf file:$ html2pdf /tmp/bash.for.loop.pdfSample outputs:

Loading pages (1/6)Counting pages (2/6)Resolving links (4/6)Loading headers and footers (5/6)Printing pages (6/6)Done

To view generatedpdf file click here. Verify pdf file, enter:$ file /tmp/bash.for.loop.pdfSample outputs:

/tmp/bash.for.loop.pdf: PDF document, version 1.4

Use thepdfinfo commandto prints the contents of the ‘Info’ dictionary (plus some other useful information) from a Portable Document Format (PDF) file:$ pdfinfo /tmp/bash.for.loop.pdfSample outputs:

Title:Frequently Asked Questions About Linux / UNIX Bash For Loop Examples PrintCreator:Producer:wkhtmltopdfCreationDate: Thu Oct 4 01:29:33 2012Tagged:noPages:4Encrypted:noPage size:595 x 842 pts (A4)File size:98792 bytesOptimized:noPDF version: 1.4Grayscale pdf

The following PDF will be generated in grayscale:$ html2pdf -g bash.for.loop.pdf

Set orientation to Landscape or Portrait

Use the following syntax:$ html2pdf -O Landscape bash.for.loop.pdfWhere,

-O Landscape|Portrait. The default is Portrait.How do I set page size?

Use the following syntax:$ html2pdf -S SIZE bash.for.loop.pdfWhere,

-s Size: Set paper size to: A4, Letter, etc. (default A4)How do I generate table of content?

A table of content can be added to the document by adding a toc the command line option. For example:$ html2pdf toc bash.for.loop.pdfSample outputs:

Fig.01: wkhtmltopdf in action

Please note that the table of content is generated based on the H tags in the input documents.How do I see all available options?世上没有绝望的处境,只有对处境绝望的人。

Convert HTML Page To a PDF Using Open Source Tool [ Linux /

相关文章:

你感兴趣的文章:

标签云: