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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

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

how to download pdf file ?


Go to End


10 Posts   10488 Views

Avatar
snaip

Community Member, 181 Posts

7 February 2009 at 2:59am

   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

Forum Moderator, 4102 Posts

7 February 2009 at 3:54am

Edited: 07/02/2009 3:55am

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

Community Member, 181 Posts

7 February 2009 at 6:06am

Edited: 07/02/2009 6:07am

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

7 February 2009 at 5:49pm

Edited: 07/02/2009 5:58pm

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

Community Member, 244 Posts

7 February 2009 at 8:00pm

Edited: 07/02/2009 8:24pm

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

Community Member, 60 Posts

19 November 2009 at 1:46am

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

Community Member, 181 Posts

15 December 2009 at 2:33am

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

Forum Moderator, 5523 Posts

15 December 2009 at 9:09am

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

Go to Top