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.