JazzCore / python-pdfkit

Wkhtmltopdf python wrapper to convert html to pdf
MIT License
1.97k stars 329 forks source link

pdfkit error: Exit with code 1 due to network error: ProtocolUnknownError #233

Open dikshitkumarmodi opened 2 years ago

dikshitkumarmodi commented 2 years ago

pdfkit error: Exit with code 1 due to network error: ProtocolUnknownError

I'm trying to convert an HTML file to PFD with the pdfkit package. Here is my python code:

Code sample


                location = os.path.join(files_path, f'user_data/temp/{code_generator(8)}')
                with app.app_context():
                    html = render_template('email_templates/invoice.html', invoice=invoice)
                    with open(f"{location}.html",'w',encoding = 'utf-8') as f:
                        f.write(html)
                    pdfkit.from_file(f"{location}.html", `f"{location}.pdf")

But I'm getting this error:

Traceback (most recent call last):
  File "/var/www/ifileshifts/run.py", line 3, in <module>
    from ifileshifts import app, socketio#, manager
  File "/var/www/ifileshifts/ifileshifts/__init__.py", line 53, in <module>
    from ifileshifts.main.routes import main
  File "/var/www/ifileshifts/ifileshifts/main/routes.py", line 6, in <module>
    from .functions import first_otp_email, duplicate_handler, share_data, convert_size, get_size, is_integer
  File "/var/www/ifileshifts/ifileshifts/main/functions.py", line 371, in <module>
    invoice_genarator()
  File "/var/www/ifileshifts/ifileshifts/main/functions.py", line 353, in invoice_genarator
    pdfkit.from_file(f"{location}.html", f"{location}.pdf")
  File "/usr/local/lib/python3.6/dist-packages/pdfkit/api.py", line 51, in from_file
    return r.to_pdf(output_path)
  File "/usr/local/lib/python3.6/dist-packages/pdfkit/pdfkit.py", line 201, in to_pdf
    self.handle_error(exit_code, stderr)
  File "/usr/local/lib/python3.6/dist-packages/pdfkit/pdfkit.py", line 155, in handle_error
    raise IOError('wkhtmltopdf reported an error:\n' + stderr)
OSError: wkhtmltopdf reported an error:
Exit with code 1 due to network error: ProtocolUnknownError

What is wrong here? What is the solution?

Your environment

savery-endpoint commented 1 year ago

I have the same exact issue - what you need to check is for any external references in the html you're passing down to wkhtmltopdf. For me, I was referencing a font file by a relative path, and I guess wkhtmltopdf doesn't know how to traverse the filesystem here or what the base path should be. I think that possibly the best option is to use only url references and be explicit about the protocol - the protocol unknown error being the clue here. So no // or file:// - try using https:// when referring to external content.

TorhamDev commented 1 year ago

Hi :)

I had the same problem as you. If files such as images/css/fonts are located in the local storage next to the html file, you can use the following example:

import pdfkit

options = {
    "enable-local-file-access": "",
}

pdfkit.from_file("input.html", "out.pdf", options=options)
neilsh commented 1 year ago

For reference, this is due to an upstream change which was made in wkhtmltopdf 0.12.6: https://github.com/wkhtmltopdf/wkhtmltopdf/issues/4536