Jump to:

3401 Posts in 979 Topics by 855 members

Template Questions

SilverStripe Forums » Template Questions » how to download pdf file ?

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 5494 Views
  • snaip
    Avatar
    Community Member
    180 Posts

    how to download pdf file ? Link to this post

    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

  • UncleCheese
    Avatar
    4085 Posts

    Re: how to download pdf file ? Link to this post

    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>

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: how to download pdf file ? Link to this post

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

  • UncleCheese
    Avatar
    4085 Posts

    Re: how to download pdf file ? Link to this post

    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.

  • ajshort
    Avatar
    Community Member
    244 Posts

    Re: how to download pdf file ? Link to this post

    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>

  • Dave L
    Avatar
    Community Member
    56 Posts

    Re: how to download pdf file ? Link to this post

    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>

  • snaip
    Avatar
    Community Member
    180 Posts

    Re: how to download pdf file ? Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: how to download pdf file ? Link to this post

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

    5494 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.