## Use Cases
- **Invoices and receipts**: your app generates HTML, converts to PDF for the customer
- **Dynamic reports**: dashboards exported as PDF
- **Certificates**: diplomas and certificates generated automatically
- **Documentation**: HTML guides converted to PDF for download
## Option 1: wkhtmltopdf (C++, Command Line)
```bash
# Install
sudo apt install wkhtmltopdf # Ubuntu/Debian
brew install wkhtmltopdf # macOS
# URL to PDF
wkhtmltopdf https://example.com/invoice/123 invoice.pdf
# Local HTML file to PDF
wkhtmltopdf page.html document.pdf
# With page options
wkhtmltopdf --page-size A4 --margin-top 20mm --margin-bottom 20mm page.html doc.pdf
# With custom header and footer
wkhtmltopdf --header-html header.html --footer-html footer.html page.html doc.pdf
```
**Advantages**: fast, no Node.js, works without a full browser.
**Limitations**: old WebKit engine, limited modern JavaScript support.
## Option 2: Puppeteer (Node.js, Headless Chrome)
```bash
npm install puppeteer
```
```javascript
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com/invoice/123', { waitUntil: 'networkidle0' });
await page.pdf({
path: 'invoice.pdf',
format: 'A4',
margin: { top: '20mm', right: '15mm', bottom: '20mm', left: '15mm' },
printBackground: true
});
await browser.close();
})();
```
**Advantages**: identical Chrome rendering, full modern CSS and JavaScript, web fonts.
**Limitations**: downloads Chromium (~300 MB), slower on headless servers.
## Option 3: PHP with DomPDF / Laravel
```bash
composer require barryvdh/laravel-dompdf
```
```php
use Barryvdh\DomPDF\Facade\Pdf;
return Pdf::loadView('invoices.template', ['data' => $data])->download('invoice.pdf');
```
## Tool Comparison
| Tool | Speed | CSS Fidelity | JavaScript | Requires |
|------|-------|-------------|------------|----------|
| wkhtmltopdf | Fast | Good | Partial | C++ libs |
| Puppeteer | Slow | Excellent | Full | Node + Chrome |
| Playwright | Slow | Excellent | Full | Node + Browser |
| DomPDF (PHP) | Medium | Medium | No | PHP |
## Tips for Quality PDFs from HTML
- Use `@media print` in CSS to adjust styles for print
- Use `@page` CSS to control page margins
- Use `page-break-before: always` to force page breaks
- Avoid `position: fixed` — causes pagination issues
- Use absolute URLs for images so the converter can find them
- Embed fonts with `@font-face` or use system fonts
Guide