PDF generation fails: wkhtmltopdf ConnectionRefusedError

Information about bug

There are many issues raised in the past about generating PDFs with wkhtmltopdf, I have been playing around with different troubleshooting options - and I believe I have conceptually figured out the solution but don’t know enough Python or about ERPNext to resolve it:

Changing host_name in sites/[sitename]/site_config.json from my actual host/domain name to "https://127.0.0.1:443" or "http://127.0.0.1:80" allows wkhtmltopdf to work, but it then breaks OAuth for logging in via Office365, links in the footer of emails to open the documents online, etc. - basically, anywhere the host name value is factored in.

I believe we need to dynamically replace the hostname being passed to wkhtmltopdf from the PDF button from the actual hostname to the localhost

image

Sources:

Follow-on issue - this issue makes it impossible to send emails from the system as wkhtmltopdf doesn’t let them go through

I’m using the Bitnami VM for ERPNext

Module

accounts, stock, buying, selling, ecommerce, manufacturing, HR, projects, support, CRM, assets, integrations, quality, regional, portal, agriculture, education, non-profit, other

Version

Akwad UI Fixes: v1.0.4
Desk Navbar Extended: v0.0.1
ERPNext: v14.28.1
ERPNext OCR: v1.0.1
Frappe Framework: v14.40.1
Frappe M365: v0.0.1
Frappe Insights: v0.6.2
Microsoft Integration: v0.0.1
One Compliance: v0.0.1
Payments: v0.0.1
SCS Sales-Purchase: v0.0.1
Simple Stock: v0.0.1
Wiki: v2.0.0

Installation method

manual install

Relevant log output / Stack trace / Full Error Message.

Traceback (most recent call last):
File “apps/frappe/frappe/app.py”, line 66, in application
response = frappe.api.handle()
File “apps/frappe/frappe/api.py”, line 54, in handle
return frappe.handler.handle()
File “apps/frappe/frappe/handler.py”, line 47, in handle
data = execute_cmd(cmd)
File “apps/frappe/frappe/handler.py”, line 85, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “apps/frappe/frappe/init.py”, line 1608, in call
return fn(*args, **newargs)
File “apps/frappe/frappe/utils/print_format.py”, line 133, in download_pdf
pdf_file = frappe.get_print(
File “apps/frappe/frappe/init.py”, line 2041, in get_print
return get_pdf(html, options=pdf_options, output=output)
File “apps/frappe/frappe/utils/pdf.py”, line 38, in get_pdf
filedata = pdfkit.from_string(html, options=options or {}, verbose=True)
File “env/lib/python3.10/site-packages/pdfkit/api.py”, line 75, in from_string
return r.to_pdf(output_path)
File “env/lib/python3.10/site-packages/pdfkit/pdfkit.py”, line 202, in to_pdf
File “env/lib/python3.10/site-packages/pdfkit/pdfkit.py”, line 156, in handle_error
OSError: wkhtmltopdf reported an error:
Exit with code 1 due to network error: ConnectionRefusedError

Hi,

I’m not well versed in Oauth integrations, so this may not be much help. Could you post the output of wkhtmltopdf -V and openssl version ? The site-config.json and /etc/hosts might be of interest as well.

1 Like