FPDF Getting "Incorrect output destination" but the error code showing the correct destination
Asked Answered
S

3

7

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');
Snooty answered 23/5, 2019 at 14:55 Comment(0)
B
10

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!

Bellboy answered 23/5, 2019 at 15:16 Comment(4)
aha, adding the public path in that way worked! For some reason it would save to the root file if I set it to Output('F', '/higher2.pdf') but wouldn't work if I set it to ``Output('F', '/pdf/higher2.pdf') Thank you! As for editing I just meant I'm adding text over it using FPDISnooty
You should read this about relative and absolute paths ;-) You're also welcome to mark my answer as the accepted one.Bellboy
Thanks! I am using mpdf and "Destination" is a directory in my mind. Not the output type. Thanks to your answer I now know that "destination" is ambiguous and the output type.Manse
I think this changed in a newer version of tFPDF. In an older version, there the path was the first parameter.Asquint
W
1

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');
Winterkill answered 23/5, 2019 at 15:13 Comment(5)
So I've tried that as well and for some reason it works if I make the filename "/higher2.pdf" but not if I try to put it into my /pdf folder, any idea why?Snooty
@Maribov does the folder pdf exists?Winterkill
"[...]the first parameter to be the destination and the 2nd parameter the filename." but why the parameters in the code example are interchanged?Bellboy
The folder pdf does exist within the same folder that it saves to if I leave it as "/filename.pdf"Snooty
@JanSlabon I guess it depends on FPDF version. The legacy 1.6 I have in a project indicates indeed Output( path, destination (like F, I, ...))Phycomycete
A
0

For the FPDF package, the syntax $pdf->Output('F','/pdf/higher2.pdf'); is wrong and you need to adjust your call as Jan Slabon explained.

However, if you want to support UTF-8 characters, then you need the tFPDF package, which is also supported by the setasign vendor:

$pdf = new \setasign\Fpdi\Tfpdf\Fpdi();

For this package you can store the output like this:

$pdf->Output('/pdf/higher2.pdf');
Asquint answered 11/11, 2019 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.