Converteer HTML naar een PDF met wkhtmltopdf

Wkhtmltopdf staat voor ‘Webkit HTML naar PDF’. Dit doet precies wat er staat.
Het is een open source command line tool welke ontwikkeld is door Apple die webpagina’s converteert naar PDF.

Zelf gebruik ik het op een website om PDF’s te genereren. Je kunt een URL ingeven die moet worden omgezet, maar ook zelf HTML naar de engine sturen. Lees hier een korte tutorial.

Installatie

Debian/Ubuntu:
Installeer wkhtmltopdf met apt-get:

$ sudo apt-get install wkhtmltopdf
$ sudo ln -s /usr/bin/wkhtmltopdf /usr/local/bin/html2pdf

Voor andere disto’s

$ wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2

$ tar xvf wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2

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

OR
$ 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 http://www.cyberciti.biz/path/to/url.html output.pdf
html2pdf http://www.cyberciti.biz/blog/print/url-slut.html output.pdf
html2pdf -option1  -option2 http://www.cyberciti.biz/blog/print/url-slut.html output.pdf

Example: Simple html to pdf file

In this example, convert out bash for loop page to a pdf file:
$ html2pdf http://www.cyberciti.biz/faq/bash-for-loop/print/ /tmp/bash.for.loop.pdf
Sample 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 generated pdf file click here. Verify pdf file, enter:
$ file /tmp/bash.for.loop.pdf
Sample outputs:

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

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

Title:          Frequently Asked Questions About Linux / UNIX » Bash For Loop Examples » Print
Creator:
Producer:       wkhtmltopdf
CreationDate:   Thu Oct  4 01:29:33 2012
Tagged:         no
Pages:          4
Encrypted:      no
Page size:      595 x 842 pts (A4)
File size:      98792 bytes
Optimized:      no
PDF version:    1.4

Grayscale pdf

The following PDF will be generated in grayscale:
$ html2pdf -g http://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf

Set orientation to Landscape or Portrait

Use the following syntax:
$ html2pdf -O Landscape http://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf
Where,

  • -O Landscape|Portrait. The default is Portrait.

How do I set page size?

Use the following syntax:
$ html2pdf -S SIZE http://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf
Where,

  • -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 http://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf

How do I see all available options?

You can see a list of commonly used options, enter:
$ wkhtmltopdf --help
OR see all available options i.e. display more extensive help, detailing less common command switches, run:
$ wkhtmltopdf -H | less

Laat een reactie achter

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *