Setasign / FPDI

FPDI is a collection of PHP classes facilitating developers to read pages from existing PDF documents and use them as templates in FPDF.
https://www.setasign.com/fpdi
MIT License
1.09k stars 159 forks source link

The xref position points to an incorrect object type #149

Closed leonidaswander closed 2 years ago

leonidaswander commented 2 years ago

Hi, i need write text in all pages of my pdf file, my code is:

    require_once('fpdf/fpdf.php');
    require_once('fpdi/autoload.php');
    use setasign\Fpdi\Fpdi;
    use setasign\Fpdi\PdfReader;

    $file= 'myfile.pdf';
    $pdf = new Fpdi;
    $pages_count = $pdf->setSourceFile( $file); 

    for($i = 1; $i <= $pages_count; $i++)
    {
            $pdf->AddPage(); 
            $tplIdx = $pdf->importPage($i);
            $pdf->useTemplate($tplIdx, 0, 10, 200);  
            $pdf->SetFont('Arial'); 
            $pdf->SetTextColor(137,137,137); 
            $pdf->SetXY(20, 10); 
            $pdf->Write(1, $nameUser ); 
    }
    $pdf->Output('livro-download.pdf', 'I');

on localhost it works perfectly, but in host i get this error

    Fatal error: Uncaught setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException: The xref position points to an incorrect object type.
    in /home/dh_server/fpdi/setasign/fpdi/src/PdfParser/CrossReference/CrossReference.php:264
    Stack trace: #0 /home/dh_server/fpdi/setasign/fpdi/src/PdfParser/CrossReference/CrossReference.php(209):
    setasign\Fpdi\PdfParser\CrossReference\CrossReference->initReaderInstance(Object(setasign\Fpdi\PdfParser\Type\PdfNumeric))
    #1 /home/dh_server/fpdi/setasign/fpdi/src/PdfParser/CrossReference/CrossReference.php(67): setasign\Fpdi\PdfParser\CrossReference\CrossReference->readXref(2714)
    #2 /home/dh_server/fpdi/setasign/fpdi/src/PdfParser/PdfParser.php(157): setasign\Fpdi\PdfParser\CrossReference\CrossReference->__construct(Object(setasign\Fpdi\PdfParser\PdfParser), 0)
    #3 /home/dh_server/fpdi/setasign/fpdi/src/PdfParser/PdfParser.php(205): setasign\Fpdi\PdfParser\PdfParser->getCrossReference()
    # in /home/dh_server/fpdi/setasign/fpdi/src/PdfParser/CrossReference/CrossReference.php on line 264 

Is there any library or something that generates this kind of failure?

JanSlabon commented 2 years ago

Double check that the PDF file was transfered in binary mode and was not corrupted during the transfer to your host.

meruiden commented 2 years ago

Having this issue too

JanSlabon commented 2 years ago

And did you double check that the PDF file was transfered in binary mode and was not corrupted during the transfer to your host? If not, feel free to share the file in question.

meruiden commented 2 years ago

Turned out it was corrupted indeed, my browser was able to view it still, which is why it didn't seem corrupted in the first place. However, when I upload it to the paid version demo of FPDI here: https://www.setasign.com/products/fpdi-pdf-parser/details/ it indeed works with the paid CorruptedReader. I guess to mitigate this issue, you'd have to upgrade

JanSlabon commented 2 years ago

I guess to mitigate this issue, you'd have to upgrade

Or create a valid PDF file - sometimes this is possible by simply resaving it e.g. through Acrobat.