0

When importing and modifying a PDF file inside a Joomla component using the FPDF and FPDI libraries, after sending the file to the browser, the PDF viewer claims the XREFS table to be broken.

Here's part of what I have:

<?php
    ...
    use \setasign\Fpdi\Fpdi;
    include "fpdf/fpdf.php";
    include "fpdi2/src/autoload.php";

    class MycompoViewMycompo extends JViewLegacy
    {
        protected $pdf;
        function display($tpl = null) {
            ...
            $this->pdf = new Fpdi();
            $this->pdf->AddPage();
            $this->pdf->setSourceFile('components/com_mycompo/views/mycompo/template.pdf');
            $tplIdx = $this->pdf->importPage(1);
            $this->pdf->useImportedPage($tplIdx, 0, 0, null, null, true);

            $this->pdf->SetFont('Arial', '', 13);
            $this->pdf->SetTextColor(0, 0, 0);

            $this->pdf->SetXY(175, 49);
            $pdf->Cell(0, 0, 'some Text', 0, 0, 'R');

            // This actually happens in the layout...
            // But I'd like to simplify here
            $doc =& JFactory::getDocument();
            $doc->setMimeEncoding('application/pdf');
            $this->pdf->Output('gift_coupon_generated.pdf', 'D', true);
        }
        ...
    }
?>

The document is sent to the browser. It also can be downloaded and opened by a PDF viewer (I use PDF Exchange Editor). All the file's conents seem to be correct. No visible problems. But a warning is shown that the XREFS table is broken. (The exact message reads like "One or more XREF data streams were not found". I don't know the exact English message, as I use a different language version)

When working with the same code OUTSIDE of Joomla (i. e. in a standard PHP environment and a simple php web page), the file is also created perfectly fine and this error is not shown.

Any ideas on what might be the reason for this error message? Or how I could debug it further?

Or is using FPDF/FPDI not supported with Joomla?

Please find the generated file here: https://file.re/2021/10/16/banktransferform/

Best regards Tom

TomS
  • 467
  • 9
  • 25
  • Please share the PDF in question. – Jan Slabon Oct 13 '21 at 05:48
  • I can share the PDF file, but I also forgot to mention that, even when commenting the call to `setSourceFile` and the call to `useImportedPage` (i. e. when starting with a completely empty document) the same error message is shown. (Sorry for not giving this information earlier.) – TomS Oct 13 '21 at 14:37
  • 1
    Then simply open the resulting PDF document in a text editor and check the beginning and end of the file for e.g. HTML output. I guess that's the problem. Otherwise, please share the resulting PDF document. – Jan Slabon Oct 14 '21 at 08:04
  • You are absolutely right! There is HTML content at the beginning of the file. I add it to my question above. Is there a way to avoid that? – TomS Oct 16 '21 at 12:27
  • 1
    Then this issue is related to "how to send a file in Joomla" instead of an issue with FPDF or FPDI. I'm not familiar with Joomla, sorry. – Jan Slabon Oct 18 '21 at 06:20
  • Please post all Joomla-related questions on [joomla.se] Stack Exchange. – mickmackusa Feb 15 '22 at 00:55

0 Answers0