Read, merge, split PDF in 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.

System Requirements

To use GemBox.Pdf in PHP, you'll need to:

  1. Download and install GemBox.Pdf Setup.
  2. Expose GemBox.Pdf to COM Interop with Regasm.exe tool:
    :: 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]

Working with PDF files in PHP

The following example shows how you can read a PDF file from PHP, merge multiple PDF files into a single PDF and split a single PDF into multiple PDF files.

Merging PDF files into one and splitting PDF pages into multiple PDF files in PHP
Screenshot of merged PDF files and split PDF pages
  // Create ComHelper object.
  $comHelper = new Com("GemBox.Pdf.ComHelper", null, CP_UTF8);
  // If using Professional version, put your serial key below.

  $fileNames = array("\\%#MergeFile01.pdf%", "\\%#MergeFile02.pdf%", "\\%#MergeFile03.pdf%");

  *** Read PDF ***

  // Load PDF file.
  $document1 = $comHelper->Load(getcwd() . $fileNames[0]);
  $pages1 = $document1->Pages;

  // Read text content from each PDF page.
  for ($i1 = 0; $i1 < $pages1->Count; $i1++) {
      $page = $pages1->Item($i1);
      echo $page->Content->ToString() . "<br>";


  *** Merge PDF ***

  // Create PdfDocument object.
  $document2 = 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 ($i2 = 0; $i2 < $sourcePages->Count; $i2++) {


  $comHelper->Save($document2, getcwd() . "\\Merge Files.pdf");

  *** Split PDF ***

  // Load PDF file.
  $document3 = $comHelper->Load(getcwd() . "\\Merge Files.pdf");
  $pages3 = $document3->Pages;

  // Split a single PDF file into multiple PDF files.
  for ($i3 = 0; $i3 < $pages3->Count; $i3++) {
      $destinationDocument = new Com("GemBox.Pdf.PdfDocument", null, CP_UTF8);

      $comHelper->Save($destinationDocument, getcwd() . "\\Page" . $i3 . ".pdf");


Wrapper Library

Not all members of GemBox.Pdf are accesible because of the COM limitations like unsupported static and overload methods. That is why you can use ComHelper class which provides alternatives for some members that cannot be called with COM Interop.

However, if you need to use many GemBox.Pdf members from PHP, a recommended approach is to create a .NET wrapper library instead. Your wrapper library should do all the work within and exposes a minimal set of classes and methods to the unmanaged code.

This will enable you to take advantage of GemBox.Pdf's full capabilities, avoid any COM limitations, and improve performnace by reducing the number of COM Callable Wrappers created at runtime.

Want more?

Next example GitHub

Check the next example or select an example from the menu. You can also download our examples from the GitHub.

Like it?

Download Buy

If you want to try the GemBox.Pdf yourself, you can download the free version. It delivers the same performance and set of features as the professional version, but with some operations limited. To remove the limitation, you need to purchase a license.