This topic is locked

Guide 18 - "Merge" of PDFS files

6/16/2021
PHPRunner Tips and Tricks
fhumanes author

I have seen in the PHPrunner forum that there was a certain need to "mix" pdf's file pages and as I saw that it is simple subject to solve and given the interest it awaken, I have created an example that happened to describe.

Objective

This example should be used to make "merge" of PDF's files

DEMO: https://fhumanes.com/pdfMerge/

All the details and the source code on my portal: https://fhumanes.com/blog/guias-desarrollo/guia-18-phprunner-merge-de-ficheros-pdfs/

fhumanes author 6/16/2021

Technical solution

To make this management with the PDF files I downloaded and used this github library: https://github.com/cogginabox/pdf-merger

In turn, this library is based on the products I have used to dynamically generate the PDF files of the multiple examples that exist in the portal.

img alt

In the documentation of the library this example of use is provided:

$pdf = new \Clegginabox\PDFMerger\PDFMerger;

$pdf->addPDF('samplepdfs/one.pdf', '1, 3, 4');
$pdf->addPDF('samplepdfs/two.pdf', '1-2');
$pdf->addPDF('samplepdfs/three.pdf', 'all');

//You can optionally specify a different orientation for each PDF
$pdf->addPDF('samplepdfs/one.pdf', '1, 3, 4', 'L');
$pdf->addPDF('samplepdfs/two.pdf', '1-2', 'P');

$pdf->merge('file', 'samplepdfs/TEST2.pdf', 'P');

// REPLACE 'file' WITH 'browser', 'download', 'string', or 'file' for output options
// Last parameter is for orientation (P for protrait, L for Landscape).
// This will be used for every PDF that doesn't have an orientation specified

Actually, it is very simple, fast and practical, the solution that gives us.

To have a functional example, I have defined the following requirements:

  • In the registry you can upload all the PDF's files that you want.
  • In the Registry there will be a "Command" field, which will specify the operations that must be done with the PDF's files.
  • The output file name (PDF) can be indicated and whereby the application will be delivered.

The application interface is:

img alt

On the "pdfMerge" button, the commands that are defined in the "Command" field are executed.

The syntax is very simple and everything has been adjusted to the library command that has previously been shown. In the example I have not dedicated a lot of time to the syntactic analysis of this nomenclature, so if it is not exact, it is possible that it does not do anything or error.

To make new tests, please create new record with your own PDF's files

The most important thing is the coding of the use of the library and that is "pdfMerge.php":

<?php
/*
library https://github.com/clegginabox/pdf-merger

Exmple:
$pdf = new \Clegginabox\PDFMerger\PDFMerger;

$pdf->addPDF('samplepdfs/one.pdf', '1, 3, 4');
$pdf->addPDF('samplepdfs/two.pdf', '1-2');
$pdf->addPDF('samplepdfs/three.pdf', 'all');

//You can optionally specify a different orientation for each PDF
$pdf->addPDF('samplepdfs/one.pdf', '1, 3, 4', 'L');
$pdf->addPDF('samplepdfs/two.pdf', '1-2', 'P');

$pdf->merge('file', 'samplepdfs/TEST2.pdf', 'P');

// REPLACE 'file' WITH 'browser', 'download', 'string', or 'file' for output options
// Last parameter is for orientation (P for protrait, L for Landscape).
// This will be used for every PDF that doesn't have an orientation specified
*/
@ini_set("display_errors","1");
@ini_set("display_startup_errors","1");

require_once(__DIR__ . "/../include/dbcommon.php");
require_once(__DIR__ . "/analysis_of_files.php");

require_once __DIR__ . '/pdfMerge_1.0/autoload.php';

$rs = DB::Query("select * from pdfmerge where id_pdfMerge =".$_SESSION['id_pdfMerge']);
while( $data = $rs->fetchAssoc() )
{
$arrayFiles = arrayPDF($data['Files']);
$arrayCommand = explode("\n", $data['Command']);

$pdf = new \Clegginabox\PDFMerger\PDFMerger;
foreach ($arrayCommand as &$Command) {
$statement = explode(";",$Command);
if ( count($statement) > 1 ) {
$part1 = explode(")",$statement[0]);
$part2 = explode("(",$part1[0]);
$file = $part2[1];
if ( is_numeric($file) ) {
$part = explode("|",$statement[1]);
switch (count($part)) {
case 1:
$pdf->addPDF($arrayFiles[$file], trim($part[0]));
break;
case 2:
$pdf->addPDF($arrayFiles[$file], trim($part[0]), trim($part[1]));
break;
}
} else {
$part = explode(" | ",$statement[1]);
switch (count($part)) {
case 2:
$pdf->merge(trim($part[0]), trim($part[1]));
break;
case 3:
$pdf->merge(trim($part[0]), trim($part[1]), trim($part[2]) );
break;
}
}
}
}

}

For any questions or need, contact me via email fernandohumanes@gmail.com

I leave the project (PHPRunner 10.2) so you can deploy it on your PCs. Recodting that you must update the sample record with your own PDF's files.