unknow-sk / laravel-pdf-to
Laravel package for converting pdf to text, html or image
Fund package maintenance!
unknow-sk
Open Collective
Requires
- php: ^8.2
- ext-fileinfo: *
- illuminate/contracts: ^11.0 || ^12.0
- illuminate/support: ^11.0 || ^12.0
- spatie/laravel-package-tools: ^1.16
- spatie/pdf-to-text: ^1.54
- symfony/process: ^4.0 || ^5.0 || ^6.0 || ^7.0
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1
- orchestra/testbench: ^9.0.0 || ^10.0.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3 || ^2.0
- phpstan/phpstan-deprecation-rules: ^1.1 || ^2.0
- phpstan/phpstan-phpunit: ^1.3 || ^2.0
- roave/security-advisories: dev-latest
- spatie/laravel-ray: ^1.35
- spatie/pdf-to-image: ^3.1
Suggests
- spatie/pdf-to-image: Required ^3.1 for pdf to image conversion via Imagick
This package is auto-updated.
Last update: 2025-04-29 06:05:58 UTC
README
Laravel package for extracting Text/Html from a PDF or converting it to images (PNG, JPeG).
Support us
We invest a lot of time and give our hearts to work in Open Source.
Installation
You can install the package via composer:
composer require unknow-sk/laravel-pdf-to
You can publish the config file with:
php artisan vendor:publish --tag="laravel-pdf-to-config"
This is the content of the published config file:
return [ /** * Set the pdftotext binary path manually */ 'pdftotext_bin' => env('PDF_TO_TEXT_PATH'), /** * Set the pdftohtml binary path manually */ 'pdftohtml_bin' => env('PDF_TO_HTML_PATH'), /** * Set the pdftoppm binary path manually */ 'pdftoppm_bin' => env('PDF_TO_PPM_PATH'), /** * Set the pdftocairo binary path manually */ 'pdftocairo_bin' => env('PDF_TO_CAIRO_PATH'), /** * Set the default output directory */ 'output_dir' => env('PDF_TO_OUTPUT_DIR', storage_path('app/pdf-to')), ];
Required Packages
This package relies on the following external tools:
- pdftotext: For extracting text from PDFs.
- pdftohtml: For converting PDFs to HTML.
- pdftoppm: For generating images from PDFs.
Make sure these tools are installed and available in your system's PATH. On macOS, you can install them via Homebrew:
brew install poppler
Binary Files Configuration
By default, the package attempts to locate the required binary files (pdftotext
, pdftohtml
, pdftoppm
, or pdftocairo
) automatically. If these binaries are not found in your system's PATH, you will need to set their paths manually in the configuration file.
You can update the configuration file config/pdf-to.php
as follows:
return [ 'pdftotext_bin' => env('PDF_TO_TEXT_PATH'), 'pdftohtml_bin' => env('PDF_TO_HTML_PATH'), 'pdftoppm_bin' => env('PDF_TO_PPM_PATH'), 'pdftocairo_bin' => env('PDF_TO_CAIRO_PATH'), ];
Alternative Libraries
For text extraction, this package uses the Spatie/pdf-to-text. However, if you don't have pdftoppm
or pdftocairo
installed, you can also use the Spatie/pdf-to-image for image generation. This provides a fallback mechanism to ensure functionality even without the required binaries.
Usage
Extract Text from PDF
use UnknowSk\LaravelPdfTo\Facades\LaravelPdfTo; $text = LaravelPdfTo::setFile('path/to/your/file.pdf') ->setTimeout(120) // optionally ->result('txt'); echo $text;
Convert PDF to HTML
use UnknowSk\LaravelPdfTo\Facades\LaravelPdfTo; $html = LaravelPdfTo::setFile('path/to/your/file.pdf') ->setConfig(['options' => [...]]) // optionally ->saveAs('output-file') // optionally, if you wan to store as file, then result returns path ->result('html'); echo $html;
Convert PDF to Images
use UnknowSk\LaravelPdfTo\Facades\LaravelPdfTo; $image = LaravelPdfTo::setFile('path/to/your/file.pdf') ->setTimeout(180) // optionally ->result('png'); echo $image; // Path to the generated image
Testing
To run the tests, use the following command:
composer test
The tests include functionality for extracting text, converting to HTML, and generating images from PDFs. Example test files are located in the tests/
directory.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.