Read, merge, and 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]

Merge PDF files in PHP

You can learn how to read and merge multiple PDF files into a single PDF file from PHP with the following example.

Merging PDF files into one in PHP
Screenshot of merged PDF files
<?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

To read a PDF file and split its pages into multiple ones, use the code in the example below.

Splitting PDF pages into multiple PDF files in PHP
Screenshot of split PDF pages
<?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

Not all members of GemBox.Pdf are accessible due to the COM limitations like unsupported static and overload methods. But you can use the ComHelper class, which provides alternatives for some members that you can’t call with COM Interop.

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.

See also


Next steps

GemBox.Pdf is a .NET component that enables developers to read, merge and split PDF files or execute low-level object manipulations from .NET applications in a simple and efficient way.

Download Buy