Jump to content

Recommended Posts

Posted

Hi there,

I am using the following template file to provide a pdf download, of a pdf stored in a file-field.

<?php
$file = $page->pdf->filename;
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);

If template caching is disabled, everything working as expected and the following header is sent

HTTP/1.1 200 OK
Date: Fri, 26 Feb 2016 13:03:27 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Disposition: attachment; filename="anyfile.pdf"
Content-Length: 909233
X-XSS-Protection: 1; mode=block
Keep-Alive: timeout=2, max=1000
Connection: Keep-Alive
Content-Type: application/pdf

Using Template cache the header is overwritten by

HTTP/1.1 200 OK
Date: Fri, 26 Feb 2016 13:06:11 GMT
Server: Apache
X-Frame-Options: SAMEORIGIN
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
X-XSS-Protection: 1; mode=block
Vary: Accept-Encoding
Content-Encoding: gzip
Keep-Alive: timeout=2, max=1000
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8

Any ideas to prevent this?
 

Posted

@LostKobrakai
Thanks, I know this. But this is only the Content-Type header. I want to change some others too! As a minimum: Content-Disposition and Content-Length.

Posted

That's probably not possible without hooking somewhere in the cache. The template cache does probably only store the content, but not any headers, which can be desired or not desired.

Posted

It works using wireSendFile(). New template:

<?php
/**
 * using this template allows you to use template cache
 *
 */
$file = $page->pdf->filename;
$disposition = 'inline'; // change to 'attachment' to provide download
$headers = array(    
    "content-length" => filesize($file),
    "content-disposition" => 'inline; filename="'.basename($file).'"'
);
wireSendFile($file, array('forceDownload' => false), $headers);

Define any headers to set new or overwrite default. "Force download 'must be set to false to avoid that 'Content-Disposition' is overwritten due to file extension.

But I am not sure if this uses the cache. Maybe its useless anyway it takes the same time to render.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...