Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Template Questions

how to download pdf file ?


Go to End
Reply

10 Posts   5844 Views

Avatar
snaip

7 February 2009 at 2:59am Community Member, 180 Posts

static $db = array(
'Pdf_file' => 'Text'
);

function pdf() {
header("Content-type: application/pdf");
header("Content-Disposition:attachment; filename='mysite/assets/pdf/".$this->Pdf_file."'");
readfile("mysite/assets/pdf/".$this->Pdf_file."");
}

how to execute function in <a href=""> ?

<a href="$pdf"> download pdf file </a> ?

doesn't work

Avatar
UncleCheese

7 February 2009 at 3:54am (Last edited: 7 February 2009 3:55am), 4085 Posts

Whoooooooa.. you're working way too hard.

class MyPage extends Page
{
static $has_one = array ('PDF' => 'File');
public function getCMSFields() {
$f = parent:: getCMSFields();
$f->addFieldToTab("Root.Content.PDF", new FileIFrameField('PDF'));
return $f;
}
}

Template:

<a href="$PDF.URL" title="Download $PDF.Title">Download ($PDF.Filesize)</a>

Avatar
snaip

7 February 2009 at 6:06am (Last edited: 7 February 2009 6:07am), Community Member, 180 Posts

i said that i want to download file, not to open it in the browser window :/

Avatar
UncleCheese

7 February 2009 at 5:49pm (Last edited: 7 February 2009 5:58pm), 4085 Posts

Safari will do that because it has native PDF support. Firefox and most other browsers will force a download. Otherwise, just right click and do a save as.

I really don't thinkit's necessary to start throwing HTTP headers for a PDF download, but if you really want to do it, here are some tips.

Your function isn't working because you keep passing it the PDF_file object rather than its URL property.

Your link isn't working because you're trying to use a template function to accomplish a controller level task. Run the pdf function as an action on the controller

<a href="$Link(pdf)">

That will return /current-url-segment/pdf and run your function in the controller.

Avatar
ajshort

7 February 2009 at 8:00pm (Last edited: 7 February 2009 8:24pm), Community Member, 244 Posts

Theres already a method for sending files to a browser that will simplify what you're trying to do here - just use something like

class Page extends SiteTree {
   
   public static $has_one = array (
      'PDF' => 'File'
   );
   
   // ...
   
}

class Page_Controller extends ContentController {
   
   public function pdf() {
      return HTTPResponse::send_file(file_get_contents($this->PDF()->URL), 'myfile.pdf');
   }
   
}

in your controller, and you can do something like this in your template (this will force a download):

<a href="$Link(pdf)">Download the PDF File ($PDF.Filesize)</a>

Avatar
Dave L

19 November 2009 at 1:46am Community Member, 56 Posts

Posting here for future searchers.

Forcing a file download dialog:

Not sure if this is kosher but seems to work, file size is the same from one on disk uploaded to CMS and then downloaded again this way. Only tested on FF3.5/OSX. Would be interested in improvements. Use at your own risk.

Rough idea, it's late.

function PhotoDownload() {
$id = Director::urlParam('ID');
if ($id) {

//INSERT SECURITY CODE HERE SO THEY CAN'T HACK THE URL ID FOR DIFFERENT FILES

//get a File object or subclass of (e.g. Image)
$image = DataObject::get_by_id("Image",$id);

$path = $image->getFullPath();
$name = basename($path);
$response = new HttpResponse();
$response->setBody(file_get_contents($path));
$response->addHeader("Content-disposition","attachment; filename=" . $name);
return $response;
} else {
return false;
}
}

Call this function in your template, i.e. <a href="$Link(PhotoDownload)/$ID">Download</a>

Avatar
snaip

15 December 2009 at 2:33am Community Member, 180 Posts

class EnWycieczkaPage_Controller extends EnPage_Controller {

   function pdf_download() {
      return HTTPResponse::send_file(file_get_contents($this->PDF()->URL), 'myfile.pdf');
   }

....

}

<% if PDF %>
   <a href="$Link(pdf_download)"><img src="themes/tutorial/images/pdf.gif" width="32" height="32" /></a>
<% end_if %>

Fatal error: Call to undefined method HTTPResponse::send_file() in C:\serwer\strony\ernesto\mysite\code\EnWycieczkaPage.php on line 103

SS 2.3.2

Avatar
Willr

15 December 2009 at 9:09am Forum Moderator, 5511 Posts

send_file() is a function on HTTPRequest not HTTPResponse. Or as of 2.4 its SS_HTTPRequest.

Go to Top