How do i use wkhtmltopdf in python?

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='http://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.pdf

But 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

How do i use wkhtmltopdf in python?

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('http://www.wikipedia.org','out.pdf')

answered Apr 16, 2017 at 17:50

How do i use wkhtmltopdf in python?

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 pdfkit

Run

How do i use wkhtmltopdf in python?
Install pdfkit library

Install wkhtmltopdf

Ubuntu or Debian users can install wkhtmltopdf using below apt-get command.

sudo apt-get install wkhtmltopdf

Run

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.

How do i use wkhtmltopdf in python?
Adding wkhtmltopdf/bin to Path environment variable.

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('https://www.google.com/','sample.pdf') 

Run

The converted PDF file is saved to the current path in the command prompt or terminal.

How do i use wkhtmltopdf in python?

Output pdf file would look like

How do i use wkhtmltopdf in python?

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

How do i use wkhtmltopdf in python?

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.

  • 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

How do I import Wkhtmltopdf into Python?

Installation.
Install Xvfd: $ sudo apt-get install xvfb..
Install Fonts: $ sudo apt-get install xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic..
Install wkhtmltopdf: $ sudo apt-get install wkhtmltopdf..
Install flashplugin: $ sudo apt-get install flashplugin-nonfree..

How do I use Wkhtmltopdf?

Open a command prompt window. The syntax for using the tool is fairly simple, enter the name wkhtmltopdf, followed by the URL of the web page, and the name of the PDF that you want to create, like so. Let's say you want to save a copy of a website, this is what the command will look like.

How do I set up Wkhtmltopdf?

Visit https://wkhtmltopdf.org/downloads.html and download the appropriate stable release for Linux. For my case I chose 32-bit which is wkhtmltox-0.12..
Unzipped the DEB file..
Unzipped data. tar. xz..
Uploaded the binary in the unzipped 'usr' folder from step 3 (usr/local/bin/wkhtmltopdf).

How do I install Wkhtmltopdf in Jupyter notebook?

wkhtmltopdf (Linux).
In [5]: ! ... .
%%! echo "Install Xvfd:" sudo apt-get install xvfb echo "Install Fonts:" sudo apt-get install xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic echo "Install wkhtmltopdf:" sudo apt-get install wkhtmltopdf. ... .
%%! source activate GISpark pip install html2pdf. ... .
In [1]: ... .
In [2]: ... .