3214 Posts in 848 Topics by 810 members
| Go to End | Next > | |
| Author | Topic: | 4692 Views |
-
how to download pdf file ?

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
-
Re: how to download pdf file ?

7 February 2009 at 3:54am Last edited: 7 February 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>
-
Re: how to download pdf file ?

7 February 2009 at 6:06am Last edited: 7 February 2009 6:07am
i said that i want to download file, not to open it in the browser window :/
-
Re: how to download pdf file ?

7 February 2009 at 5:49pm Last edited: 7 February 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.
-
Re: how to download pdf file ?

7 February 2009 at 8:00pm Last edited: 7 February 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>
-
Re: how to download pdf file ?

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>
-
Re: how to download pdf file ?

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
-
Re: how to download pdf file ?

15 December 2009 at 9:09am
send_file() is a function on HTTPRequest not HTTPResponse. Or as of 2.4 its SS_HTTPRequest.
| 4692 Views | ||
| Go to Top | Next > |



