AVAILABLE NOW: Spring 2025 Release

How to Programmatically Merge, Split, and Reorder PDFs Using PHP

By Isaac Maw | 2025 Apr 23

Sanity Image
Read time

4 min

Summary: Offering essential PDF Manipulation tools within your application helps keep users within your ecosystem. Here’s a quick overview of adding Apryse PDF Manipulation tools via SDK to your application using PHP.

Introduction

Copied to clipboard

When you’re building an application that handles documents, it’s important to provide manipulation tools. Without a robust solution for handling PDF pages, users might seek external tools to split, reorder, or merge PDFs. This not only highlights a gap in your application's functionality but also raises potential security and privacy concerns when users turn to third-party tools.

To address this, we can explore how to use the Apryse SDK to provide PDF manipulation capabilities, such as splitting a PDF into multiple pages and merging several PDFs into one. While the examples will be in JavaScript, the SDK also supports other languages and frameworks like C++, .NET, Python, and Node.js. 

Getting Started

Copied to clipboard

Getting started is easy:

  1. Download the Apryse Server SDK. In this case, we’ll want to choose the JavaScript PDF library.
  2. Extract the folder from the .zip file.
  3. Get a free trial key.
  4. See our documentation for the full code you’ll need to handle the following example tasks. I’ve included a sample of the specific code for each task.

Now that we’ve got everything set up, we can look at some examples of what we can do. In this blog, we’ll look at how to:

  • Split a PDF document into multiple pages
  • Merge several PDF documents into one
  • Delete every second page

Split a PDF into Multiple

Copied to clipboard

There may be times that you need to split a PDF into multiple documents. For example, you’re splitting out a report of your employees to create a separate file for each employee.

We’ll use the following code:

$doc = new PDFDoc($filename);
$page_num = $doc->GetPageCount();
for ($i = 1; $i <= $page_num; ++$i)
{
  $new_doc = new PDFDoc();
  $new_doc->InsertPages(0, $doc, $i, $i, PDFDoc::e_none);
  $new_doc->Save($output_filename."_split_page_".$i.".pdf", SDFDoc::e_linearized);
  $new_doc->Close();
}

Merge Several PDFs into One

Copied to clipboard

Now let’s look at the previous example in reverse. You have a separate PDF for each of your employees and need to merge them into one report for your manager.

Let’s use the following code to merge those PDFs.

echo nl2br("_______________________________________________\n");  
echo nl2br("Sample 2 - Merge several PDF documents into one...\n"); $new_doc = new PDFDoc(); $new_doc->InitSecurityHandler(); 
$page_num = 15; 
for ($i = 1; $i <= $page_num; ++$i) 
{     
    echo nl2br("Opening newsletter_split_page_".$i.".pdf\n"); 
    $in_doc = new PDFDoc($output_path."newsletter_split_page_".$i.".pdf"); 
    $new_doc->InsertPages($i, $in_doc, 1, $in_doc->GetPageCount(), PDFDoc::e_none); 
    $in_doc->Close(); 
} 
$new_doc->Save($output_path."newsletter_merge_pages.pdf", SDFDoc::e_remove_unused); 
echo nl2br("Done. Result saved in newsletter_merge_pages.pdf\n"); 
$in_doc->Close(); 

Delete Every Second Page

Copied to clipboard

If you need to delete specific pages, you can try this sample code. For example, if a long report has title pages or blank pages that you’d like to remove, this function can help. In this sample code, you'll find how to delete every second page, leaving just the pages you need:

 // Sample 3 - Delete every second page 
 
echo nl2br("_______________________________________________\n"); 
echo nl2br("Sample 3 - Delete every second page...\n"); 
echo nl2br("Opening the input pdf...\n"); 
$in_doc = new PDFDoc($input_path."newsletter.pdf"); 
$in_doc->InitSecurityHandler(); 
     
$page_num = $in_doc->GetPageCount(); 
while ($page_num>=1) 
{ 
    $itr = $in_doc->GetPageIterator($page_num); 
    $in_doc->PageRemove($itr); 
    $page_num -= 2; 
}         
     
$in_doc->Save($output_path."newsletter_page_remove.pdf", 0); 
echo nl2br("Done. Result saved in newsletter_page_remove.pdf...\n"); 
 

Other Examples

Copied to clipboard

See our documentation for more examples of what you can do such as removing a PDF page and cropping a page.

Conclusion

Copied to clipboard

We’ve just covered a few of the many ways to manipulate PDFs using the Apryse PDF SDK. The code in these examples can be expanded to create more elaborate solutions to help you to manipulate PDFs, whether that is to split, add, remove, crop or reorder pages, or to rotate single or multiple pages in your PDF.

Sanity Image

Isaac Maw

Technical Content Creator

Share this post

email
linkedIn
twitter