using header() to rewrite filename in URL for dynamic pdf

后端 未结 5 1955
野的像风
野的像风 2020-12-03 06:56

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_

相关标签:
5条回答
  • 2020-12-03 07:04

    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.

    0 讨论(0)
  • 2020-12-03 07:09

    Should be:

    header('Content-disposition: attachment;filename="July Report.pdf"'); 
    
    0 讨论(0)
  • 2020-12-03 07:14

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

    0 讨论(0)
  • 2020-12-03 07:15

    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.

    0 讨论(0)
  • 2020-12-03 07:25

    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=

    0 讨论(0)
提交回复
热议问题