0

I've written the following code for merging PDFs using this answer

function merge_pdfs() {

    $pdfs_array = array('1.pdf', '2.pdf');

    $pdf = new FPDI_Protection();

    for ($i = 0; $i < count($pdfs_array); $i++ ) {
        $pagecount = $pdf->setSourceFile($pdfs_array[$i]);

        for($j = 0; $j < $pagecount ; $j++) {

            $tplidx = $pdf->importPage(($j +1), '/MediaBox');
            $pdf->addPage('P','A4');
            $pdf->useTemplate($tplidx, 0, 0, 0, 0, TRUE);
        }
    }

    $dt = new DateTime(NULL, new DateTimeZone($data->user->timezone));
    $pdf->SetTitle('PDF, created: '.$dt->format(MYHMRS_DATETIME_FRIENDLY));
    $pdf->SetSubject('PDF subject !');
    $output = $pdf->Output('', 'S');
    $name = "PDF".'-'.$dt->format('ymd').'.pdf';

    $this->output
        ->set_header("Content-Disposition: filename=$name;")
        ->set_content_type('Application/pdf')
        ->set_output($output);
}

So, after this I'm getting the following error message

This document (1.pdf) probably uses a compression technique which is not supported by the free parser shipped with FPDI. (See https://www.setasign.com/fpdi-pdf-parser for more details)

I've checked the link and it suggests to set another PDF Parser ( If I understand right )

But I'm not sure how to make it working with Codeigniter and my example

Should I create library and try to use it? Or maybe you know another solution for merging PDFs

ArmKh
  • 435
  • 9
  • 31

2 Answers2

1

The issue was related to PDF versions

Edit

If you don't know, the PDFs has versions. Yeah, I was surprised as well. Please check them here PDF versions

So, the problem was that I was trying to merge PDF 1.5 version with PDF 1.6

Jake
  • 2,058
  • 2
  • 24
  • 33
ArmKh
  • 435
  • 9
  • 31
-1

An example. It is simple.

<?php
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->AddPage('P');
$mpdf->WriteHTML('<h1>More</h1>');
$mpdf->Output();
?>
halojoy
  • 225
  • 2
  • 7