Arshad Hussain
Arshad Hussain

Reputation: 777

How to merge two pdf in single pdf

I have two different pdf files, named diff-1.pdf and diff-2.pdf and a single pdf named common.pdf.

Now I want to merge the contents of common.pdf to diff-1.pdf and generate a new file named first.pdf.

After that I want to merge the contents of common.pdf to diff-2.pdf and generate a new file named second.pdf.

I am using the pdfmerger library. I have used following code:

  <?pdf
       include 'PDFMerger.php';
       $pdf = new PDFMerger;

       <!-- Merge 'common.pdf' and 'diff-1.pdf' -->
       $pdf->addPDF('commonpdf/common.pdf', 'all');
       $pdf->addPDF('allpdf/diff-1.pdf', 'all');
       $pdf->merge('file', 'pdftest/first.pdf');

       <!-- Merge 'common.pdf' and 'diff-2.pdf' -->
       $pdf->addPDF('commonpdf/common.pdf', 'all');
       $pdf->addPDF('allpdf/diff-2.pdf', 'all');
       $pdf->merge('file', 'pdftest/second.pdf');
   ?>

but problem is arising that this code is generating only first.pdf at once. But I want to generate two pdf files in a single execution of this code. What should I do for this purpose?

Upvotes: 1

Views: 623

Answers (1)

Jan Slabon
Jan Slabon

Reputation: 5058

You have to initate a 2nd instance of PDFMerger after the first merger() call.

Anyhow, you should avoid using the PDFMerger class at all, because it uses a 5 (!!) years old version of FPDI! Just check this demo for a simple example showing you how to concatenate documents with FPDI.

Upvotes: 1

Related Questions