Optional module · pdftoolkit/html-to-pdf 0.4.0
A one-page French invoice with accented text, genuine bold faces, colored sections, a fixed three-column table, right-aligned amounts and a structured hyperlink.
The independent oracle checks the amount 480,00 €, source text order, embedded fonts, the three column headers and the URI annotation/OBJR. The illustration is fictitious and has no commercial value.
php -d memory_limit=512M examples/generate.php _build/examples
python tools/validate_pdf.py --directory _build/examples
This example illustrates the documented profile, not a complete CSS level or PDF/UA certification.
View the PDF result
This file was generated with the code in this documentation. All pages appear below; open the PDF to use its links and bookmarks.
Use the browser PDF viewer
This viewer depends on your browser. If the document does not appear, use “Open the PDF” or “Download”.
HTML source
Read the HTML source
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>Facture — Atelier des objets</title>
<style>
body { font-size: 11pt; line-height: 1.4; color: #243746; }
h1 { color: #176a80; font-size: 30pt; margin: 0 0 8pt; }
h2 { font-size: 15pt; margin: 20pt 0 8pt; }
p { margin: 0 0 9pt; }
.eyebrow { color: #176a80; font-size: 9pt; margin-bottom: 12pt; }
.address { background-color: #e7eff4; padding: 14pt; margin: 18pt 0; }
table { table-layout: fixed; width: 100%; margin: 12pt 0; }
th { background-color: #176a80; color: #ffffff; text-align: left; padding: 9pt; }
td { padding: 9pt; border: 0.5pt solid #c7d6df; }
.amount { text-align: right; }
.total { font-size: 18pt; font-weight: bold; color: #176a80; text-align: right; margin-top: 18pt; }
.note { font-size: 9pt; color: #526674; margin-top: 22pt; }
a { color: #176a80; }
</style>
</head>
<body>
<p class="eyebrow">ATELIER DES OBJETS · DOCUMENT DE DÉMONSTRATION</p>
<h1>Facture</h1>
<p><strong>N° F-2026-042</strong> · Émise le 2 novembre 2026</p>
<div class="address"><p><strong>Client : Maison du papier</strong></p><p>24, rue des Ateliers<br>75011 Paris · France</p></div>
<h2>Votre commande</h2>
<table>
<thead><tr><th scope="col">Article</th><th scope="col">Quantité</th><th scope="col">Montant HT</th></tr></thead>
<tbody>
<tr><td>Carnet relié — papier ivoire</td><td>12</td><td class="amount">180,00 €</td></tr>
<tr><td>Lampe de bureau en métal</td><td>2</td><td class="amount">148,00 €</td></tr>
<tr><td>Tasse en céramique émaillée</td><td>6</td><td class="amount">72,00 €</td></tr>
</tbody></table>
<p class="amount">Sous-total HT : 400,00 €</p>
<p class="amount">TVA 20 % : 80,00 €</p>
<p class="total">Total TTC : 480,00 €</p>
<h2>Merci pour votre confiance</h2>
<p>Le texte reste sélectionnable, les accents sont conservés et les colonnes
du tableau s’alignent sur une grille commune.</p>
<p class="note">Exemple fictif, sans valeur commerciale.<br>
Moteur PHP natif : <a href="https://github.com/PDFToolkit/html-to-pdf">PDFToolkit HTML to PDF</a>.</p>
</body>
</html>
Complete source code
Code from pdftoolkit/html-to-pdf · View the source at the documented commit ↗ · HTML source at the documented commit ↗
Read the commented script: generate.php
<?php
declare(strict_types=1);
use PDF\HtmlToPdf\ConversionOptions;
use PDF\HtmlToPdf\Converter;
use PDF\HtmlToPdf\Font\FontFace;
use PDF\HtmlToPdf\Font\FontSet;
use PDF\HtmlToPdf\HtmlSource;
use PDF\HtmlToPdf\Resource\ImageAsset;
use PDF\HtmlToPdf\Resource\ResourceCatalog;
require dirname(__DIR__) . '/vendor/autoload.php';
// This CLI explicitly owns file access. The library receives only immutable bytes.
$destination = $argv[1] ?? dirname(__DIR__) . '/_build/examples';
if (!is_dir($destination) && !mkdir($destination, 0777, true) && !is_dir($destination)) {
throw new RuntimeException('Cannot create the example output directory.');
}
$read = static function (string $path): string {
$bytes = file_get_contents($path);
if ($bytes === false) {
throw new RuntimeException('Cannot read ' . $path);
}
return $bytes;
};
$fonts = new FontSet(
regular: new FontFace($read(__DIR__ . '/assets/fonts/Lato-Regular.ttf')),
bold: new FontFace($read(__DIR__ . '/assets/fonts/Lato-Bold.ttf')),
italic: new FontFace($read(__DIR__ . '/assets/fonts/Lato-Italic.ttf')),
);
$images = [];
foreach (['notebook', 'lamp', 'mug'] as $name) {
$images[$name . '.png'] = new ImageAsset($read(__DIR__ . '/assets/images/' . $name . '.png'));
}
$options = new ConversionOptions(fonts: $fonts, resources: new ResourceCatalog(images: $images));
$converter = new Converter();
$manifest = ['php' => PHP_VERSION, 'engine' => 'native-php', 'documents' => []];
foreach (['invoice', 'catalogue', 'multipage', 'css1'] as $name) {
$started = hrtime(true);
$html = $read(__DIR__ . '/sources/' . $name . '.html');
$result = $converter->convert(new HtmlSource($html), $options);
file_put_contents($destination . '/' . $name . '.pdf', $result->pdf);
$manifest['documents'][$name] = [
'pages' => $result->pageCount,
'elapsedSeconds' => (hrtime(true) - $started) / 1e9,
'peakMemoryBytes' => memory_get_peak_usage(true),
'sourceSha256' => hash('sha256', $html),
'pdfSha256' => hash('sha256', $result->pdf),
'bytes' => strlen($result->pdf),
'diagnostics' => $result->diagnostics,
];
fwrite(STDOUT, $name . ': ' . $result->pageCount . ' page(s), ' . strlen($result->pdf) . " bytes\n");
}
file_put_contents($destination . '/manifest.json', json_encode($manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR) . "\n");
