PDF Generation Slow on Ubuntu

I actually have been living with this for awhile but realized that it is just getting unbearably slower over time. It takes approximately 30s to generate any PDFs for any documents including Sales Orders, Invoices etc. I run ERPNext on Ubuntu and based on a simple version check have wkhtmltopdf 0.12.2.1 (with patched qt).

demo.erpnext.com generated PDFs in 1 or 2 seconds. Anyone encountered similar problems?

I am also on Ubuntu. Are you on a vm?

nope, manual installation. What are your PDF generation times like? say for a Sales Order

I am in a vm environment (Vmware, 1 CPU, 1GB ram only) and creating a pdf takes 3 seconds top.

That’s pretty quick for such a small machine. Does anyone know what could possibly be the problem?

Did you remove wkhtmltopdf and reinstall ?

@bohlian How much RAM does the machine have?

4GB, I’ve also tried increasing to 8GB but it doesn’t help.

@anand I’ve narrowed down to the following line of code which runs very slowly

pdfkit.from_string(html, fname, options=options or {})

This is found in frappe/pdf.py at 69fac8f5e986d86e9ae6a3e4233ae3f5a57058c6 · frappe/frappe · GitHub

I find it quite strange, cause I’ve even tried to push the HTML output to a file then created a standalone python file and within this app I pull the contents of the HTML output into a string then ran the same pdfkit.from_string command and it seems to run fast. Although the output is not as nicely formatted as if I had generated from ERPNEXT.

Just some more information, even doing this is slow:

pdfkit.from_string("<html></html>", fname, options=options or {})

Look what I just found…High CPU usage just to generate 1 PDF

:frowning: sad to see. did you remove wkhtmltopdf, update the system then install it again ?

dont think that would help to be honest. Can you help me with something, can you share your output when running “wkhtmltopdf --version” and would you mid sharing whether you are using 32 or 64 bit for ubuntu?

Are you running it on a desktop with a GPU or on a server?

Thanks.

@bohlian maybe you should ask on wkhtmltopdf forums or search for such an issue.

@ferohers thanks for sharing

@rmehta yea, I too think thats the best way forward … have been trying to search for similar problems for days no to avail, will post an issue on the wkhtmltopdf forum and will share the results once I get them

I finally managed to crack this problem.

I cannot believe I missed this all this while, for Ubuntu there is reduced functionality and some dependencies need to be installed. I think perhaps one or two were missing or misbehaving. I reinstalled them and its back to normal!

Just run the following:

sudo apt-get install -y openssl build-essential xorg libssl-dev

I find it quite strange that it would behave this way especially since I used the installation script provided to install ERPNext. Anyhow, for anyone who has problems with Ubuntu PDF generation performance you can try this.

5 Likes

@pdvyas

I once this issue, used strace (tip from the maintainer, wkhtmltopdf takes a lot of time if HOME env var is not set · Issue #2024 · wkhtmltopdf/wkhtmltopdf · GitHub). Turned out that the font-cache wasn’t up to date and as $HOME is not available, it would not cache it in $HOME either.

A quick try would be run sudo fc-cache and then try again

@bohlian Reinstalling would have fired fc-cache as a post install script

2 Likes

@pdvyas thanks for the explanation. Will try your suggestion if I ever hit this problem again.

The link you provided clarifies alot.

Just for my information, may I know from your experience why would an unset $HOME cause the font cache to not be available? or cause the system to want to re-cache the fonts?

We had the same problem whereby PDF generation was taking longer and longer by the day. It got to the point that it was taking more than 30 seconds just to create a single PDF!

bohlian seems to have the perfect solution (for Ubuntu Server 14.04 at least)

SSH onto your Server and run the commands as suggested:

sudo apt-get install -y openssl build-essential xorg libssl-dev

bohlian - Thank you for sharing your awesome find!

1 Like