moinul / laravel-pdf-to-html
A Laravel package to convert PDF files to HTML using poppler-utils
Requires
- php: ^8.2
- illuminate/support: ^7.0|^8.0|^9.0|^10.0|^11.0
README
A Laravel package that converts PDF files to HTML with preserved formatting and styling using poppler-utils.
Requirements
- PHP 8.2 or higher
- Laravel 7.x|8.x|9.x|10.x|11.x
pdftohtml
command-line tool (poppler-utils)
Installation
- Install the package via Composer:
composer require moinul/laravel-pdf-to-html
- Install the required system dependency:
For Ubuntu/Debian:
sudo apt-get install poppler-utils
For CentOS/RHEL:
sudo yum install poppler-utils
For macOS:
brew install poppler
-
The package will automatically register its service provider in Laravel 5.5 and newer.
-
Create the storage symlink if you haven't already:
php artisan storage:link
Usage
Basic Usage
use Moinul\LaravelPdfToHtml\Services\PdfToHtmlConverter; public function convertPdf(Request $request) { $converter = app(PdfToHtmlConverter::class); $html = $converter->convert('path/to/your/file.pdf'); return $html; // Returns HtmlString }
Example Route
Here's a complete example of a route that converts a PDF file to HTML:
use Illuminate\Support\Facades\Route; Route::get('/pdf-to-html', function () { $pdfPath = public_path('example.pdf'); try { $converter = app(PdfToHtmlConverter::class); $html = $converter->convert($pdfPath); return view('pdf.html-output', ['html' => $html]); } catch (\Exception $e) { return "Error converting PDF: " . $e->getMessage(); } });
Example View
Create a view file resources/views/pdf/html-output.blade.php
:
<!DOCTYPE html> <html> <head> <title>PDF to HTML Conversion</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> {!! $html !!} </body> </html>
Features
- Converts PDF files to responsive HTML
- Preserves text formatting and layout
- Handles images and maintains their positions
- Includes print-friendly styles
- Responsive design for various screen sizes
- Uses the efficient
pdftohtml
command-line tool
How it Works
The package uses the pdftohtml
command-line tool from poppler-utils to convert PDF files to HTML. The conversion process:
- Takes a PDF file as input
- Converts it to HTML while preserving formatting
- Processes images and fixes their paths
- Adds responsive and print-friendly CSS
- Returns a clean, formatted HTML string
License
This package is open-sourced software licensed under the MIT license.
Support
If you encounter any issues or have questions, please create an issue on GitHub.