Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDF generation fails: wkhtmltopdf ConnectionRefusedError #36018

Closed
redolencetech opened this issue Jul 6, 2023 · 3 comments
Closed

PDF generation fails: wkhtmltopdf ConnectionRefusedError #36018

redolencetech opened this issue Jul 6, 2023 · 3 comments
Labels

Comments

@redolencetech
Copy link

redolencetech commented Jul 6, 2023

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:
https://discuss.frappe.io/t/pdf-printing-issues/97832/3
https://discuss.frappe.io/t/wkhtmltopdf-error-creating-pdf/101926/8
https://discuss.frappe.io/t/oserror-wkhtmltopdf-reported-an-error-exit-with-code-1-due-to-network-error-sslhandshakefailederror/96029/1

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
@redolencetech redolencetech changed the title PDF generation of order types fails: wkhtmltopdf ConnectionRefusedError PDF generation fails: wkhtmltopdf ConnectionRefusedError Jul 6, 2023
@James3UK
Copy link

James3UK commented Jan 3, 2024

see also:
#35155
#35155 (comment)

wkhtmltopdf has been abandoned the last year and the repository is now archived.
Its author recommends WeasyPrint.

When setting to 127.0.0.1 I get the content not found error as here:
frappe/frappe#19701

@ankush
Copy link
Member

ankush commented Feb 21, 2024

Duplicate frappe/frappe#23958

possible fix: host_name should be public facing host name and not local IP.

@ankush ankush closed this as not planned Won't fix, can't repro, duplicate, stale Feb 21, 2024
@pgandert1963
Copy link

pgandert1963 commented Mar 2, 2024

I have the same issue.
Interactively the PDF generation is working very well.
But in the context of sending email the wkhtmltopdf is throwing a ConnectionRefusedError.
Also setting the host_name to the public visible url brings no result.
When setting the host_name to 127.0.0.1 then sending the mail with attachment is working and no error is thrown. But with host_name 127.0.0.1 the interactive Rendering of PDF is broken.
This is in my opinion a showstopper, because there is no remedy for this.
The backgroundscript should work on localhost, while the frontend can use the public visible url.

SOLUTION. When setting the host_name ONLY the url must be set. Never include http:// or https://.
Example: www.mydomain.tld instead of https://www.mydomain.tld
Now both usecases are working.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants