ERPNext Server Error: PDF invoice Error

Hello,
I have a self-installed erpNext 14

when I generate an invoice, it shows proper data and everything is good
but when I try getting PDF, I face server an error.


Traceback (most recent call last):
  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 201, in to_pdf
    self.handle_error(exit_code, stderr)
  File "env/lib/python3.10/site-packages/pdfkit/pdfkit.py", line 155, in handle_error
    raise IOError('wkhtmltopdf reported an error:\n' + stderr)
OSError: wkhtmltopdf reported an error:
The switch --print-media-type, is not support using unpatched qt, and will be ignored.The switch --header-html, is not support using unpatched qt, and will be ignored.The switch --footer-html, is not support using unpatched qt, and will be ignored.The switch --disable-smart-shrinking, is not support using unpatched qt, and will be ignored.QStandardPaths: XDG_RUNTIME_DIR not set, defaulting to '/tmp/runtime-frappe'
Exit with code 1 due to network error: ContentNotFoundError


During handling of the above exception, another exception occurred:

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 45, in handle
    data = execute_cmd(cmd)
  File "apps/frappe/frappe/handler.py", line 83, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "apps/frappe/frappe/__init__.py", line 1607, in call
    return fn(*args, **newargs)
  File "apps/frappe/frappe/utils/print_format.py", line 129, in download_pdf
    pdf_file = frappe.get_print(
  File "apps/frappe/frappe/__init__.py", line 2040, in get_print
    return get_pdf(html, options=pdf_options, output=output)
  File "apps/frappe/frappe/utils/pdf.py", line 46, in get_pdf
    frappe.throw(_("PDF generation failed because of broken image links"))
  File "apps/frappe/frappe/__init__.py", line 525, in throw
    msgprint(
  File "apps/frappe/frappe/__init__.py", line 493, in msgprint
    _raise_exception()
  File "apps/frappe/frappe/__init__.py", line 442, in _raise_exception
    raise raise_exception(msg)
frappe.exceptions.ValidationError: PDF generation failed because of broken image links

can anyone advise?
Thank you
Ali

It seems like you don’t have the right version of wkhtmltopdf.
What is the output of

wkhtmltopdf --version

(on your server, of course) ?

I was able to solve this by taking the following steps.
1: wkhtmltopdf -V (wkhtmltopdf 0.12.6.1) which was not qt patched.
so I removed it and installed a patched version.

sudo apt-get remove --purge wkhtmltopdf
sudo apt-get autoremove

then downloaded the latest from wkhtmltopdf for my Ubuntu 22.04 (jammy). and installed

wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.jammy_amd64.deb
sudo dpkg -i wkhtmltox_0.12.6.1-2.jammy_amd64.deb
sudo cp /usr/local/bin/wkhtmltopdf /usr/bin

But that did not solve the issue and I was still getting
“PDF generation failed because of broken image links”

and the problem was the hostname, (very tricky)
I was using a domain name to access ERPNExt which actually resolves to a different IP and I was using “hosts” File to point the domain manually to certain IP.
I am assuming that the pdf generation mechanism uses URI to access some images, the server was accessing the read domain name and files were not found.
so I just made an entry in the server /etc/hosts and pointed the domain to the server IP. like

127.0.0.1 erp.mydomain.com

and all PDF generation started working.

and I think if I just add the hosts entry without updating the wkhtmltopdf, it will still work. (not sure)

Thanks
Ali

1 Like