0

I have some PHP pages and PDF that I need to merge into a single PDF file. I am using the MPDF library but I am getting the following error:

"Unable to find object (4, 0) at expected location."

This is my code:

<?php

function pdf_recreate($f)
{
    rename($f, str_replace('.pdf', '_.pdf', $f));

    $fileArray = [str_replace('.pdf', '.pdf', $f)];
    $outputName = $f;
    $cmd = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=$outputName ";

    foreach ($fileArray as $file) {
        $cmd .= $file . " ";
    }

    $result = shell_exec($cmd);
    //unlink(str_replace('.pdf','_.pdf',$f));
}

$new_pdf_recreate = pdf_recreate('../upload_dir/testPDF.pdf');

try {

    $mpdf = new \Mpdf\Mpdf();
    $mpdf->setFooter('{PAGENO}');
    $mpdf->SetImportUse();

    // Create Initial Invoice PDF
    $mpdf->WriteHTML($content);

    // import C.pdf
    // $pagecount = $mpdf->SetSourceFile(dirname(__FILE__).'/C.pdf');
    $pagecount = $mpdf->SetSourceFile(dirname(__FILE__) . $new_pdf_recreate);

    //Loop through the pages adding them to the PDF
    for ($i = 1; $i <= $pagecount; $i++) {
        $mpdf->AddPage('');
        $import_page = $mpdf->ImportPage($i);
        $mpdf->UseTemplate($import_page);
    }

    $mpdf->Output();

} catch (\Mpdf\MpdfException $e) {
    echo $e->getMessage();
}

Can anyone tell me why am getting this message/error?

Finwe
  • 6,372
  • 2
  • 29
  • 44
Yaco Zaragoza
  • 429
  • 1
  • 7
  • 18
  • 1
    This sounds like the cross reference table of the pdf in question is broken and contains wrong offsets, at least for object 4. – mkl Jul 08 '18 at 06:11
  • 1
    +1 for @mkl. But I wonder that `gs` will produce such faulty files? Can you provide testPDF.pdf and the result of `pdf_recreate()` – Jan Slabon Jul 09 '18 at 12:23
  • The file in question is a word doc with the words "This is a test" that was printed as a PDF called Test D.pdf – Yaco Zaragoza Jul 09 '18 at 14:11

0 Answers0