Optional module · pdftoolkit/html-to-pdf 0.4.0
Three original product illustrations sit beside descriptions in a fixed two-column table. Image proportions, cell padding and wrapped text remain visible on one page.
The oracle checks all source text, three embedded images and three Figure elements with alternative descriptions. These geometric illustrations are original MIT assets.
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="en">
<head>
<meta charset="utf-8">
<title>Everyday objects — illustrated catalogue</title>
<style>
body { font-size: 11pt; line-height: 1.45; color: #243746; }
h1 { font-size: 29pt; color: #176a80; margin: 0 0 10pt; }
h2 { font-size: 17pt; margin: 6pt 0 9pt; }
p { margin: 0 0 9pt; }
.intro { font-size: 13pt; margin-bottom: 22pt; }
table { table-layout: fixed; width: 100%; margin-bottom: 20pt; }
td { width: 50%; vertical-align: top; padding: 10pt; }
img { width: 100%; }
.price { font-weight: bold; font-size: 15pt; color: #176a80; }
.footer { font-size: 9pt; color: #526674; margin-top: 12pt; }
</style>
</head>
<body>
<h1>Everyday objects</h1>
<p class="intro">Three useful things, thoughtfully made. An illustrated catalogue
converted directly from HTML with native PHP.</p>
<table><tbody>
<tr><td><img src="notebook.png" alt="Teal notebook with a cream binding"></td><td><h2>The notebook</h2><p>Ivory pages, a sturdy cover and room for your next idea. The image keeps its original proportions.</p><p class="price">15,00 €</p></td></tr>
<tr><td><img src="lamp.png" alt="Amber desk lamp with a broad shade"></td><td><h2>The desk lamp</h2><p>A warm colour for a calm workspace. Text wraps inside its column without overlapping the illustration.</p><p class="price">74,00 €</p></td></tr>
<tr><td><img src="mug.png" alt="Green ceramic mug with a curved handle"></td><td><h2>The ceramic mug</h2><p>A comfortable handle and a generous shape. Each image has an alternative description in the PDF structure.</p><p class="price">12,00 €</p></td></tr>
</tbody></table>
<p class="footer">Original demonstration illustrations · No browser is required to generate this PDF.</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");
