11

When running code the file is the pdf file is getting saved in the browser instead of it getting saved on the server

fpdf Output('filename.pdf','F'); 

downloading file on browser instead of saving it on the server

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
user3766591
  • 357
  • 1
  • 2
  • 11

6 Answers6

33

As explained in the FPDF documentation, you have to use the D as parameter to the output function to send the PDF to the browser and force a file download with the name given by name:

Output('D','filename.pdf');

For reference, here are the different values for the destination parameter:

I: send the file inline to the browser. The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
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. name is ignored.

Veve
  • 6,643
  • 5
  • 39
  • 58
  • 2
    I want to generate the pdf file on my server...and dont want to get the file downloaded. – user3766591 Mar 19 '15 at 11:26
  • OK, didn't understood your question that way. Do you have any error message? What's the result of your instruction? It shouldn't do anything more than create the file on your server with option `F`... – Veve Mar 19 '15 at 11:30
  • ya when i used F its downloading file to computer and not the server – user3766591 Mar 20 '15 at 09:57
  • Are you testing it on localhost ? I don't see otherwhise why it would be on the computer. – Veve Mar 20 '15 at 10:05
  • 1
    If I run the example you give, it doesn't work. The parameters are backwards for me. Doing what you and the docs show gives me the error and I have to enter `$pdf->Output('filename.pdf','D')` This was driving me nuts for the last hour. – Altimus Prime Jun 14 '21 at 05:32
8

Here is the example, checkit out!

       $pdf->Output(F,'directory/filename.pdf'); 

Remember that if you use F in the output settings that your directory on your server needs to have permissions set to 777 or it does not work. Hope that helps anyone who is not getting the file to save to their directory.

:) Kim

Shoeb Mirza
  • 912
  • 1
  • 11
  • 30
horsey_kim
  • 137
  • 1
  • 7
  • 1
    This is a good answer Kim. But I would suggest users keep the directory permissions to 755 and make sure the httpd user (like 'nobody') has write permissions to the directory. You don't want to open up a security problem. – Jeff Feb 14 '16 at 23:33
  • I agree Jeff, thanks for that bit of advice. What would you suggest if you need the file to create on your server and not download to your desktop? Any advice is greatly welcomed. – horsey_kim Feb 15 '16 at 17:28
  • 1
    not sure about the server vs desktop. My point was that the directory 'some-directory' should not allow anyone at all to write to it. Whatever user is running the webserver process should be the only user writing to this directory. 755 or even 775 is safer than 777. In this exmaple, the httpd process is owned by the user 'nobody' `drwxr-xr-x nobody webusers 136 Dec 11 20:57 some-directory` – Jeff Feb 15 '16 at 17:39
  • Thanks I see what you are saying about user. Thanks for the advice. I agree that 775 would be a better choice and it does work. 777 not a good choice for security reasons. Thanks so much and have a wonderful day. – horsey_kim Feb 15 '16 at 17:50
  • This is perfect, Thanks for giving the idea, I used - AddPage(); $pdf->SetFont('Arial','B',16); $pdf->Cell(40,10,'Hello World!'); $pdf->Output(F,'/var/www/html/-PATH-/filename.pdf'); ob_end_flush(); ?> – Wasim Khan Aug 04 '17 at 14:22
2

I'm using FPDF 1.6 and PHP 7.0.33 and the Output() function does not work like the manual says.

I couldn't find the manual for this version, and the manual for the version 1.81 says to use this function like this:

string Output([string dest [, string name [, boolean isUTF8]]])

But, in my project it only works with the following parameter order:

string Output( string name,string dest, boolean isUTF8)
biruk1230
  • 3,042
  • 4
  • 16
  • 29
  • Thanks I have PDPF version 1.7 (with TCPDF over it) and it seems to be the same case too. Quite a change this was. – cdsaenz Jun 14 '20 at 18:51
1

Output
string Output([string dest [, string name [, boolean isUTF8]]])

Description
Send the document to a given destination: browser, file or string. In the case of a browser, the PDF viewer may be used or a download may be forced.
The method first calls Close() if necessary to terminate the document.

Parameters

  • des
    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.
  • name
    The name of the file. It is ignored in case of destination S.
    The default value is doc.pdf.
  • isUTF8
    Indicates if name is encoded in ISO-8859-1 (false) or UTF-8 (true). Only used for destinations I and D.
    The default value is false.

Example:

$pdf->Output('D', "filename.pdf", true);

font: http://www.fpdf.org/?lang=en

0

This worked for me. $PaymentReference is a string.

$pdf->Output('',$PaymentReference.'.pdf', false);
NOhs
  • 2,780
  • 3
  • 25
  • 59
0

Late to the party, but for helping others who may be looking for similar solutions. Using the documentation, this is what worked for me

if (!class_exists('FPDF'))
   require(plugin_dir_path( __FILE__ ).'pdf/fpdf.php');  
    
$pdf = new FPDF('P', 'in', 'Letter');
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(50,50, "this is a pdf example 1", 0, 1, 'C', true);

// Now download to the local computer
$pdf->Output("test.pdf", "D");    // Download to the user's computer

or to put to the same browser page

if (!class_exists('FPDF'))
   require(plugin_dir_path( __FILE__ ).'pdf/fpdf.php');  
    
$pdf = new FPDF('P', 'in', 'Letter');
$pdf->AddPage();
$pdf->SetFont("Arial","B",14);
$pdf->Cell(50,50, "this is a pdf example 1", 0, 1, 'C', true);

ob_clean();                      // CLEAN THE previous output for the page. 
$pdf->Output("test.pdf", "I");   // display pdf in the same page
Debbie Kurth
  • 403
  • 3
  • 16