Haven worked on projects where I converted HTML/CSS to pdf, I have come to learn a few things. First, I must say that Puppeteer in my opinion is still the best converter from the server-side of things.
So, in this article, I will be discussing how to install Puppeteer and mitigate errors that are likely to arise.
Scenario:
My project was a Laravel/PHP project which communicated with a NodeJS server through a socket within the same localhost. So PHP sends the JSON data to the NodeJS, then NodeJS converts the data with a predefined HTML template into pdf, saves the file, and writes the result(file) back to PHP through the socket. Php on receiving the pdf file returns the result as a downloadable file to the browser.
In this way, I have achieved total separation of concern, highly reusable, and printed out a very clear pdf.
The project was hosted on an AWS EC2/ubuntu instance.
How to install Puppeteer on an Ubuntu instance (18.04 and 20.04)
- Go to your project directory
- Install NodeJS/npm if you haven't
- Run:
sudo npm install -g puppeteer --unsafe-perm=true -allow-root && sudo apt install chromium-browser -y
Errors you are likely to face
ubuntu 20.04/18.04 Oh error: Error: Failed to launch the browser process! error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory.
Note: You might see this same error for libnss3.so, libgbm.so, libxss1.so or libasound2.so missing packages
Solution:
You can install these packages singly like
- sudo apt install libnss3-dev
- sudo apt install libgbm-dev and so on
But here is a complete install
Run:
sudo apt update && sudo apt install -y gconf-service libgbm-dev libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget
This will install all dependencies that might be missing.
If however, you encounter any challenges along the line, you could reach out, I could be of help. Thank you.
Links that were of help to me
- https://techoverflow.net/2018/06/05/how-to-fix-puppetteer-error-while-loading-shared-libraries-libx11-xcb-so-1-cannot-open-shared-object-file-no-such-file-or-directory/
- https://zhang0peter.com/2020/02/07/linux-chrome-bug/
- https://github.com/actions/virtual-environments/issues/732
- https://github.com/puppeteer/puppeteer/issues/807
- https://github.com/alixaxel/chrome-aws-lambda/issues/93
Top comments (5)
Seems to work, though
libgbm
needs to be changed tolibgbm-dev
ok thanks, will check that out
Thanks! This solution save me a lot of time. It worked on my ubuntu ec2 instance.
thanks! this solution worked on my ubuntu ec2 instance. For
https://www.npmjs.com/package/node-html-to-image
this package.Couldn't get it to work for some reason same error I made sure to install it all and in the right place so I'm not really sure what it is...