I'm trying to use FPDF and FPDI to edit a PDF and add text to it. I keep getting an "Incorrect output destination" error but the destination is the correct location that I want it to create a file in, why does FPDF not like my output destination?
This is in a laravel project
$pdf = new \setasign\Fpdi\Fpdi();
$pdf->AddPage();
$pdf->setSourceFile(public_path('/pdf/higher.pdf'));
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx, 10, 10, 100);
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');
$pdf->Output(public_path('/pdf/'),'higher2');
return $pdf;
and the error is:
message: "FPDF error: Incorrect output destination: /home/vagrant/code/project-name/public/pdf/"
I've also tried removing the "public_path()" and just setting it to Output('pdf', 'higher2')
and no good there either.
Furthermore I've also tried changing the name of the output pdf to higher2.pdf
just in case it wanted to see the extension (but obviously it's having more of a problem with the destination and not the name)
I've even tried changing permissions on this folder to be writable by anyone:
drwxrwxrwx 5 ion staff 160 May 21 05:44 pdf
edit: Just to note I see that the method with the public_path() is trying to save to my vagrant folder for some reason, that's part of the reason I'm confused. When I try to save to '/pdf' without public_path(), I get this error:
message: "FPDF error: Incorrect output destination: /pdf/"
edit 2:
I've also tried this:
$pdf->Output('F','/pdf/higher2.pdf');
and got the error:
message: "file_put_contents(/pdf/higher2.pdf): failed to open stream: No such file or directory"
and also tried the original name of the pdf which definitely exists and got the same error:
$pdf->Output('F','/pdf/higher.pdf');
You should never overwrite the file you are reading from!
The signature of the Output()
method is:
string Output([string dest [, string name [, boolean isUTF8]]])
The $dest
parameter is defined as:
Destination where to send the document. It can be one of the following:
I: send the file inline to the browser. The PDF viewer is used if available. D: send to the browser and force a file download with the name given by name. F: save to a local file with the name given by name (may include a path). S: return the document as a string.
The default value is I.
So your code:
$pdf->Output(public_path('/pdf/'),'higher2');
makes absolutely no sense. I guess you want to save the resulting PDF to the path in the public area with the name higher2.pdf
. So your code should look like:
$pdf->Output('F', public_path('/pdf/higher2.pdf'));
PS: You cannot edit a PDF with FPDI!
The Output() method requires the first parameter to be the destination and the 2nd parameter the filename.
From the documentation:
F: save to a local file with the name given by name (may include a path).
Try this:
$filename="/pdf/higher2.pdf";
$pdf->Output($filename,'F');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With