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:
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
How Download Big File Using PHP (Low Memory Usage)
Create Superglobal Variables in PHP
How to Strip Out the Domain Name from a Url in PHP
Codeigniter: How to Do a Select (Distinct Fieldname) MySQL Query
Curl File Uploads Not Working Anymore After Upgrade from PHP 5.5 to 5.6
How to Change the Array Key to Start from 1 Instead of 0
How to Convert a String to an Array in PHP
How to Check for Special Characters PHP
Next Business Day of Given Date in PHP
Inet_Aton() and Inet_Ntoa() in PHP
Laravel Form HTML with Put Method for Put Routes
How to Convert Between Time Zones in PHP Using the Datetime Class
Change Woocommerce Default Password Security Level
PHP Difference Between Notice and Warning
How to Determine Whether It's a Mobile Device with PHP
Call to Undefined Function Curl_Init() Even It Is Enabled in PHP7
How to Redefine a Type Hint to a Descendant Class When Extending an Abstract Class