There are meetings on this website and each meeting has a PDF attached showing the meeting minutes. The next meeting that takes place dynamically collects this attached PDF and allows for a download as the previous minutes. This is all working perfectly, however, when clicking the link for the previous minutes it always opens up a save as box. I was wondering how to get this to just display in the browser (assuming the user has a PDF viewer enabled)?
Here is the function from the controller:
public function current_pdf($id = null, $pdf = null) {
$this->loadModel('Document');
$document = $this->Document->find('list', array(
'fields' => array('Document.dir', 'Document.url')
));
$request_pdf = $this->Meeting->read(null, $id);
if ($pdf == 'current') {
$file = $document[$request_pdf['Meeting']['minutes']];
if (!empty($file)) {
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize(WWW_ROOT . 'files/document/url/' . $request_pdf['Meeting']['minutes'] . '/' . $file));
flush(); // this doesn't really matter.
$fp = fopen(WWW_ROOT . 'files/document/url/' . $request_pdf['Meeting']['minutes'] . '/' . $file, "r");
while (!feof($fp)) {
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
//echo WWW_ROOT.'files/document/url/pdf/'.$file; die;
//readfile(WWW_ROOT.'files/document/url/pdf/'.$file);
exit;
}
$this->redirect(array('controller' => 'meetings', 'action' => 'view/' . $id));
} else {
$file = $request_pdf['Meeting']['current_minutes'];
if (!empty($file)) {
//echo filesize(WWW_ROOT.'files/document/url/pdf/'.$file); die;
/* header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . urlencode(basename($file)));
// header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush(); */
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize(WWW_ROOT . 'files/document/url/pdf/' . $file));
flush(); // this doesn't really matter.
$fp = fopen(WWW_ROOT . 'files/document/url/pdf/' . $file, "r");
while (!feof($fp)) {
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
//echo WWW_ROOT.'files/document/url/pdf/'.$file; die;
//readfile(WWW_ROOT.'files/document/url/pdf/'.$file);
exit;
}
$this->redirect(array('controller' => 'meetings', 'action' => 'view/' . $id));
}
}
And here is the link in the view:
<?php echo $this->Html->link(__('Download Previous Minutes'), array('controller' => 'meetings', 'action' => 'current_pdf', $download, 'current')); ?>
I have tried removing the header("Content-Type...") lines of code and changing attachment in header("Content-Disposition: attachment; filename=" . urlencode($file));
to inline. This stopped the save as pop-up but displayed a load of spurious code in the browser instead.
Any help would be greatly appreciated as I'm currently very confused.