Note that there are some explanatory texts on larger screens.

plurals
  1. POView PDF in browser rather than download
    text
    copied!<p>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)?</p> <p>Here is the function from the controller: </p> <pre><code>public function current_pdf($id = null, $pdf = null) { $this-&gt;loadModel('Document'); $document = $this-&gt;Document-&gt;find('list', array( 'fields' =&gt; array('Document.dir', 'Document.url') )); $request_pdf = $this-&gt;Meeting-&gt;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-&gt;redirect(array('controller' =&gt; 'meetings', 'action' =&gt; '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-&gt;redirect(array('controller' =&gt; 'meetings', 'action' =&gt; 'view/' . $id)); } </code></pre> <p>}</p> <p>And here is the link in the view:</p> <p><code>&lt;?php echo $this-&gt;Html-&gt;link(__('Download Previous Minutes'), array('controller' =&gt; 'meetings', 'action' =&gt; 'current_pdf', $download, 'current')); ?&gt;</code></p> <p>I have tried removing the header("Content-Type...") lines of code and changing attachment in <code>header("Content-Disposition: attachment; filename=" . urlencode($file));</code> to inline. This stopped the save as pop-up but displayed a load of spurious code in the browser instead.</p> <p>Any help would be greatly appreciated as I'm currently very confused.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload