rovac
rovac

Reputation: 2073

Getting 'Cannot open pdf' error with a correct source url when using FPDI

I'm trying to edit a pdf in laravel. I've created the following function in my controller, which has use FPDI and use FPDF

static function getHigherPDF() {

    $pdf = new FPDI();
    $pdf->AddPage();
    $pdf->setSourceFile('/pdf/higher.pdf');
    $tplIdx = $pdf->importPage(1);
    $pdf->useTemplate($tplIdx, 10, 10, 100);
    $pdf->Output();
    $pdf->SetFont('Helvetica');
    $pdf->SetTextColor(255, 0, 0);
    $pdf->SetXY(30, 30);
    $pdf->Write(0, 'This is just a simple text');

    return $pdf;

}

I keep getting the following error and I can't figure out why, I even get the error if I put a full path with http:// etc to the pdf. The pdf opens in my browser when I go to that file. I can't find any information about why that might be happening if the file url is a valid url

any ideas?

{message: "Cannot open /pdf/higher.pdf !", exception: "InvalidArgumentException",...}
exception: "InvalidArgumentException"
file: "/home/vagrant/code/brainskills-at-work/vendor/setasign/fpdi/pdf_parser.php"
message: "Cannot open /pdf/higher.pdf !"

Upvotes: 0

Views: 1626

Answers (1)

namelivia
namelivia

Reputation: 2735

You should reference your file using the laravel generator for public paths:

$pdf->setSourceFile(public_path('/pdf/higher.pdf'));

That will generate an absolute path on your filesystem to the file.

Upvotes: 2

Related Questions