Try out before you actually use it
docker run --pull always -p 9090:80 treineticprojects/demo_opensource:latestA robust PHP wrapper for Ghostscript, designed to make PDF manipulation easy. Convert PDFs to images, create PDFs from images, optimize file sizes, and merge documents with simple, fluent APIs.
👉 Try the Interactive Demo (Run it locally to see features in action)
This project is an initiative of Treinetic (Pvt) Ltd, Sri Lanka.
| Basic Operations | Manipulation (v1.5) | Advanced (v2.0) |
|---|---|---|
| PDF to Images | Split PDF | OCR (Text Recognition) |
| Images to PDF | Encrypt / Protect | PDF/A Archiving |
| Compress / Optimize | Watermark | Page Rotation |
| Merge PDFs | Thumbnail | Metadata Editing |
| Version Convert | Form Flattening |
- PHP >= 5.5.0
- Ghostscript >= 9.16 installed and configured on your system.
Ubuntu / Debian
sudo apt-get update
sudo apt-get install ghostscriptMacOS (Homebrew)
brew install ghostscriptWindows
- Download the Ghostscript AGPL Release installer from the official website.
- Run the installer.
- Important: Add the
binandlibdirectories of your Ghostscript installation (e.g.,C:\Program Files\gs\gs9.54.0\bin) to your system'sPATHenvironment variable. - Verify by running
gswin64c -vin Command Prompt.
Install via Composer:
composer require imal-h/pdf-boxConvert a PDF page to an image in just a few lines:
use ImalH\PDFLib\PDFLib;
$pdfLib = new PDFLib();
$pdfLib->setPdfPath("document.pdf")
->setOutputPath("output_folder")
->convert();Convert specific pages or the entire document to PNG or JPEG.
$pdfLib = new PDFLib();
$pdfLib->setPdfPath('my_document.pdf')
->setOutputPath('output_images')
->setImageFormat(PDFLib::$IMAGE_FORMAT_PNG) // or $IMAGE_FORMAT_JPEG
->setDPI(300)
->setPageRange(1, 5) // Optional: convert only pages 1-5
->convert();Combine a list of images into a single PDF file.
$pdfLib = new PDFLib();
$images = ['page1.jpg', 'page2.jpg', 'page3.jpg'];
$pdfLib->makePDF('combined_output.pdf', $images);Reduce file size using Ghostscript's optimization presets.
$pdfLib = new PDFLib();
// Levels: screen (72dpi), ebook (150dpi), printer (300dpi), prepress (300dpi, color)
$pdfLib->compress('large.pdf', 'optimized.pdf', PDFLib::$COMPRESSION_EBOOK);Combine multiple PDF documents into one.
$pdfLib = new PDFLib();
$files = ['part1.pdf', 'part2.pdf'];
$pdfLib->merge($files, 'merged_complete.pdf');Extract a specific page or a range of pages.
$pdfLib = new PDFLib();
// Extract just Page 1
$pdfLib->split(1, 'page_one.pdf', 'source.pdf');
// Extract Pages 1 to 5
$pdfLib->split('1-5', 'chapter_one.pdf', 'source.pdf');Protect your PDF with passwords and disable printing/copying.
$pdfLib = new PDFLib();
// args: user_password, owner_password, output_path, input_path
$pdfLib->encrypt('open123', 'admin123', 'protected_doc.pdf', 'source.pdf');Add a text watermark to every page (overlay).
$pdfLib = new PDFLib();
$pdfLib->addWatermarkText('CONFIDENTIAL', 'watermarked.pdf', 'source.pdf');Generate a thumbnail image (JPEG) of the first page.
$pdfLib = new PDFLib();
// args: output_image, width (approx), input_pdf
$pdfLib->createThumbnail('thumbnail.jpg', 200, 'source.pdf');Convert a PDF to a specific PDF version (e.g., 1.4 for compatibility).
$pdfLib = new PDFLib();
$pdfLib->convertToVersion('1.4', 'compatible.pdf', 'source.pdf');Set PDF properties like Title, Author, etc.
$pdfLib = new PDFLib();
$metadata = [
'Title' => 'Financial Report 2024',
'Author' => 'Finance Dept',
'Keywords' => 'finance, 2024, report'
];
$pdfLib->setMetadata($metadata, 'tagged.pdf', 'source.pdf');Rotate all pages by 90, 180, or 270 degrees.
$pdfLib = new PDFLib();
// Rotate 90 degrees clockwise
$pdfLib->rotateAll(90, 'rotated.pdf', 'source.pdf');Burn interactive form fields into the page content (prevent editing).
$pdfLib = new PDFLib();
$pdfLib->flatten('flat.pdf', 'form.pdf');Convert to PDF/A-1b standard for archival (requires valid color profiles in Ghostscript).
$pdfLib = new PDFLib();
try {
$pdfLib->convertToPDFA('archive.pdf', 'source.pdf');
} catch (Exception $e) {
echo "PDF/A conversion failed: " . $e->getMessage();
}Convert scanned PDFs to searchable text. Requires Ghostscript >= 9.53 with Tesseract/OCR devices.
$pdfLib = new PDFLib();
try {
// Language code: 'eng', 'deu', 'spa', etc.
$pdfLib->ocr('eng', 'searchable.pdf', 'scanned.pdf');
} catch (Exception $e) {
echo "OCR failed (check if Tesseract is installed): " . $e->getMessage();
}Fine-tune the behavior of the library:
setNumberOfRenderingThreads(int $threads): Speed up conversion by using multiple threads (Default: 4).setDPI(int $dpi): Set output image resolution (Default: 300).setImageQuality(int $quality): Set JPEG quality (0-100).setFilePrefix(string $prefix): Custom prefix for output images (Default: "page-").
$pdfLib->setNumberOfRenderingThreads(8)
->setDPI(150)
->setFilePrefix('slide-');Error: **** Unable to open the initial device, quitting.
- Cause: Ghostscript cannot create temporary files due to permission issues.
- Fix: Ensure your web server (e.g., Apache/Nginx user) has write permissions to the system's temporary directory, or check your server logs for specific path errors.
Ghostscript Issues
- Ensure the
gscommand is available in your system path. - On Windows, ensure
gswin64c.exeorgswin32c.exeis in your PATH.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
