{"id":4287,"date":"2012-10-04T07:14:12","date_gmt":"2012-10-04T07:14:12","guid":{"rendered":"https:\/\/noi3.org\/site\/?p=4287"},"modified":"2012-10-04T07:14:12","modified_gmt":"2012-10-04T07:14:12","slug":"convert-html-page-to-a-pdf-using-open-source-tool-linux-os-x-windows","status":"publish","type":"post","link":"https:\/\/site.noi3.org\/?p=4287","title":{"rendered":"Convert HTML Page To a PDF Using Open Source Tool [ Linux \/ OS X \/ Windows ]"},"content":{"rendered":"<p> \t<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-1683\" alt=\"\" src=\"https:\/\/noi3.org\/site\/wp-content\/uploads\/2012\/06\/pdf.png\" style=\"height: 250px; width: 250px;\" width=\"0\" height=\"0\" \/>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.<\/p>\n<p>  <!--more-->  <\/p>\n<p> \t<span class=\"drop_cap\">D<\/span>o 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.<\/p>\n<p> \tFrom the project home page:<\/p>\n<p> \tSimple 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.<\/p>\n<h2> \tSoftware features<\/h2>\n<ol>\n<li> \t\tCross platform.<\/li>\n<li> \t\tOpen source.<\/li>\n<li> \t\tConvert any web pages into PDF documents using webkit.<\/li>\n<li> \t\tYou can add headers and footers.<\/li>\n<li> \t\tTOC generation.<\/li>\n<li> \t\tBatch mode conversions.<\/li>\n<li> \t\tCan run on Linux server with an XServer (the X11 client libs must be installed).<\/li>\n<li> \t\tCan be directly used by PHP or Python via bindings to libwkhtmltox.<\/li>\n<\/ol>\n<h2> \tDownload wkhtmltopdf<\/h2>\n<p> \tVisit this <a href=\"http:\/\/code.google.com\/p\/wkhtmltopdf\/downloads\/list\" rel=\"nofollow\" target=\"_blank\">page<\/a> to grab wkhtmltopdf for Linux \/ MS-Windows \/ Apple Mac OS X. You can also use the <a href=\"http:\/\/www.cyberciti.biz\/tips\/linux-wget-your-ultimate-command-line-downloader.html\" rel=\"nofollow\" target=\"_blank\">wget command<\/a> as follows:<br \/> \t<code>$ wget http:\/\/wkhtmltopdf.googlecode.com\/files\/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2<\/code><br \/> \tSample outputs:<\/p>\n<pre class=\"ini\"> Resolving wkhtmltopdf.googlecode.com... <span style=\"\">74.125<\/span><span style=\"\">.135<\/span><span style=\"\">.82<\/span>, <span style=\"\">2404<\/span>:<span style=\"\">6800<\/span>:<span style=\"\">4001<\/span>:c01::<span style=\"\">52<\/span> Connecting to wkhtmltopdf.googlecode.com|<span style=\"\">74.125<\/span><span style=\"\">.135<\/span><span style=\"\">.82<\/span>|:<span style=\"\">80<\/span>... connected. HTTP request sent, awaiting response... <span style=\"\">200<\/span> OK Length: <span style=\"\">11175276<\/span> <span style=\"\">(<\/span>11M<span style=\"\">)<\/span> <span style=\"color:#000066;font-weight:bold;\"><span style=\"\">[<\/span>application\/octet-stream<span style=\"\">]<\/span><\/span> Saving to: `wkhtmltopdf<span style=\"\">-0.11<\/span>.0_rc1-static-amd64.tar.bz2&#39; &nbsp; <span style=\"\">100<\/span>%<span style=\"color:#000066;font-weight:bold;\"><span style=\"\">[<\/span>======================================&gt;<span style=\"\">]<\/span><\/span> <span style=\"\">1<\/span>,<span style=\"\">11<\/span>,<span style=\"\">75<\/span>,<span style=\"\">276<\/span>  480K\/s   in 23s &nbsp; <span style=\"\">2012<\/span><span style=\"\">-10<\/span><span style=\"\">-04<\/span> <span style=\"\">01<\/span>:<span style=\"\">21<\/span>:<span style=\"\">43<\/span> <span style=\"\">(<\/span><span style=\"\">477<\/span> KB\/s<span style=\"\">)<\/span> - `wkhtmltopdf<span style=\"\">-0.11<\/span>.0_rc1-static-amd64.tar.bz2&#39; saved <span style=\"color:#000066;font-weight:bold;\"><span style=\"\">[<\/span><span style=\"\">11175276<\/span>\/<span style=\"\">11175276<\/span><span style=\"\">]<\/span><\/span> &nbsp;<\/pre>\n<h2> \tInstall wkhtmltopdf under Linux<\/h2>\n<p> \tType the following <a href=\"http:\/\/www.cyberciti.biz\/faq\/tar-extract-linux\/\" rel=\"nofollow\" target=\"_blank\">tar command to extract files<\/a>:<br \/> \t<code>$ tar xvf wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2<\/code><br \/> \tSample outputs:<\/p>\n<pre> wkhtmltopdf-amd64<\/pre>\n<p> \tInstall the same in your private <kbd>~\/bin\/<\/kbd> directory or in <kbd>\/usr\/local\/bin<\/kbd> directory:<br \/> \t<code>$ mv wkhtmltopdf-amd64 ~\/bin\/<br \/> \tln -s ~\/bin\/wkhtmltopdf-amd64 ~\/bin\/html2pdf<\/code><br \/> \tOR<br \/> \t<code>$ sudo mv wkhtmltopdf-amd64 \/usr\/local\/bin\/<br \/> \tln -s \/usr\/local\/bin\/wkhtmltopdf-amd64 \/usr\/local\/bin\/html2pdf<\/code><\/p>\n<h2> \tHow do I use wkhtmltopdf?<\/h2>\n<p> \tThe syntax is as follows:<\/p>\n<pre class=\"bash\"> &nbsp; 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 &nbsp;<\/pre>\n<h3> \tExample: Simple html to pdf file<\/h3>\n<p> \tIn this example, convert out <a href=\"http:\/\/www.cyberciti.biz\/faq\/bash-for-loop\/\" rel=\"nofollow\" target=\"_blank\">bash for loop page<\/a> to a pdf file:<br \/> \t<code>$ html2pdf http:\/\/www.cyberciti.biz\/faq\/bash-for-loop\/print\/ \/tmp\/bash.for.loop.pdf<\/code><br \/> \tSample outputs:<\/p>\n<pre> Loading pages (1\/6) Counting pages (2\/6) Resolving links (4\/6) Loading headers and footers (5\/6) Printing pages (6\/6) Done <\/pre>\n<p> \tTo view generated <a href=\"https:\/\/noi3.org\/site\/wp-content\/uploads\/2012\/10\/bash.for_.loop_.pdf\" rel=\"nofollow\" target=\"_blank\">pdf file click here<\/a>. Verify pdf file, enter:<br \/> \t<code>$ file \/tmp\/bash.for.loop.pdf <\/code><br \/> \tSample outputs:<\/p>\n<pre> \/tmp\/bash.for.loop.pdf: PDF document, version 1.4<\/pre>\n<p> \tUse the <a href=\"http:\/\/www.cyberciti.biz\/faq\/linux-unix-view-technical-details-of-pdf\/\" rel=\"nofollow\" target=\"_blank\">pdfinfo command<\/a> to prints the contents of the &#39;Info&#39; dictionary (plus some other useful information) from a Portable Document Format (PDF) file:<br \/> \t<code>$ pdfinfo \/tmp\/bash.for.loop.pdf <\/code><br \/> \tSample outputs:<\/p>\n<pre> Title:          Frequently Asked Questions About Linux \/ UNIX &raquo; Bash For Loop Examples &raquo; 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 <\/pre>\n<h3> \tGrayscale pdf<\/h3>\n<p> \tThe following PDF will be generated in grayscale:<br \/> \t<code>$ html2pdf -g http:\/\/www.cyberciti.biz\/faq\/bash-for-loop\/print\/ bash.for.loop.pdf<\/code><\/p>\n<h3> \tSet orientation to Landscape or Portrait<\/h3>\n<p> \tUse the following syntax:<br \/> \t<code>$ html2pdf -O Landscape http:\/\/www.cyberciti.biz\/faq\/bash-for-loop\/print\/ bash.for.loop.pdf<\/code><br \/> \tWhere,<\/p>\n<ul>\n<li> \t\t<kbd>-O Landscape|Portrait<\/kbd>. The default is Portrait.<\/li>\n<\/ul>\n<h3> \tHow do I set page size?<\/h3>\n<p> \tUse the following syntax:<br \/> \t<code>$ html2pdf -S SIZE http:\/\/www.cyberciti.biz\/faq\/bash-for-loop\/print\/ bash.for.loop.pdf<\/code><br \/> \tWhere,<\/p>\n<ul>\n<li> \t\t<kbd>-s Size<\/kbd> : Set paper size to: A4, Letter, etc. (default A4)<\/li>\n<\/ul>\n<h3> \tHow do I generate table of content?<\/h3>\n<p> \tA table of content can be added to the document by adding a toc the command line option. For example:<br \/> \t<code>$ html2pdf toc http:\/\/www.cyberciti.biz\/faq\/bash-for-loop\/print\/ bash.for.loop.pdf<\/code><br \/> \tSample outputs:<\/p>\n<p> \t<img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-4286\" alt=\"Linux \/ Unix HTML to PDF File Command Line Option\" class=\"size-full wp-image-239\" height=\"586\" src=\"https:\/\/noi3.org\/site\/wp-content\/uploads\/2012\/10\/wkhtmltopdf-amd64.output.png\" title=\"Linux \/ Unix HTML to PDF File Command Line Option\" width=\"593\" srcset=\"https:\/\/site.noi3.org\/wp-content\/uploads\/2012\/10\/wkhtmltopdf-amd64.output.png 593w, https:\/\/site.noi3.org\/wp-content\/uploads\/2012\/10\/wkhtmltopdf-amd64.output-300x296.png 300w, https:\/\/site.noi3.org\/wp-content\/uploads\/2012\/10\/wkhtmltopdf-amd64.output-100x100.png 100w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/>{{all}}<\/p>\n<p class=\"wp-caption-text\"> \tFig.01: wkhtmltopdf in action<\/p>\n<p> \tPlease note that the table of content is generated based on the H tags in the input documents.<\/p>\n<h3> \tHow do I see all available options?<\/h3>\n<p> \tYou can see a list of commonly used options, enter:<br \/> \t<code>$ wkhtmltopdf --help<\/code><br \/> \tOR see all available options i.e. display more extensive help, detailing less common command switches, run:<br \/> \t<code>$ wkhtmltopdf -H | less<\/code><\/p>\n<p> \t&nbsp;<\/p>\n<hr \/>\n<p> \t<a href=\"http:\/\/www.cyberciti.biz\/open-source\/html-to-pdf-freeware-linux-osx-windows-software\/\">Articolul original<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":1,"featured_media":1683,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[984,982,983,312,790],"class_list":["post-4287","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-informatica","tag-ajutor","tag-generare","tag-html","tag-linux","tag-pdf"],"_links":{"self":[{"href":"https:\/\/site.noi3.org\/index.php?rest_route=\/wp\/v2\/posts\/4287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/site.noi3.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/site.noi3.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/site.noi3.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/site.noi3.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4287"}],"version-history":[{"count":0,"href":"https:\/\/site.noi3.org\/index.php?rest_route=\/wp\/v2\/posts\/4287\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/site.noi3.org\/index.php?rest_route=\/wp\/v2\/media\/1683"}],"wp:attachment":[{"href":"https:\/\/site.noi3.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/site.noi3.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/site.noi3.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}