Complete PHP examples for common HTML to Image 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 PNG result. Stream examples use a file stream as their destination.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); $client->setOutputFormat('png'); $client->convertUrlToFile('https://example.com/', 'example.png');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); $client->setOutputFormat('png'); $image = $client->convertUrl('https://example.com/'); // binary string echo strlen($image), PHP_EOL;
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); $output_stream = fopen('example.png', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->setOutputFormat('png'); $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\HtmlToImageClient('demo', 'demo'); $client->setOutputFormat('png'); $client->convertFileToFile('document.html', 'document.png');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); $client->setOutputFormat('png'); $image = $client->convertFile('document.html'); // binary string echo strlen($image), PHP_EOL;
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); $output_stream = fopen('document.png', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->setOutputFormat('png'); $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\HtmlToImageClient('demo', 'demo'); $html = file_get_contents('document.html'); if ($html === false) { throw new \RuntimeException('Cannot read HTML input'); } $client->setOutputFormat('png'); $client->convertStringToFile($html, 'html.png');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); $html = file_get_contents('document.html'); if ($html === false) { throw new \RuntimeException('Cannot read HTML input'); } $client->setOutputFormat('png'); $image = $client->convertString($html); // binary string echo strlen($image), PHP_EOL;
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); $output_stream = fopen('html.png', '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->setOutputFormat('png'); $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\HtmlToImageClient('demo', 'demo'); $html = ' <!doctype html> <html><body><h1>Monthly report</h1></body></html>'; $client->setOutputFormat('png'); $client->convertStringToFile($html, 'report.png');
Open document.html as a binary input stream and save the PNG to document.png.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); $input_stream = fopen('document.html', 'rb'); if ($input_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->setOutputFormat('png'); $client->convertStreamToFile($input_stream, 'document.png'); } 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\HtmlToImageClient('demo', 'demo'); $client->setOutputFormat('png'); $client->convertFileToFile('document.zip', 'output.png');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); $client->setOutputFormat('png'); $client->setZipMainFilename('index.html'); $client->convertFileToFile('document.zip', 'document.png');
Create document.zip from the existing document directory, then convert it to PNG.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); // Requires the Phar extension. (new PharData('document.zip'))->buildFromDirectory('document'); $client->setOutputFormat('png'); $client->convertFileToFile('document.zip', 'document.png');
Capture a 1280-pixel-wide viewport and scale the PNG output to 50%.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); $client->setOutputFormat('png'); $client->setScaleFactor(50); $client->setScreenshotWidth(1280); $client->convertUrlToFile('https://example.com/', 'customized.png');
Replace the selectors with elements from your page.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); $client->setCustomCss(' /* Hide navigation, ads, and sidebar */ nav, .advertisement, .sidebar, footer { display: none !important; } /* Adjust layout for screenshot */ .content { width: 100% !important; max-width: none !important; }'); $client->setOutputFormat('png'); $client->convertUrlToFile('https://example.com/', 'custom_css.png');
This example removes widgets and expands sections by changing their CSS classes. Adjust the selectors and class names for your page.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\HtmlToImageClient('demo', 'demo'); $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->setOutputFormat('png'); $client->convertUrlToFile('https://example.com/', 'custom_js.png');
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\HtmlToImageClient('demo', 'demo'); $client->setElementToConvert('#main'); $client->setOutputFormat('png'); $client->convertFileToFile('document.html', 'html_part.png');
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\HtmlToImageClient('demo', 'demo'); $client->setOutputFormat('png'); $client->setWaitForElement('#content-loaded'); $client->convertFileToFile('document.html', 'wait_element.png');
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\HtmlToImageClient('demo', 'demo'); $client->setJavascriptDelay(2000); $client->setOutputFormat('png'); $client->convertUrlToFile('https://example.com/', 'with_delay.png');
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\HtmlToImageClient('demo', 'demo'); $client->setOutputFormat('png'); $client->setUsePrintMedia(true); $client->convertUrlToFile('https://example.com/', 'print_media.png');
Add these fragments to HTML you control.
<nav class="pdfcrowd-remove">This won't appear in the screenshot</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.
<style> /* These styles only apply during image 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\HtmlToImageClient('demo', 'demo'); $client->setDataFormat('json'); $client->setOutputFormat('png'); $client->setDataFile('data.json'); $client->convertFileToFile('template.html', 'output.png');