I am able to generate the pdf using the Command Line wkhtmltopdf but when i use it in python lib
from wkhtmltopdf import WKhtmlToPdf wkhtmltopdf = WKhtmlToPdf( url='//www.wikipedia.org', output_file='a.pdf', )i get
'Exception: Missing url and output file arguments'asked Aug 30, 2012 at 10:08
2
I think there is an issue with the current version. I had the same issues, and if you look at their Github issues page, someone posted the same issue two days ago.
This should have worked also, according to their documentation:
python -m wkhtmltopdf.main google.com ~/google.pdfBut instead I get:
optparse.OptionConflictError: option -h/--header-html: conflicting option string(s): -h
Since it's a wrapper, I'm guessing the underlying application was updated, but the wrapper has not been.
answered Sep 1, 2012 at 2:20
tacotaco
1,36117 silver badges31 bronze badges
1
The problem in typos and rewrited API in wkhtmltopdf/main.py
Right now API is:
from wkhtmltopdf import WKhtmlToPdf wkhtmltopdf = WKhtmlToPdf('//www.wikipedia.org','out.pdf')answered Apr 16, 2017 at 17:50
Andrey NikishaevAndrey Nikishaev
3,6595 gold badges38 silver badges53 bronze badges
Contents
- Introduction
- What additional libraries or software do we need?
- Install pdfkit
- Install wkhtmltopdf
- Example 1: HTML to PDF using URL
- Example 2: Convert HTML to PDF from Local File
- Example 2: Convert HTML String to PDF
- Summary
PDF is one of the most used digital format to save or transfer documents. In this article, we will learn how to convert HTML page to PDF.
What additional libraries or software do we need?
We will use pdfkit library and wkhtmltopdf.
Install pdfkit
To install pdfkit, run the following pip command.
pip install pdfkitRun
Install wkhtmltopdf
Ubuntu or Debian users can install wkhtmltopdf using below apt-get command.
sudo apt-get install wkhtmltopdfRun
Provide the password if prompted.
Windows users can download wkhtmltopdf from this official github repository wkhtmltopdf. The file size would be around 25MB and takes a moment to download.
Once downloaded, double click on the binary file and continue with the installation. It would be mostly installed at the path C:\Program Files\wkhtmltopdf. We should add bin folder to the system PATH variable in Environment Variables. For example, C:\Program Files\wkhtmltopdf\bin.
Restart the command prompt, if you are running the python program using command prompt python command for the Path to take effect.
Example 1: HTML to PDF using URL
Now that the environment is setup, following is a simple example to convert HTML to PDF, where HTML is downloaded from a URL. We use the function from_url().
import pdfkit pdfkit.from_url('//www.google.com/','sample.pdf')Run
The converted PDF file is saved to the current path in the command prompt or terminal.
Output pdf file would look like
Example 2: Convert HTML to PDF from Local File
If your HTML file is stored locally, you can use from_file() function and convert the local HTML file to PDF.
import pdfkit pdfkit.from_file('local.html', 'sample.pdf')Run
Example 2: Convert HTML String to PDF
If your HTML data is stored in a Python variable, you can use from_string() function and convert the HTML string to PDF.
import pdfkit var htmlstr = '<h2>Heading 2</h2><p>Sample paragraph.</p>' pdfkit.from_string(htmlstr, 'sample.pdf')Run
Summary
We have successfully converted a HTML data to PDF. We have considered HTML data to be from a URL, local file or a string.
Related Tutorials
- Python Convert String to Lowercase
- Python Convert Int to String
- Python Convert String to Uppercase
- Python Convert String to Int
- Python Convert Int to Complex
- Pandas DataFrame to NumPy Array
- Python Convert Int to Float