Using Header() to Rewrite Filename in Url for Dynamic PDF

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

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.

Display a pdf with PHP not displayed correctly

Have you tried embedding it using html object tag?

<object data="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf">
<embed src="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf" />
</object>

Alternatively in php, try this header:

header('Content-Disposition: attachment; filename="' . $filename . '"');

Finally, if @readfile($file) isn't working, try:

echo @file_get_contents($file);

After trying this myself locally, I think I managed to get it working. Try this:

<?php

$remote_pdf = 'http://www.saronicferries.gr/content/pdfFiles/sample.pdf';
$remote_file_name = end(explode('/', $remote_pdf));
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'. $remote_file_name .'"');
header('Content-Transfer-Encoding: binary');
readfile($remote_pdf);

?>

Output:

Sample Image

I think what was the problem is that filesize($file) was failing for remote file. After removing that and header('Accept-Ranges: bytes');, it works.

name web pdf for better default save filename in Acrobat?

Part of the problem is that the relevant RFC 2183 doesn't really state what to do with a disposition type of "inline" and a filename.

Also, as far as I can tell, the only UA that actually uses the filename for type=inline is Firefox (see test case).

Finally, it's not obvious that the plugin API actually makes that information available (maybe someboy familiar with the API can elaborate).

That being said, I have sent a pointer to this question to an Adobe person; maybe the right people will have a look.

Related: see attempt to clarify Content-Disposition in HTTP in draft-reschke-rfc2183-in-http -- this is early work in progress, feedback appreciated.

Update: I have added a test case, which seems to indicate that the Acrobat reader plugin doesn't use the response headers (in Firefox), although the plugin API provides access to them.

Generate and display unqiue PDF filename

You can use the uniqid() function to create a unique name.

The alternative would be to create a database and store the data there. Via Auto_Increment you would get a unique ID with which you can create a name for the PDF file.

These are in principle the most common options. You could also, if you have a username, combine it with a unique ID.

Download and save a file using php (keeping original filename)

Here's an adaptation of the thread I linked to that should work for your case. The regex is looking for the last '/' and then returning everything after it.

<?php
function get_real($url) {
$headers = get_headers($url);
foreach($headers as $header) {
if (strpos(strtolower($header),'location:') !== false) {
return preg_replace('~.*/(.*)~', '$1', $header);
}
}
}
echo get_real('http://www.example.com/bajar.php?id=420633&u=7');
?>

Django Change File name in PDF export

filename="somefilename.pdf" there is place where you can determine your filename.
You can use it as:

filename = 'somefilename.pdf'
response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)

Specifying filename for dynamic PDF in asp.net

Add a content-disposition to the header:

Response.AddHeader("content-disposition", @"attachment;filename=""MyFile.pdf""");


Related Topics



Leave a reply



Submit