Complete PHP examples for common PDF to HTML 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 HTML result. Stream examples use a binary file stream as their destination.
The memory variants return UTF-8 bytes. Decode them when your application needs a text string.
Upload an existing logo.pdf file.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $client->convertFileToFile('logo.pdf', 'logo.html');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $html_bytes = $client->convertFile('logo.pdf'); // binary string echo strlen($html_bytes), PHP_EOL;
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $output_stream = fopen('logo.html', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->convertFileToStream('logo.pdf', $output_stream); if (!fflush($output_stream)) { throw new \RuntimeException('Cannot flush output stream'); } } finally { fclose($output_stream); }
Fetch a PDF from its URL.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $client->convertUrlToFile( 'https://your-server.com/invoice.pdf', 'invoice.html' );
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $html_bytes = $client->convertUrl('https://your-server.com/invoice.pdf'); // binary string echo strlen($html_bytes), PHP_EOL;
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $output_stream = fopen('invoice.html', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->convertUrlToStream( 'https://your-server.com/invoice.pdf', $output_stream ); if (!fflush($output_stream)) { throw new \RuntimeException('Cannot flush output stream'); } } finally { fclose($output_stream); }
Read logo.pdf as binary data. In an application, pass the bytes you already hold in memory instead.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $pdf_data_1 = file_get_contents('logo.pdf'); // binary string if ($pdf_data_1 === false) { throw new \RuntimeException('Cannot read binary input'); } $client->convertRawDataToFile($pdf_data_1, 'logo.html');
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $pdf_data_1 = file_get_contents('logo.pdf'); // binary string if ($pdf_data_1 === false) { throw new \RuntimeException('Cannot read binary input'); } $html_bytes = $client->convertRawData($pdf_data_1); // binary string echo strlen($html_bytes), PHP_EOL;
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $output_stream = fopen('logo.html', 'wb'); if ($output_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $pdf_data_1 = file_get_contents('logo.pdf'); // binary string if ($pdf_data_1 === false) { throw new \RuntimeException('Cannot read binary input'); } $client->convertRawDataToStream($pdf_data_1, $output_stream); if (!fflush($output_stream)) { throw new \RuntimeException('Cannot flush output stream'); } } finally { fclose($output_stream); }
Open logo.pdf as a binary input stream and save the result to logo.html.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $input_stream = fopen('logo.pdf', 'rb'); if ($input_stream === false) { throw new \RuntimeException('Cannot open stream'); } try { $client->convertStreamToFile($input_stream, 'logo.html'); } finally { fclose($input_stream); }
These examples upload report.pdf, a document with text, images, and vector graphics.
Convert pages 1 and 3 of report.pdf into excerpt.html.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $client->setPrintPageRange('1,3'); $client->convertFileToFile('report.pdf', 'excerpt.html');
Save the HTML, stylesheets, fonts, and images in report.zip. Extract the archive and serve its HTML with the accompanying files, preserving their relative paths.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $client->setCssMode('separate'); $client->setFontMode('separate'); $client->setForceZip(true); $client->setImageMode('separate'); $client->convertFileToFile('report.pdf', 'report.zip');
Scale pages to 75% and change the background, page spacing, and corners in preview.html. Use !important to override the generated background and margin rules.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $client->setCustomCss( '#page-container { background: #eaf0f6 !important; } .page { margin: 24px auto !important; border-radius: 8px; }' ); $client->setScaleFactor(75); $client->convertFileToFile('report.pdf', 'preview.html');
Prefix generated IDs, CSS classes, and font names with report_ in namespaced.html. This helps avoid naming conflicts when integrating the converted content into another page; adjust the surrounding layout separately.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $client->setHtmlNamespace('report_'); $client->convertFileToFile('report.pdf', 'namespaced.html');
Render page graphics at 72 DPI instead of the default 144 DPI and save smaller.html. Lower resolution can reduce file size at the cost of image detail; text remains HTML text.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $client->setDpi(72); $client->convertFileToFile('report.pdf', 'smaller.html');
Use SVG for page graphics in vector.html, preserving vector shapes when zooming. Raster images in the PDF remain raster images within the SVG.
<?php require 'vendor/autoload.php'; $client = new \Pdfcrowd\PdfToHtmlClient('demo', 'demo'); $client->setImageFormat('svg'); $client->convertFileToFile('report.pdf', 'vector.html');