wkhtmltopdf (and its sibling wkhtmltoimage) are often used to generate pdf files or images from html. However when you install wkhtmltopdf on Amazon Linux you can run into a small issue which is not that easy to fix, especially if you don’t know much about Amazon Linux.
Normally you would install this package as any other rpm file, you download the source from their website and run yum install. However in this case, at least with Amazon Linux 2018 you get the following error message:
wkhtmltopdf: error while loading shared libraries: libpng15.so.15: cannot open shared object file: No such file or directory
Now that’s a bit annoying. I have spent like 2-3 hours trying to resolve this issue and after much trial & error in the end I have succeeded.
- First you need to install wkhtmlpdf
- then you need to get the libpng library and build it from source. However if you don’t have the Linux development tools installed the you need to install them as well.
- After you built the library from source you need to install libtiff as well.
Below you can find the full list of commands I’ve used to fix this. Please keep in mind that the lines that begin with # are just comments.
#!/bin/bash
#step 1 - download and install wkhtmltopdf
wget https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox-0.12.5-1.centos7.x86_64.rpm
sudo yum install -y wkhtmltox-0.12.5-1.centos7.x86_64.rpm
#step 2 - download the libpng15 library and build it from source
wget https://sourceforge.net/projects/libpng/files/libpng15/older-releases/1.5.15/libpng-1.5.15.tar.gz/download -O libpng-1.5.15.tar.gz
tar -zxvf libpng-1.5.15.tar.gz
cd libpng-1.5.15
#step 3 - only needed if you don't have the tools installed and you get the error:
# "configure: error: no acceptable C compiler found in $PATH"
sudo yum groupinstall -y "Development Tools"
./configure --prefix=/usr
sudo make install
#step 4 - installing libtiff
sudo yum install -y libtiff
So that’s it. After you completed the steps above you should be able to run wkhtmltopdf and wkhtmltoimage.
Generating PDF files and images is pretty easy. This is how you generate a PDF:
wkhtmltopdf https://majestic.cloud majestic.pdf
And this is how you generate an image:
wkhtmltoimage --width 1280 --height 1600 --javascript-delay 10000 --enable-plugins https://majestic.cloud majestic.png
I hope you found this post useful. If you did you can check out other posts or you can head over to our Youtube channel where you can find lots of useful videos about cloud technologies.
Thank you!