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.
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
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
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
sudo apt update && sudo apt install -y gconf-service libgbm 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