Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

                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?

like image 273
Kute Konok Avatar asked Oct 20 '25 16:10

Kute Konok


1 Answers

Edit your code where you wrote this

pdfkit.from_file(f"{location}.html", f"{location}.pdf")
#to 
pdfkit.from_file(f"{location}.html", f"{location}.pdf", options={"enable-local-file-access": ""})
like image 178
Zobaer Avatar answered Oct 23 '25 05:10

Zobaer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!