Why am I getting wkhtmltopdf IO Error when invoking via python-pdfkit?
Asked Answered
H

2

5

I'm trying to upgrade my wkhtmltopdf package to 0.12.2.1 and not having any luck.

Below is what I've done/tried so far. Has anyone had any luck updating their setup? Or any pointers would be greatly helpful! Thanks!

Out of the box I got Configuration Error I hard coded to simply get past this.

# -*- coding: utf-8 -*-
import subprocess
import sys

class Configuration(object):
    def __init__(self, wkhtmltopdf='', meta_tag_prefix='pdfkit-'):
        self.meta_tag_prefix = meta_tag_prefix

        self.wkhtmltopdf = wkhtmltopdf

        if not self.wkhtmltopdf:
            if sys.platform == 'win32':
                self.wkhtmltopdf = subprocess.Popen(                    ['where', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].strip()
            else:
                self.wkhtmltopdf = subprocess.Popen( ['which', 'wkhtmltopdf'], stdout=subprocess.PIPE).communicate()[0].strip()

        try:
            #with open(self.wkhtmltopdf) as f:
           with open("/usr/local/bin/wkhtmltopdf") as f:
                pass
        except IOError:
            raise IOError('No wkhtmltopdf executable found: "%s"\n'
                          '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' % self.wkhtmltopdf)

Now I'm getting this error and do not know how to proceed. I tried adding shell=True to the Popen above but that didn't work either.

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/rq/worker.py", line 543, in perform_job
    rv = job.perform()
  File "/usr/local/lib/python2.7/dist-packages/rq/job.py", line 490, in perform
    self._result = self.func(*self.args, **self.kwargs)
  File "/home/worker-1/Desktop/Dropbox/changeaddress/facts/jobs.py", line 864, in job_sharepdfs
    mymovepdf_link = build_mymovepdf(account_uuid, addresschange_uuid)
  File "/home/worker-1/Desktop/Dropbox/changeaddress/facts/jobs.py", line 608, in build_mymovepdf
    s3file = pdfkit.from_string( output.getvalue() , False )
  File "/usr/local/lib/python2.7/dist-packages/pdfkit/api.py", line 68, in from_string
    return r.to_pdf(output_path)
  File "/usr/local/lib/python2.7/dist-packages/pdfkit/pdfkit.py", line 93, in to_pdf
    stderr=subprocess.PIPE)
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
Hacienda answered 13/3, 2015 at 6:40 Comment(0)
V
4

You have to set configuration (path) of your wkhtmltopdf library.

I have created a basic helper function like below,

import os
import pdfkit

WKHTMLTOPDF_PATH = '/usr/local/bin/wkhtmltopdf'

def generate_pdf(html, static_path,  _path):
    config = pdfkit.configuration(wkhtmltopdf=WKHTMLTOPDF_PATH)
    _status = pdfkit.from_string(
        html,
        os.path.join(static_path, _path),
        configuration=config,
        options={
            'page-size': 'A4',
            'margin-top': '0',
            'margin-right': '0',
            'margin-left': '0',
            'margin-bottom': '0',
            'zoom': '1.2',
            'encoding': "UTF-8",
        })
    return _path if _status else ''

Use:

html = "<h1>Hello World !!!</h1>"
static_path = "/static/"
file_path = "pdfs/out.pdf"
generate_pdf(html, static_path, file_path)
Vada answered 20/10, 2015 at 10:29 Comment(0)
V
3

Another solution (installing wkhtmltopdf):

wget http://download.gna.org/wkhtmltopdf/0.12/0.12.3/wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
tar xvf wkhtmltox-0.12.3_linux-generic-amd64.tar.xz
sudo mv wkhtmltox/bin/wkhtmltopdf /usr/local/bin/wkhtmltopdf
sudo chmod +x /usr/local/bin/wkhtmltopdf
Vada answered 23/2, 2017 at 11:42 Comment(2)
Please be informed that download.gna.org is closed now. So if you want to download lates binaries visit this linkMair
Any solution Mac M1 Chip ??Umbra

© 2022 - 2024 — McMap. All rights reserved.