{"id":35940,"date":"2022-10-30T14:00:04","date_gmt":"2022-10-30T14:00:04","guid":{"rendered":"https:\/\/www.askpython.com\/?p=35940"},"modified":"2023-02-16T19:56:36","modified_gmt":"2023-02-16T19:56:36","slug":"pdfkit-module","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/pdfkit-module","title":{"rendered":"PDFKit Module in Python &#8211; Convert HTML to PDF"},"content":{"rendered":"\n<p>In this article let&#8217;s learn about a PDFKit, which is\u00a0a PDF document generation open-source library. It is the easiest way to convert any webpage into a PDF using Python. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites for Converting HTML to PDF<\/h2>\n\n\n\n<p>Before we start with the code python, make sure to install PDFKit. To install PDFKit below are the commands to be used.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ pip install pdfkit  (or pip3 for python3)<\/code><\/pre>\n\n\n\n<p>Once the library is installed, install <code>wkhtmltopdf<\/code>. It is an open-source command line tool used to render HTML into PDF and various image formats using the Qt WebKit rendering engine.<\/p>\n\n\n\n<p>For Debian\/Ubuntu:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo apt-get install wkhtmltopdf<\/code><\/pre>\n\n\n\n<p>For macOS<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ brew install homebrew\/cask\/wkhtmltopdf<\/code><\/pre>\n\n\n\n<p>For Windows <a href=\"https:\/\/github.com\/wkhtmltopdf\/wkhtmltopdf\/releases\/download\/0.12.4\/wkhtmltox-0.12.4_msvc2015-win64.exe\" target=\"_blank\" rel=\"noopener\">Click Here.<\/a><\/p>\n\n\n\n<p><em>Remember to add the path <\/em> <code>wkhtmltopdf <\/code><em>to environment variables to avoid errors.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Converting HTML to PDF using Python<\/h2>\n\n\n\n<p>While implementing if your editor throws an error like &#8220;ImportError: No module named pdfkit&#8221; try to configure the path in your current directory. This can be done by executing the following code in your editor beforehand. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\npath_wkthmltopdf = &#039;C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe&#039;\nconfig = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)\n\n<\/pre><\/div>\n\n\n<p><strong>To convert a website URL to PDF<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pdfkit\n\npdfkit.from_url(&quot;https:\/\/www.askpython.com\/&quot;, &quot;sample_url.pdf&quot;)\n<\/pre><\/div>\n\n\n<p><strong>OUTPUT<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-1-1024x683.png\" alt=\"Pdfkit 1\" class=\"wp-image-35951\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-1-1024x683.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-1-300x200.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-1-768x512.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-1.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Pdfkit 1<\/figcaption><\/figure>\n\n\n\n<p><strong>To convert HTML files into PDF<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Remember the file you want to convert into PDF should be in <code>.HTML <\/code>format.<\/li><li>Also, the file should be present in the same directory.<\/li><\/ul>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pdfkit\n\npdfkit.from_file(&quot;sample.html&quot;, &quot;sample_file.pdf&quot;)\n<\/pre><\/div>\n\n\n<p><strong>OUTPUT<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-2-1024x683.png\" alt=\"Pdfkit 2\" class=\"wp-image-35952\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-2-1024x683.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-2-300x200.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-2-768x512.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-2.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Pdfkit 2<\/figcaption><\/figure>\n\n\n\n<p><strong>To convert a string into a PDF<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pdfkit\n\npdfkit.from_string(&#039;AskPython :)&#039;,&#039;sample_string.pdf&#039;)\n<\/pre><\/div>\n\n\n<p><strong>OUTPUT<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-3-1024x683.png\" alt=\"Pdfkit 3\" class=\"wp-image-35953\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-3-1024x683.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-3-300x200.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-3-768x512.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2022\/10\/pdfkit-3.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Pdfkit 3<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this article, we have discussed the PDFKit wrapper or library. PDFKit for python is an adapted version of&nbsp;the ruby PDFKit&nbsp;library. Also, we learned the easiest way to convert web pages, HTML files, or strings into PDFs right in python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article let&#8217;s learn about a PDFKit, which is\u00a0a PDF document generation open-source library. It is the easiest way to convert any webpage into a PDF using Python. Prerequisites for Converting HTML to PDF Before we start with the code python, make sure to install PDFKit. To install PDFKit below are the commands to [&hellip;]<\/p>\n","protected":false},"author":41,"featured_media":35954,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-35940","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/35940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=35940"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/35940\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/35954"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=35940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=35940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=35940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}