Working with PDF files in PHP should be headache, because you need Imagick extension, Ghostscript interpreter, etc.
So we created own API for basic PDF operations like merging, converting and stacking.
Install with Composer
composer require owly-digital/pdf-api-client
You can retrieve token by contacting us at info@owly.cz
use Owly\PdfApiClient\PdfApiClient;
$client = new PdfApiClient('token');
List of available endpoints will change over time. You can also suggest new features by creating issue.
$pdfFiles = [
'path/to/1.pdf',
'path/to/2.pdf',
];
$mergedPdfFile = $client->mergePdfFiles($pdfFiles);
file_put_contents('merged.pdf', $mergedPdfFile);
Convert PDF files (also with multiple pages) into various formats.
Parameters:
- Formats:
jpg
,png
,webp
(defaultjpg
) - Quality: (detected automatically when omited)
jpg
: 0 - 100 (default85
)png
: 0 - 9 (default9
)webp
: (default80
)
- Resolution (in pixels per inch) (default
600
- suitable for printing)
$pdfFiles = [
'path/to/1.pdf',
'path/to/2.pdf',
];
// Basic conversion with automatically detected quality and printable resolution
$convertedFiles = $client->convertPdfFiles($pdfFiles, 'jpg');
// With custom quality and resolution
$convertedFiles = $client->convertPdfFiles($pdfFiles, 'jpg', 50, 300);
// Conversion to webp with custom resolution (default quality)
$convertedFiles = $client->convertPdfFiles($pdfFiles, 'webp', null, 300);
foreach ($convertedFiles as $name => $file) {
file_put_contents('/path/' . $name, base64_decode($file));
}
When PDF with multiple pages passed, to each page is added suffix with page number.
Input:
awesomePdfFile.pdf (3 pages)
Output:
awesomePdfFile_0.jpg
awesomePdfFile_1.jpg
awesomePdfFile_2.jpg
$html = "<html><body><h1>Look At Me, I'm PDF now!</h1></body></html>";
// $pdf can be directly forwarded for download
$pdf = $client->convertHtmlToPdf($html);
// or with custom filename
$pdf = $client->convertHtmlToPdf($html, 'awesomePdfFile.pdf');
You can customize conversion with DevTools Protocol Options
// Show footer with page numbers
$options = [
'displayHeaderFooter' => true,
'headerTemplate' => '<div></div>',
'footerTemplate' => '<div>Page: <span class="pageNumber"></span> / <span class="totalPages"></span></div>'
];
$pdf = $client->convertHtmlToPdf($html, null, $options);