Complete PHP examples for common HTML to PDF tasks. See the PHP SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each PHP example can be copied and run independently.
Choose file, in-memory, or output-stream handling for the result. Stream examples use a file stream as their destination.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->convertUrlToFile('https://example.com/', 'example.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $pdf = $client->convertUrl('https://example.com/'); // binary string echo strlen($pdf), PHP_EOL;
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $output_stream = fopen('example.pdf', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->setContentViewportWidth('balanced'); $client->convertUrlToStream('https://example.com/', $output_stream); if (!fflush($output_stream)) { throw new \RuntimeException('Cannot flush output stream'); } } finally { fclose($output_stream); }
Upload an existing document.html file. For local images and stylesheets, see Local assets.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->convertFileToFile('document.html', 'document.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $pdf = $client->convertFile('document.html'); // binary string echo strlen($pdf), PHP_EOL;
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $output_stream = fopen('document.pdf', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->setContentViewportWidth('balanced'); $client->convertFileToStream('document.html', $output_stream); if (!fflush($output_stream)) { throw new \RuntimeException('Cannot flush output stream'); } } finally { fclose($output_stream); }
These examples read HTML text from document.html. In an application, pass the HTML produced by your template renderer instead.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $html = file_get_contents('document.html'); if ($html === false) { throw new \RuntimeException('Cannot read HTML input'); } $client->setContentViewportWidth('balanced'); $client->convertStringToFile($html, 'html.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $html = file_get_contents('document.html'); if ($html === false) { throw new \RuntimeException('Cannot read HTML input'); } $client->setContentViewportWidth('balanced'); $pdf = $client->convertString($html); // binary string echo strlen($pdf), PHP_EOL;
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $output_stream = fopen('html.pdf', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $html = file_get_contents('document.html'); if ($html === false) { throw new \RuntimeException('Cannot read HTML input'); } $client->setContentViewportWidth('balanced'); $client->convertStringToStream($html, $output_stream); if (!fflush($output_stream)) { throw new \RuntimeException('Cannot flush output stream'); } } finally { fclose($output_stream); }
Define a small HTML document directly in your code.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $html = ' <!doctype html> <html><body><h1>Monthly report</h1></body></html>'; $client->setContentViewportWidth('balanced'); $client->convertStringToFile($html, 'report.pdf');
Open document.html as a binary input stream and save the PDF to document.pdf.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $input_stream = fopen('document.html', 'rb'); if ($input_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->setContentViewportWidth('balanced'); $client->convertStreamToFile($input_stream, 'document.pdf'); } finally { fclose($input_stream); }
Include HTML and its local images, stylesheets, and scripts in a ZIP archive, keeping their relative paths.
Upload an existing document.zip. Select the main HTML file explicitly when the archive contains multiple documents.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->convertFileToFile('document.zip', 'output.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->setZipMainFilename('index.html'); $client->convertFileToFile('document.zip', 'document.pdf');
Create document.zip from the existing document directory, then convert it to PDF.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); // Requires the Phar extension. (new PharData('document.zip'))->buildFromDirectory('document'); $client->setContentViewportWidth('balanced'); $client->convertFileToFile('document.zip', 'document.pdf');
Package the existing document directory in memory. Its index.html file can refer to other files in the directory by relative path.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->setZipMainFilename('index.html'); $directory = realpath('document'); if ($directory === false) { throw new \RuntimeException('Cannot find the input directory'); } $archive = fopen('php://memory', 'w+b'); // in-memory stream resource if ($archive === false) { throw new \RuntimeException('Cannot open archive stream'); } try { $zip = new \ZipStream\ZipStream( outputStream: $archive, sendHttpHeaders: false ); $files = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS) ); foreach ($files as $file) { if ($file->isFile()) { $relative_path = substr($file->getPathname(), strlen($directory) + 1); $zip->addFileFromPath( str_replace(DIRECTORY_SEPARATOR, '/', $relative_path), $file->getPathname() ); } } $zip->finish(); if (!rewind($archive)) { throw new \RuntimeException('Cannot rewind archive stream'); } $client->convertStreamToFile($archive, 'document.pdf'); } finally { fclose($archive); }
This example uses PHP 8.2 or newer and ZipStream-PHP. Install the archive library and its mbstring polyfill with composer require maennchen/zipstream-php:3.1.2 symfony/polyfill-mbstring:1.33.0.
Use landscape Letter pages with half-inch margins.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->setMarginBottom('0.5in'); $client->setMarginLeft('0.5in'); $client->setMarginRight('0.5in'); $client->setMarginTop('0.5in'); $client->setOrientation('landscape'); $client->setPageSize('Letter'); $client->convertUrlToFile('https://example.com/', 'page-layout.pdf');
Show the source URL in the header and “Page N of M” in the footer.
See the headers and footers guide for more layouts and placeholders.
Scale the HTML content to 300%.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setScaleFactor(300); $client->convertUrlToFile('https://example.com/', 'zoom_300.pdf');
The browser viewport controls responsive breakpoints independently of the PDF paper size.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('large'); $client->setNoMargins(true); $client->convertUrlToFile('https://example.com/', 'bootstrap.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('small'); $client->convertUrlToFile('https://example.com/', 'viewport_small.pdf');
Replace the selectors with elements from your page.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->setCustomCss(' /* Hide navigation, ads, and sidebar */ nav, .advertisement, .sidebar, footer { display: none !important; } /* Adjust layout for print */ .content { width: 100% !important; max-width: none !important; }'); $client->convertUrlToFile('https://example.com/', 'custom_css.pdf');
Adjust the selectors and class names for your page.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->setCustomJavascript(' // Remove dynamic content before conversion document.querySelectorAll(\'.video-player, .chat-widget\').forEach(el => { el.remove(); }); // Expand collapsed sections document.querySelectorAll(\'.collapsed\').forEach(el => { el.classList.remove(\'collapsed\'); });'); $client->convertUrlToFile('https://example.com/', 'custom_js.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setCustomJavascript(' el = document.createElement(\'h2\'); el.textContent = \'Hello from PDFCrowd API\'; el.style.color = \'red\'; el_before = document.getElementsByTagName(\'h1\')[0]; el_before.parentNode.insertBefore(el, el_before.nextSibling);'); $client->convertUrlToFile('https://example.com/', 'html_inject.pdf');
Convert only #main in an existing document.html file. Adjust the filename and selector for your document.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setElementToConvert('#main'); $client->convertFileToFile('document.html', 'html_part.pdf');
Wait for #content-loaded to appear in an existing document.html file. Choose an element that appears when your content is ready.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->setWaitForElement('#content-loaded'); $client->convertFileToFile('document.html', 'wait_element.pdf');
Add a delay of 2000 milliseconds. When the page has a reliable readiness marker, use Wait for a page element.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->setJavascriptDelay(2000); $client->convertUrlToFile('https://example.com/', 'with_delay.pdf');
Apply the page’s @media print rules. The page must provide print styles for this to change its appearance.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->setUsePrintMedia(true); $client->convertUrlToFile('https://example.com/', 'print_media.pdf');
Highlight element boxes to investigate spacing and layout.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setCustomJavascript(' libPdfcrowd.highlightHtmlElements({ backgroundColor: \'rgba(255, 191, 0, 0.1)\', borderColor: null });'); $client->convertUrlToFile('https://example.com/', 'highlight_background.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setCustomJavascript(' libPdfcrowd.highlightHtmlElements({ borderColor: \'orange\', backgroundColor: null, padding: \'4px\', margin: \'4px\' });'); $client->convertUrlToFile('https://example.com/', 'highlight_borders.pdf');
Add these fragments to HTML you control.
<nav class="pdfcrowd-remove">This won't appear in the PDF</nav> <div class="pdfcrowd-hide">This is invisible but takes up space</div>
pdfcrowd-remove removes the element from the layout. pdfcrowd-hide preserves its space.
<!-- Force a page break before the following section --> <div style="page-break-before: always"></div> <!-- Avoid breaking tables across pages --> <style> table, tr, td { page-break-inside: avoid; } </style>
Content taller than a page still needs to break.
<style> /* These styles only apply during PDF conversion */ .pdfcrowd-body h1 { font-size: 48px; } .pdfcrowd-body footer { display: none; } .pdfcrowd-body .main-content { width: 100%; } </style>
The .pdfcrowd-body prefix limits these rules to conversion; it does not change the page in a visitor’s browser.
Upload existing template.html and data.json files. See the template rendering guide for template syntax, data mapping, and other supported data formats.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->setDataFormat('json'); $client->setDataFile('data.json'); $client->convertFileToFile('template.html', 'output.pdf');
Use an existing form.html file containing HTML form controls.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setContentViewportWidth('balanced'); $client->setEnablePdfForms(true); $client->convertFileToFile('form.html', 'form.pdf');
See fillable PDF forms for supported controls and attributes.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setAuthor('PDFCrowd'); $client->setKeywords('PDFCrowd,demo'); $client->setSubject('Demo'); $client->setTitle('Hello World'); $client->convertUrlToFile('https://example.com/', 'with_metadata.pdf');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToPdfClient('demo', 'demo'); $client->setInitialZoomType('fit-page'); $client->setNoMargins(true); $client->setOrientation('landscape'); $client->setPageLayout('single-page'); $client->setPageMode('full-screen'); $client->convertUrlToFile('https://example.com/', 'slide_show.pdf');
These settings request a full-screen initial view with landscape pages. The PDF viewer controls whether it honors those preferences.