How to Install WKHTMLtoPDF on Ubuntu (Step-by-Step Guide)
Source: Dev.to
What is WKHTMLtoPDF?
WKHTMLtoPDF is a command‑line tool that converts HTML pages into PDF documents. It’s widely used in:
- Laravel applications
- PHP projects
- Node.js back‑ends
- Server‑side PDF generation
- Automated reporting systems
You can download the official build from the WKHTMLtoPDF website.
Why Use WKHTMLtoPDF on Ubuntu?
Ubuntu servers are commonly used for web hosting and backend applications. Installing WKHTMLtoPDF on Ubuntu enables you to:
- Generate invoices automatically
- Create reports in PDF format
- Convert dynamic web pages to downloadable PDFs
- Automate document workflows
Installation Steps
Step 1: Update Your Ubuntu System
sudo apt update && sudo apt upgrade -y
This ensures all dependencies are up to date.
Step 2: Install Required Dependencies
sudo apt install -y fontconfig libxrender1 libxext6 xfonts-base xfonts-75dpi
These libraries ensure proper font rendering and display support.
Step 3: Download WKHTMLtoPDF for Ubuntu
It’s recommended to install the official .deb package instead of using the default Ubuntu repository (as it may be outdated).
wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb
sudo dpkg -i wkhtmltox_0.12.6-1.focal_amd64.deb
If you encounter dependency errors, fix them with:
sudo apt --fix-broken install
Verify the installation:
wkhtmltopdf --version
You should see the installed version number.
Step 4: Test HTML to PDF Conversion
wkhtmltopdf https://example.com output.pdf
If the PDF generates successfully, the installation is complete.
Common Installation Errors & Fixes
-
WKHTMLtoPDF not found
which wkhtmltopdfIf the command returns no path, reinstall the package.
-
Missing shared libraries
sudo apt install -fThis installs any missing dependencies automatically.
-
Fonts not rendering properly
sudo apt install ttf-mscorefonts-installer
Best Practices for Production Servers
- Always use the official stable release.
- Prefer the
.debpackage over snap installations for better stability. - Keep the required dependencies up to date.
- For Laravel projects, consider integrating with
barryvdh/laravel-snappyfor easy PDF generation from Blade templates.
Final Thoughts
Installing WKHTMLtoPDF on Ubuntu is straightforward when you follow the correct steps and install the proper dependencies. Whether you’re building automated reports, invoices, or dynamic PDF systems, WKHTMLtoPDF remains a reliable solution for server‑side HTML‑to‑PDF conversion.