
Audio Narration
wkhtmltopdf with Patched Qt: The Complete Developer's Guide
Introduction
PDF generation from HTML remains one of the core challenges in web development. In the PHP world, wkhtmltopdf stands as one of the most popular tools for this task — a command-line utility that uses the WebKit engine to render HTML into PDF. However, not all wkhtmltopdf versions are created equal. In this comprehensive article, we'll explore the critical differences between regular and "patched" versions, why the latter is essential for production applications, and how to properly install and configure it.
What is wkhtmltopdf?
wkhtmltopdf is an open-source command-line utility developed for converting HTML and CSS to PDF and various image formats. Created by Jakob Truelsen in 2008, it has since become the de-facto standard for server-side PDF generation in web applications.
Key advantages of wkhtmltopdf:
- Accurate rendering: Uses the WebKit engine, the same as in browsers
- CSS support: Full CSS support, including media queries
- JavaScript: Ability to execute JavaScript before PDF generation
- Headers and footers: Built-in header/footer support
- Stability: Time-tested solution
- Integration: Easy integration with PHP through libraries like SnappyPDF
The Problem: Regular vs Patched Qt
Regular (Unpatched) Version
When you install wkhtmltopdf through standard package managers (apt, yum, etc.), you get a version compiled against the system Qt library. This version has significant limitations:
1$ sudo apt-get install wkhtmltopdf
2$ wkhtmltopdf --version
3wkhtmltopdf 0.12.6
4
5$ wkhtmltopdf --print-media-type test.html test.pdf
6The switch --print-media-type, is not support using unpatched qt, and will be ignored.Limitations of the unpatched version:
- No print media query support — CSS
@media printrules are ignored - Landscape orientation issues — incorrect handling of landscape orientation
- Unstable header/footer — positioning and styling problems
- Limited CSS3 support — some modern CSS properties don't work
- Font issues — incorrect rendering of custom fonts
Patched Qt Version
The "patched" version of wkhtmltopdf comes with a specially modified Qt WebKit that includes numerous print-specific fixes:
1$ wkhtmltopdf --version
2wkhtmltopdf 0.12.6 (with patched qt)
3
4$ wkhtmltopdf --print-media-type test.html test.pdf
5Loading page (1/2)
6Printing pages (2/2)
7DoneAdvantages of the patched version:
- Full print media query support — correct handling of CSS for print
- Stable landscape orientation — flawless landscape orientation handling
- Reliable header/footer — proper positioning and repetition on each page
- Modern CSS support — support for CSS3, flexbox, grid
- Quality typography — correct font and kerning handling
- Precise positioning — pixel-perfect element positioning
Technical Differences
Anatomy of the Patches
The patches in Qt WebKit for wkhtmltopdf include hundreds of changes aimed at improving print functionality:
1// Example patch for print media query support
2// Original Qt WebKit
3bool WebPage::shouldApplyPrintMediaType() const
4{
5 return false; // Always returned false
6}
7
8// Patched version
9bool WebPage::shouldApplyPrintMediaType() const
10{
11 return m_printMediaTypeEnabled; // Considers settings
12}Key improvement areas:
- Media Query Engine: Redesigned media query processing mechanism
- Page Layout: Improved page layout algorithm
- Font Rendering: Optimized font rendering for print
- CSS Parser: Extended CSS property support for print
- Memory Management: Optimized memory management for large documents
Installing the Patched Version
Ubuntu/Debian
1# Remove old version
2sudo apt-get remove wkhtmltopdf
3
4# Download patched version
5wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-3/wkhtmltox_0.12.6.1-3.jammy_amd64.deb
6
7# Install dependencies
8sudo apt-get update
9sudo apt-get install -y libfontconfig1 libfreetype6 libx11-6 libxext6 libxrender1
10
11# Install package
12sudo dpkg -i wkhtmltox_0.12.6.1-3.jammy_amd64.deb
13sudo apt-get install -fCentOS/RHEL
1# Remove old version
2sudo yum remove wkhtmltopdf
3
4# Download and install
5wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-3/wkhtmltox-0.12.6.1-3.centos8.x86_64.rpm
6sudo yum localinstall wkhtmltox-0.12.6.1-3.centos8.x86_64.rpmmacOS
1# With Homebrew
2brew install --cask wkhtmltopdf
3
4# Or download from official site
5wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-3/wkhtmltox-0.12.6.1-3.macos-cocoa.pkgDocker (Recommended for production)
1FROM ubuntu:20.04
2
3RUN apt-get update && apt-get install -y \
4 wget fontconfig libfontconfig1 libfreetype6 \
5 libx11-6 libxext6 libxrender1 xvfb
6
7RUN wget -O wkhtmltox.deb \
8 https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-3/wkhtmltox_0.12.6.1-3.focal_amd64.deb && \
9 dpkg -i wkhtmltox.deb && \
10 rm wkhtmltox.deb
11
12# Headless wrapper
13RUN echo '#!/bin/bash\nxvfb-run -a wkhtmltopdf "$@"' > /usr/local/bin/wkhtmltopdf-headless && \
14 chmod +x /usr/local/bin/wkhtmltopdf-headlessPractical Examples
CSS for print with patched version
1/* Basic print styles */
2@page {
3 size: A4 portrait;
4 margin: 2cm 1.5cm;
5}
6
7@page:first {
8 margin-top: 5cm; /* More space for header */Advanced capabilities
1<!DOCTYPE html>
2<html>
3<head>
4 <meta charset="utf-8">
5 <title>Advanced PDF Report</title>
6 <style>
7 @page {
8 size: A4 landscape;PHP Integration
SnappyPDF
1// composer require knplabs/knp-snappy
2
3use Knp\Snappy\Pdf;
4
5$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
6
7// Settings for patched version
8$snappy->setOptions([Laravel DomPDF vs SnappyPDF
1// Performance and quality comparison
2
3// DomPDF - pure PHP, limited CSS
4$pdf = PDF::loadView('report', $data);
5// ❌ No CSS Grid/Flexbox support
6// ❌ Limited CSS3 support
7// ❌ Complex layout issues
8
9// SnappyPDF with patched wkhtmltopdf
10$pdf = SnappyPdf::loadHTML($html)
11 ->setOption('print-media-type', true)
12 ->setOrientation('landscape');
13// ✅ Full modern CSS support
14// ✅ Accurate browser-like rendering
15// ✅ Excellent performanceInstallation Automation
Ansible Playbook
1---
2- name: Install wkhtmltopdf with patched Qt
3 hosts: webservers
4 become: yes
5
6 vars:
7 wkhtmltopdf_version: "0.12.6.1-3"
8 wkhtmltopdf_package: "wkhtmltox_{{ wkhtmltopdf_version }}.focal_amd64.deb"Monitoring and Diagnostics
Runtime version checking
1class WkhtmltopdfChecker
2{
3 public static function checkVersion()
4 {
5 $version = shell_exec('wkhtmltopdf --version 2>&1');
6
7 return [
8 'version' => trim($version),Logging and debugging
1class PdfGenerator
2{
3 private function generatePdf($html, $options = [])
4 {
5 $start = microtime(true);
6
7 Log::info('PDF generation started', [
8 'options' => $options,Performance Optimization
Caching
1class CachedPdfGenerator
2{
3 private $cache;
4 private $snappy;
5
6 public function __construct(Cache $cache, Pdf $snappy)
7 {
8 $this->cache = $cache;Queue jobs
1// Job for asynchronous PDF generation
2class GeneratePdfJob implements ShouldQueue
3{
4 private $reportId;
5 private $template;
6 private $data;
7
8 public function handle(PdfGenerator $generator)Alternatives and Comparison
Modern alternatives
Puppeteer/Chrome Headless
1const puppeteer = require('puppeteer');
2
3const browser = await puppeteer.launch();
4const page = await browser.newPage();
5await page.setContent(html);
6const pdf = await page.pdf({
7 format: 'A4',
8 landscape: true,
9 printBackground: true
10});Playwright
1use Nesk\Puphpeteer\Puppeteer;
2
3$puppeteer = new Puppeteer;
4$browser = $puppeteer->launch();
5$page = $browser->newPage();
6$page->setContent($html);
7$pdf = $page->pdf(['format' => 'A4']);WeasyPrint (Python-based, probably micro service or docker)
1import weasyprint
2
3html = weasyprint.HTML(string=html_string)
4pdf = html.write_pdf()Comparison table
| Solution | CSS Support | Performance | Memory Usage | Maintenance |
|---|---|---|---|---|
| wkhtmltopdf (patched) | ★★★★☆ | ★★★★★ | ★★★★☆ | ★★★☆☆ |
| Puppeteer | ★★★★★ | ★★★☆☆ | ★★☆☆☆ | ★★★★☆ |
| DomPDF | ★★☆☆☆ | ★★★★★ | ★★★★★ | ★★★★★ |
| WeasyPrint | ★★★★☆ | ★★★☆☆ | ★★★☆☆ | ★★★☆☆ |
Conclusion
wkhtmltopdf with patched Qt remains one of the best solutions for server-side PDF generation, especially in PHP applications. Despite the emergence of modern alternatives like Puppeteer, the patched version of wkhtmltopdf provides:
- Stability: Years-tested solution
- Performance: Optimized resource consumption
- Compatibility: Excellent integration with existing PHP applications
- Functionality: Full support for modern web standards
Key takeaways:
- Always use the patched version for production applications
- Plan installation in advance — the patched version requires manual installation
- Test print media queries — this is the main indicator of correct functionality
- Monitor version at runtime — add checks to your CI/CD pipeline
- Consider Docker for consistency across environments
A properly configured wkhtmltopdf with patched Qt will provide your application with high-quality PDF document generation with minimal overhead and maximum stability.
🎁 Bonus Script: One-Command wkhtmltopdf Installation
Made it this far? Here's your reward - a universal installer that handles everything automatically:
1#!/bin/bash
2
3# =============================================================================
4# wkhtmltopdf Patched Qt Universal Installer
5# =============================================================================
6#
7# Universal script for installing wkhtmltopdf with patched Qt
8# Supports: Ubuntu 18.04+, Debian 9+, CentOS 7+, Amazon Linux 2
