Combine PDFs with Page Imposition - PHP Sample Code

Sample code for using Apryse SDK to combine (impose) multiple PDF pages. Page imposition can be used to arrange/order pages prior to printing or for document assembly (assemble a 'master' page from several 'source' pages). It is also possible to write applications that can re-order the pages such that they will display in the correct order when the hard copy pages are compiled and folded correctly. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF Editing & Manipulation Library.

1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/newsletter_booklet.pdf";
13
14//-----------------------------------------------------------------------------------
15// The sample illustrates how multiple pages can be combined/imposed
16// using PDFNet. Page imposition can be used to arrange/order pages
17// prior to printing or to assemble a 'master' page from several 'source'
18// pages. Using PDFNet API it is possible to write applications that can
19// re-order the pages such that they will display in the correct order
20// when the hard copy pages are compiled and folded correctly.
21//-----------------------------------------------------------------------------------
22
23 PDFNet::Initialize($LicenseKey);
24 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
25
26 echo nl2br("-------------------------------------------------\n");
27 echo nl2br("Opening the input pdf...\n");
28
29 $in_doc = new PDFDoc($input_path."newsletter.pdf");
30 $in_doc->InitSecurityHandler();
31
32 // Create a list of pages to import from one PDF document to another.
33 $import_pages = new VectorPage();
34 for ($itr=$in_doc->GetPageIterator(); $itr->HasNext(); $itr->Next())
35 $import_pages->push($itr->Current());
36
37 $new_doc = new PDFDoc();
38 $imported_pages = $new_doc->ImportPages($import_pages);
39
40 // Paper dimension for A3 format in points. Because one inch has
41 // 72 points, 11.69 inch 72 = 841.69 points
42 $media_box = new Rect(0.0, 0.0, 1190.88, 841.69);
43 $mid_point = $media_box->Width()/2;
44
45 $builder = new ElementBuilder();
46 $writer = new ElementWriter();
47
48 for ($i=0; $i<$imported_pages->size(); ++$i)
49 {
50 // Create a blank new A3 page and place on it two pages from the input document.
51 $new_page = $new_doc->PageCreate($media_box);
52 $writer->Begin($new_page);
53
54 // Place the first page
55 $src_page = $imported_pages->get($i);
56 $element = $builder->CreateForm($src_page);
57
58 $sc_x = $mid_point / $src_page->GetPageWidth();
59 $sc_y = $media_box->Height() / $src_page->GetPageHeight();
60 $scale = $sc_x < $sc_y ? $sc_x : $sc_y; // min(sc_x, sc_y)
61 $element->GetGState()->SetTransform($scale, 0.0, 0.0, $scale, 0.0, 0.0);
62 $writer->WritePlacedElement($element);
63
64 // Place the second page
65 ++$i;
66 if ($i<$imported_pages->size()) {
67 $src_page = $imported_pages->get($i);
68 $element = $builder->CreateForm($src_page);
69 $sc_x = $mid_point / $src_page->GetPageWidth();
70 $sc_y = $media_box->Height() / $src_page->GetPageHeight();
71 $scale = $sc_x < $sc_y ? $sc_x : $sc_y; // min(sc_x, sc_y)
72 $element->GetGState()->SetTransform($scale, 0.0, 0.0, $scale, $mid_point, 0.0);
73 $writer->WritePlacedElement($element);
74 }
75
76 $writer->End();
77 $new_doc->PagePushBack($new_page);
78 }
79
80 $new_doc->Save($output_path, SDFDoc::e_linearized);
81 PDFNet::Terminate();
82 echo nl2br("Done. Result saved in newsletter_booklet.pdf...");
83?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales