Read, merge, split PDF files from PHP
GemBox.Pdf is a .NET library that enable you to process PDF files from any .NET application. But it's also a COM-accessible library that you can use in PHP as well. To use GemBox.Pdf in PHP, you'll need to: You can learn how to read and merge multiple PDF files into a single PDF file from PHP with the following example. To read a PDF file and split its pages into multiple ones, use the code in the example below. Not all members of GemBox.Pdf are accessible due to the COM limitations like unsupported static and overload methods. But you can use the Nonetheless, if you need to use many GemBox.Pdf members from PHP, we recommend creating a .NET wrapper library. Your wrapper library should be responsible for all the work, exposing a minimal set of classes and methods to the unmanaged code. It will enable you to take advantage of GemBox.Pdf's full capabilities, avoid any COM limitations, and improve performance by reducing the number of COM Callable Wrappers created at runtime.System Requirements
:: Add GemBox.Pdf to COM registry for x86 (32-bit) applications.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe [path to installed GemBox.Pdf.dll]
:: Add GemBox.Pdf to COM registry for x64 (64-bit) applications.
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe [path to installed GemBox.Pdf.dll]
Merge PDF files in PHP
<?php
// Create ComHelper object.
$comHelper = new Com("GemBox.Pdf.ComHelper", null, CP_UTF8);
// If using the Professional version, put your serial key below.
$comHelper->ComSetLicense("FREE-LIMITED-KEY");
$fileNames = array("\\%#MergeFile01.pdf%", "\\%#MergeFile02.pdf%", "\\%#MergeFile03.pdf%");
// Create PdfDocument object.
$document = new Com("GemBox.Pdf.PdfDocument", null, CP_UTF8);
// Merge multiple PDF files into a single PDF file.
foreach ($fileNames as $fileName) {
$sourceDocument = $comHelper->Load(getcwd() . $fileName);
$sourcePages = $sourceDocument->Pages;
for ($i = 0; $i < $sourcePages->Count; $i++) {
$document->Pages->AddClone($sourcePages->Item($i));
}
$sourceDocument->Dispose();
}
$comHelper->Save($document, getcwd() . "\\MergedFile.pdf");
$document->Dispose();
?>
Split PDF Files using PHP
<?php
// Create ComHelper object.
$comHelper = new Com("GemBox.Pdf.ComHelper", null, CP_UTF8);
// If using the Professional version, put your serial key below.
$comHelper->ComSetLicense("FREE-LIMITED-KEY");
// Load PDF file.
$document = $comHelper->Load(getcwd() . "\\%#LoremIpsum.pdf%");
$pages = $document->Pages;
// Split a single PDF file into multiple PDF files.
for ($i = 0; $i < $pages->Count; $i++) {
$destinationDocument = new Com("GemBox.Pdf.PdfDocument", null, CP_UTF8);
$destinationDocument->Pages->AddClone($pages->Item($i));
$comHelper->Save($destinationDocument, getcwd() . "\\LoremIpsum" . $i . ".pdf");
$destinationDocument->Dispose();
}
$document->Dispose();
?>
Wrapper Library
ComHelper
class, which provides alternatives for some members that you can’t call with COM Interop.