40

I have a php script that generates a pdf report. When we go to save the pdf document, the filename that Acrobat suggests is report_pdf, since the php script is named report_pdf.php. I would like to dynamically name the pdf file, so I don't have to type the appropriate name for the report each time that I save it.

Asking on a news group, someone suggested this, where filename="July Report.pdf" is the intended name of the report

<?
header('Content-Type: application/pdf');
header('Content-disposition: filename="July Report.pdf"');

But it doesn't work. Am I doing it wrong, or will this work at all? Is this a job for mod_rewrite?


So I've tried both
header('Content-disposition: inline; filename="July Report.pdf"');

and

header('Content-disposition: attachment; filename="July Report.pdf"');

( not at the same time ) and neither work for me. Is this a problem with my web host? For this url, here's my code:

<?
header('Content-disposition: inline; filename="July Report.pdf"');

// requires the R&OS pdf class
require_once('class.ezpdf.php');
require_once('class.pdf.php');

// make a new pdf object
$pdf = new Cpdf();
// select the font
$pdf->selectFont('./fonts/Helvetica');
$pdf->addText(30,400,30,'Hello World');
$pdf->stream();

?>
user151841
  • 17,377
  • 29
  • 109
  • 171
  • 1
    If you want the file to be downloaded (and not openend in the browser) , add these: header('Content-Transfer-Encoding: binary'); header('Content-Type: application/force-download'); Otherwise, you should still include the line header('Content-Type: application/pdf') so you browser knows how to open the file. – eCaroth Jan 06 '10 at 22:07
  • Also, you might be missing the content-length for the file - some browsers open files correctly without this and some don't. To get this, dump all the output into an output buffer, then print out the headers and specify one as 'Content-Length:'**the strlen of the output buffer*** – eCaroth Jan 06 '10 at 22:11
  • Temporarily comment those php header lines and see the output. It should NOT contain any php errors. Apply back the headers after you fix any errors if so. – Bimal Poudel Feb 09 '18 at 16:54

5 Answers5

73

Try:

header('Content-Disposition: attachment; filename="July Report.pdf"');

or

header('Content-Disposition: inline; filename="July Report.pdf"');

Another option would be to use the $_SERVER['PATH_INFO'] to pass your "July Report.pdf" - an example link might be:

<a href="report_pdf.php/July%20Report.pdf?month=07">

That file should default to saving as "July Report.pdf" - and should behave exactly like your old php script did, just change the code that produces the link to the pdf.

gnarf
  • 105,192
  • 25
  • 127
  • 161
9

Should be:

header('Content-disposition: attachment;filename="July Report.pdf"'); 
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
2

Based on https://github.com/rospdf/pdf-php/raw/master/readme.pdf (Page 19) The stream-method accepts an array as parameter:

stream([array options]) Used for output, this will set the required headers and output the pdf code. The options array can be used to set a number of things about the output:

'Content-Disposition'=>'filename' sets the filename, ...

This code works well for me

$ezOutput = $pdf->ezStream(array("Content-Disposition"=>"YourFileName.pdf"));

In my case I use ezStream() but I think stream() should give the same result.

Seelixh
  • 77
  • 6
  • 1
    Actually the accepted doesn't work for me. This one actually works!! Hence thank you! – Alex Jan 20 '16 at 16:49
1

For the name shown on the title tab in the browser

I had the same problem, then i found that it's metadata missing inside my .pdf. I used a tools ("debenu PDF tools") for edit pdf property like author, title, etc... I just change title from empty field to what title I want, upload the new pdf and now, with same code, same script the browser show the right name!

For the name when u ask to save document

is what u specify in header filename=

ArK
  • 20,698
  • 67
  • 109
  • 136
JoTaRo
  • 173
  • 12
  • Setting the PDF title in the metadata also fixes the filename shown in the top left corner when viewing the PDF in Chrome. For those using FPDF to dynamically generate PDFs, use `$pdf->SetTitle('Your Title');`. Thanks! – jessica Sep 14 '16 at 17:25
0

Did you try to remove spaces from file name using hyphens? So, I think its name must be like this; filename=July-Report.pdf

Kerem
  • 11,377
  • 5
  • 59
  • 58