60

I tried installing pdfkit Python API in my windows 8 machine. I'm getting issues related to path.

Traceback (most recent call last):
  File "C:\Python27\pdfcre", line 13, in <module>
    pdfkit.from_url('http://google.com', 'out.pdf')
  File "C:\Python27\lib\site-packages\pdfkit\api.py", line 22, in from_url
    configuration=configuration)
  File "C:\Python27\lib\site-packages\pdfkit\pdfkit.py", line 38, in __init__
    self.configuration = (Configuration() if configuration is None
  File "C:\Python27\lib\site-packages\pdfkit\configuration.py", line 27, in __init__
    'https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf' % self.wkhtmltopdf)
IOError: No wkhtmltopdf executable found: ""
If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

Is anybody installed Python PDFKIt in windows machine? How to resolve this error.

My sample code :

import pdfkit
import os
config = pdfkit.configuration(wkhtmltopdf='C:\\Python27\\wkhtmltopdf\bin\\wkhtmltopdf.exe')
pdfkit.from_url('http://google.com', 'out.pdf')
Arun Prakash
  • 913
  • 2
  • 8
  • 11

14 Answers14

79

The following should work without needing to modify the windows environment variables:

import pdfkit
path_wkhtmltopdf = r'C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkhtmltopdf)
pdfkit.from_url("http://google.com", "out.pdf", configuration=config)

Assuming the path is correct of course (e.g. in my case it is r'C:\Program Files (x86)\wkhtmltopdf\bin\wkhtmltopdf.exe').

Daniel Butler
  • 3,239
  • 2
  • 24
  • 37
kadee
  • 8,067
  • 1
  • 39
  • 31
  • 8
    Just to make it obvious to readers who might not have admin rights, you do not need to actually run the wkhtmltopdf installer after downloading, you can just unzip the `.exe` and then point to the `\bin\wkhtmltopdf.exe` file inside the unzipped `.exe`. For me (on a Windows machine without privileges) this was the most suitable of all the Python html-to-pdf solutions I tested (I also tried weasyprint and xhtml2pdf). – Alexander Dec 17 '19 at 15:04
  • 1
    For those who use VS code, restart VS Code or maybe just the Terminal. After inserting the Environment variable. I got errors but after restarting VS Code it worked. – elano7 Mar 26 '20 at 18:57
  • Yes it worked. I first missed the configuration=config, but when added it created the pdf. Thanks magical. – Michael Larsson May 02 '22 at 10:57
28

Please install wkhtmltopdf using,

sudo apt install -y wkhtmltopdf

for windows machine install it from below link, http://wkhtmltopdf.org/downloads.html

and you need to add wkhtmltopdf path into environment variables

Nam G VU
  • 33,193
  • 69
  • 233
  • 372
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
  • You also need to add it to the PATH on Windows according to the pdfkit wiki link here https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf – Max Jun 30 '17 at 02:09
18

IOError: 'No wkhtmltopdf executable found'

Make sure that you have wkhtmltopdf in your $PATH or set via custom configuration. where wkhtmltopdf in Windows or which wkhtmltopdf on Linux should return actual path to binary.

Adding this configuration line worked for me:

config = pdfkit.configuration(wkhtmltopdf="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe")
pdfkit.from_string(html, 'MyPDF.pdf', configuration=config)

From github

Seems you need to pass configuration=config as argument.

Rutrus
  • 1,367
  • 15
  • 27
  • 2
    This is a godsend....I have been struggling to get Weasyprint or PyQT5 to do something like this for a day. This is so simple. Thank you! – Korzak Feb 26 '19 at 19:09
5

I am learning python today, and I met the same problem, lately I set the windows enviroment variables and everything is OK.
I add the install path of wkhtml to the path, for example:"D:\developAssistTools\wkhtmltopdf\bin;" is my install path of wkhtml, and I add it to the path, everything is OK.

import pdfkit
pdfkit.from_url("http://google.com", "out.pdf")

finally, I find a out.pdf.

feng smith
  • 1,487
  • 2
  • 9
  • 22
4
import pdfkit
path_wkthmltopdf = b'C:\Program Files\wkhtmltopdf\\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
pdfkit.from_url("http://google.com", "rajul-url.pdf", configuration=config)
pdfkit.from_file("output.xml","rajul-pdf.pdf", configuration=config)

The Above Code block is working perfectly fine for me. Please note that file which needs to be converted is in the same directory where the pdf file is creating.

3

Ran into the same problem on a Mac. For some reason-- it worked after unistalling the pip installation and reinstall wkhtmltopdf using brew

pip uninstall wthtmltopdf

and use brew

brew install Caskroom/cask/wkhtmltopdf
anjchang
  • 483
  • 1
  • 7
  • 14
  • 2
    After installation on MacOS I got an error: `OSError: wkhtmltopdf reported an error: Exit with code 1 due to network error: ProtocolUnknownError`. Solution: You need to set options `{"enable-local-file-access": ""}`. For example: `pdfkit.from_string(_html, pdf_path, options={"enable-local-file-access": ""})` – Milovan Tomašević Dec 21 '22 at 21:44
2

You need set:

pdfkit.from_url('http://google.com', 'out.pdf',configuration=config)
jtlz2
  • 7,700
  • 9
  • 64
  • 114
Wang Zoro
  • 29
  • 1
1
def urltopdf(url,pdffile):
    import pdfkit
    '''
        input
        - url : target url
        - pdffile : target pdf file name
    '''
    path_wkthmltopdf = 'D:\\Program Files (x86)\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    #pdfkit.from_url(url=urlpath, output_path=pdffilepath,configuration=config)
    pdfkit.from_url(url,pdffile,configuration=config)


urltopdf('http://www.google.com','pdf/google.pdf')

very good solution! thanks everyone!

1

When I tried all of the above methods, I was till facing Permission Error as I don't have the admin rights to my workstation. If that's the case for you too, then make sure when you install your wkhtmltopdf.exe. The destination folder for installation is in your python site-packages folder, or add the directory to sys.path. Normally it gets installed in Program files folder. I changed the installation directory and this works for me:

import pdfkit
pdfkit.from_url("http://google.com", "out.pdf")
Archie25
  • 11
  • 2
0

Found the decode on a windows platform needed to be a binary string, try:

        path_wkthmltopdf = b'C:\Program Files\wkhtmltopdf\\bin\wkhtmltopdf.exe'
        config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
        pdfkit.from_url(url=urlpath, output_path=pdffilepath,configuration=config)
kariato
  • 169
  • 1
  • 4
0

[For Ubuntu/Debian]

first run: sudo apt-get update --fix-missing

then: sudo apt-get install -y wkhtmltopdf

hope it would solve your problem.

Asif Alam
  • 59
  • 2
0

for windows Try to use the complete path C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe

Imad77
  • 39
  • 4
-1

If you have added the path but still getting the error Just simply restart the vs code and the error will gone

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 25 '23 at 12:57
-2

No need to write wkhtmltopdf path into code. Just define an environment variable for that, and it works.

import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')

For me this code is working.

Waqar Detho
  • 1,502
  • 18
  • 17