0

Thanks in advance...

I am trying to generate a pdf in CodeIgniter using the fpdi and tcpdf libraries using the following code...

<?php
use setasign\Fpdi;

require_once('tcpdf/tcpdf.php');
require_once('fpdi2/autoload.php');

class Pdf extends Fpdi\Tcpdf\Fpdi
{
    /**
     * "Remembers" the template id of the imported page
     */
    protected $logo;

    /**
     * Draw an imported PDF logo on every page
     */
    function Header()
    {
        if ($this->logo === null) {
            $this->setSourceFile(base_url().'assets/pdf/logo.pdf'); //Will work if these 3 lines are commented
            $this->logo = $this->importPage(1);                     //Will work if these 3 lines are commented
        }
        $size = $this->useImportedPage($this->logo, 130, 5, 60);    //Will work if these 3 lines are commented

        $this->SetFont('freesans', 'B', 20);
        $this->SetTextColor(0);
        $this->SetXY(PDF_MARGIN_LEFT, 5);
        $this->Cell(0, 30, 'TCPDF and FPDI');
    }
}

// initiate PDF
$pdf = new Pdf();
$pdf->SetMargins(PDF_MARGIN_LEFT, 40, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(true, 40);

// add a page
$pdf->AddPage();

$pdf->Write(5, "hello world");

$pdf->Output('generated.pdf', 'I');

?>

It runs and generates the pdf if I comment the lines I mention in the inline comments, but generates the error:

Type: InvalidArgumentException Message: Given stream is not seekable!

when I uncomment those lines. The code works in my local server outside codeigniter. I am able to embed the pdf in another view, so it is not a problem with the pdf nor with a restriction of accessing pdfs from my assets folder. The same problem happens when I try to load an external font. The view is being called by the controller this way...

class Projects extends CI_Controller{
        public function fpdi(){
            $this->load->view('projects/fpdi');
        }
}

Thanks a lot! (note that this is an oversimplification of the problem, I am not loading the libraries inside the view, this is just to make the problem as concise as possible for demonstration purposes) edit-UPDATE tried the same thing with fpdf and I still get the same error "Given stream is not seekable".

UPDATE 2! Thanks to Jan Slabon's advice I kind of solved it, a replaced base_url() helper with CodeIgniter constant FCPATH so the path was relative and not a URL. Apparently this is required for fopen to function properly which is used by the fpdi library.

I don't know more and I feel this is inelegant solution but it works for now! If anybody has more info it would be greatly appreciated.

  • The function ´base_url()` seems to return an URL which is simply not seekable. You should use a local path and not an URL. – Jan Slabon Mar 14 '21 at 13:15
  • @JanSlabon how do I use a local path inside a view? I made a small experiment where I load an image, when the source is local it doesn't load, but when I use base_url() it does. The same happens with an embedded pdf. Thanks! – JupiterPalace Mar 14 '21 at 15:40

1 Answers1

0

Changed the source from base_url() to relative path FCPATH. (More info at the bottom of question.)

  • This isn't an issue about absolute or relative path but about the difference between an URL and a simple local path to a file. – Jan Slabon Mar 15 '21 at 06:58
  • Thanks! Does it have to do with how fopen works when the source of the file is external vs local? – JupiterPalace Mar 15 '21 at 18:27
  • Exactly! See [here](https://www.php.net/manual/de/wrappers.php) for an overview of builtin protocols and these streams needs to be [seekable](https://www.php.net/manual/de/function.stream-get-meta-data.php) for PDF parsing. – Jan Slabon Mar 16 '21 at 07:15