A robust and dependency-free Laravel package to convert PDF files to HTML using poppler-utils (pdftohtml).
- Dependency Free: Does not rely on external PHP packages.
- Laravel Integration: Automatic discovery, config publishing, and easy-to-use API.
- Binary Auto-Discovery: Automatically finds
pdftohtmlandpdfinfobinaries on your system. - Customizable: Extensive options for zooming, image handling, and output formatting.
- Inline Assets: Automatically inlines CSS and Images (Base64) for a self-contained HTML output.
- Strict Types: Written with modern PHP standards and strict typing.
- PHP > 8.1
poppler-utilsinstalled on your server (containspdftohtmlandpdfinfo).
-
Install via Composer:
composer require squareetlabs/laravel-pdf-to-html
-
Install
poppler-utils:- Ubuntu/Debian:
sudo apt-get install poppler-utils
- MacOS:
brew install poppler
- CentOS/RHEL:
sudo yum install poppler-utils
- Ubuntu/Debian:
-
Publish Configuration (Optional):
php artisan vendor:publish --provider="Squareetlabs\LaravelPdfToHtml\Providers\PdfToHtmlServiceProvider"
use Squareetlabs\LaravelPdfToHtml\Pdf;
try {
// Create a new instance
$pdf = new Pdf('/path/to/document.pdf');
// Get HTML content
$html = $pdf->getHtml();
// Get all pages content as an array
$pages = $html->getAllPages();
// Get specific page
$page1 = $html->getPage(1);
echo $page1;
} catch (\Exception $e) {
echo "Error: " . $e->getMessage();
}You can pass options to the constructor to customize the behavior:
$options = [
'pdftohtml_path' => '/usr/custom/bin/pdftohtml', // Optional custom path
'pdfinfo_path' => '/usr/custom/bin/pdfinfo', // Optional custom path
'generate' => [
'singlePage' => false, // Split pages (default)
'imageJpeg' => true, // Convert images to JPEG
'ignoreImages' => false, // Keep images
'zoom' => 1.5, // Zoom factor
'noFrames' => true, // Output without frames
],
'html' => [
'inlineCss' => true, // Inline CSS into style attributes
'inlineImages' => true, // Convert images to Base64
'onlyContent' => true, // Return only body content
],
'clearAfter' => true, // Clear temp files after processing
];
$pdf = new Pdf('/path/to/document.pdf', $options);$info = $pdf->getInfo();
// Returns array: ['pages' => 10, 'size' => '...', ...]
$count = $pdf->countPages();composer testMIT