Error occurred when sending email

Hi fellows

I’m encountering an issue when sending an email of a purchase invoice. I checked the error log and noticed that error occurred during the process of converting HTML to PDF. If I just untick the option ‘Send Print as PDF’ in the printing setting then it sends the email perfectly.

Any help would be appreciated.

Error log:

Below is the printing setting.

ERPNext version
image

Do you have wkhtmltopdf package installed on your server? make sure you have that package.

follow this link, in one of the steps it tells you how to install wkhtmltopdf

Hi Botanium,
Thanks for the link, it’s really helpful. I checked the packages in the docker container and found that I got ‘wkhtmltox’ installed instead of ‘wkhtmltopdf’, tried to reinstall ‘wkhtmltopdf’ but it doesn’t allow me to do that.

1 Like

Manually reinstall:

1 Like

I also checked the package info of ‘wkhtmltox’, feel like it is ‘wkhtmltopdf’ but just got a different name?

1 Like

0.12.5 with patched Qt is recommended.

Remove existing installation:

sudo apt-get remove --purge wkhtmltopdf

and get the recommened version above

Hi Botanium, Thanks for helping. I think I better make it clearer.

I’m running ERPNext on Mac docker desktop for testing purposes, and when I run ‘wkhtmltopdf --version’ inside the docker container it returns ‘wkhtmltopdf 0.12.6 (with patched qt)’. With ERPNext V13, I think we should use wkhtmltopdf with version 0.12.6 as it is being explicitly defined in the Dockerfile(frappe_docker/build/bench/Dockerfile).

Also, I found a post talking about that we shouldn’t use a version less than 0.12.6, as it would cause issues like Ms-alignment of document. PDF , Footer not printed

So I think the problem here might not be related to the installation/version of ‘wkhmltopdf’, I hope my assumption makes sense to you.

1 Like

Can you create a simple test html page ( get the code from w3schools )

and try
wkhtmltopdf test.html test.pdf ( Run this on your erpnext user, not root )
It should create a test.pdf file if wkhtmltopdf installation is ok.

Hi, thanks for helping. I tried that and it worked perfectly, So the wkhtmltopdf should be installed with no issues. BTW, I tested it in the container ‘rockleeerp_erpnext-worker-default_1’.

And copied the pdf file to my local and it can be viewed as well.

below is the error log detail

2021-11-29 03:47:37,088 ERROR frappe Could not take error snapshot: characters_written
Site: mysite.localhost
Form Dict: {'name': 'SAL-ORD-2021-00001', 'cmd': 'frappe.email.doctype.email_queue.email_queue.send_now', 'doctype': 'Sales Order', 'format': 'Standard', 'style': None, 'doc': None, 'no_letterhead': False}
Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 66, in application
    response = frappe.api.handle()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/api.py", line 54, in handle
    return frappe.handler.handle()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 31, in handle
    data = execute_cmd(cmd)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 67, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1213, in call
    return fn(*args, **newargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/email/doctype/email_queue/email_queue.py", line 320, in send_now
    record.send()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/email/doctype/email_queue/email_queue.py", line 123, in send
    message = ctx.build_message(recipient.recipient)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/email/doctype/email_queue/email_queue.py", line 240, in build_message
    message = self.include_attachments(message)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/email/doctype/email_queue/email_queue.py", line 300, in include_attachments
    print_format_file = frappe.attach_print(**attachment)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1588, in attach_print
    content = get_print(doctype, name, **kwargs)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1554, in get_print
    return get_pdf(html, options=pdf_options, output=output)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/pdf.py", line 37, in get_pdf
    filedata = pdfkit.from_string(html, False, options=options or {})
  File "/home/frappe/frappe-bench/env/lib/python3.9/site-packages/pdfkit/api.py", line 72, in from_string
    return r.to_pdf(output_path)
  File "/home/frappe/frappe-bench/env/lib/python3.9/site-packages/pdfkit/pdfkit.py", line 156, in to_pdf
    raise IOError('wkhtmltopdf reported an error:\n' + stderr)
OSError: wkhtmltopdf reported an error:
Exit with code 1 due to network error: ConnectionRefusedError


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/error.py", line 36, in make_error_snapshot
    snapshot = get_snapshot(exception)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/utils/error.py", line 127, in get_snapshot
    value = pydoc.text.repr(getattr(evalue, name))
AttributeError: characters_written

For anyone else who encounters this issue, please refer here for the solution: Error occurred when sending email of a sales order with PDF print format setting · Issue #589 · frappe/frappe_docker · GitHub

1 Like