om_file("webapp/index.html", "index.pdf", verbose=True, options=)
You can also convert a local HTML file in your machine to a PDF document here's how: # from file The first argument to the from_url() function is the URL you want to convert, and the second argument is the PDF document name you wish to generate.
Let's install it: $ pip install pdfkitįor instance, let's convert the Google search page to a PDF document: import pdfkit Pdfkit did a great job wrapping wkhtmltopdf in Python we use effortless methods to do such complicated tasks. You can simply install it using brew: $ brew install Caskroom/cask/wkhtmltopdf Converting HTML from URL to PDF If you're on Linux, it's much simpler as it'll be added to PATH automatically using your package manager.īelow is the command for Ubuntu/Debian: $ apt updateĪnd below is for Debian/CentOS: $ sudo yum makecache -refresh Once you click on Edit on either variables, go on and add the path of where you've installed wkhtmltopdf to the PATH variable:Īfter you've done that, click the OK button and close the previous windows, and you're good to go.
In the new window, you're free to choose the system or user variables and find the PATH variable to edit: َA new window will appear, and click on "Environment Variables.": To do it, you must go to Windows search and write "environment", you'll see "Edit the system environment variables", click on it: In my case, I downloaded the 64-bit architecture one that is supported on Vista or later since I have Windows 10.Īfter you have downloaded the installer and successfully installed the wkhtmltopdf tool, now you need to add it to the PATH environment variable.
Go to the wkhtmltopdf official downloads page, and download the Windows installer for your Windows architecture. To get started, we have to install wkhtmltopdf tool and its pdfkit wrapper in Python. Here is the table of contents of this tutorial: We will use the wkhtmltopdf tool, an open-source command-line utility that renders HTML into PDF using the Qt WebKit rendering engine. In this tutorial, you will learn how you can do that with Python. We have considered HTML data to be from a URL, local file or a string.There are a lot of online tools that provide converting HTML to PDF documents, and most of them are free. We have successfully converted a HTML data to PDF.
om_string(htmlstr, 'sample.pdf') Run Summary Var htmlstr = 'Heading 2Sample paragraph.' If your HTML data is stored in a Python variable, you can use from_string() function and convert the HTML string to PDF. om_file('local.html', 'sample.pdf') Run Example 2: Convert HTML String to PDF If your HTML file is stored locally, you can use from_file() function and convert the local HTML file to PDF. Output pdf file would look like Example 2: Convert HTML to PDF from Local File The converted PDF file is saved to the current path in the command prompt or terminal. Now that the environment is setup, following is a simple example to convert HTML to PDF, where HTML is downloaded from a URL. Restart the command prompt, if you are running the python program using command prompt python command for the Path to take effect. Adding wkhtmltopdf/bin to Path environment variable. For example, C:\Program Files\wkhtmltopdf\bin. We should add bin folder to the system PATH variable in Environment Variables. It would be mostly installed at the path C:\Program Files\wkhtmltopdf.
Once downloaded, double click on the binary file and continue with the installation.
The file size would be around 25MB and takes a moment to download. Windows users can download wkhtmltopdf from this official github repository wkhtmltopdf. Ubuntu or Debian users can install wkhtmltopdf using below apt-get command. pip install pdfkit Run Install pdfkit library Install wkhtmltopdf To install pdfkit, run the following pip command.