0

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 !"
movac
  • 1,576
  • 3
  • 21
  • 45
  • 1
    By indicating `$pdf->setSourceFile('/pdf/higher.pdf');` you are referencing an absolute route, you must use a relative one. Where is your pdf located? – namelivia May 22 '19 at 14:50
  • it's located in ``http://www.sample.com/pdf/higher.pdf`` – movac May 22 '19 at 14:52
  • 1
    I mean, in your filesystem, what folder inside your laravel project? `storage/app/public`? – namelivia May 22 '19 at 14:54
  • ahh right the path in the folder structure is root/public/pdf/higher (not in storage) – movac May 22 '19 at 14:55

1 Answers1

2

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.

namelivia
  • 2,657
  • 1
  • 21
  • 24