在浏览器中查看PDF而不是下载

时间:2013-11-05 23:05:42

标签: php cakephp pdf

本网站上有会议,每个会议都附有PDF,显示会议纪要。下一次会议动态收集此附加PDF并允许下载前几分钟。这一切都很完美,但是,当点击前几分钟的链接时,它总是打开一个保存为框。我想知道如何让它只在浏览器中显示(假设用户启用了PDF查看器)?

以下是控制器的功能:

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));
}

}

以下是视图中的链接:

<?php echo $this->Html->link(__('Download Previous Minutes'), array('controller' => 'meetings', 'action' => 'current_pdf', $download, 'current')); ?>

我尝试删除标题(“Content-Type ...”)代码行并将header("Content-Disposition: attachment; filename=" . urlencode($file));中的附件更改为内联。这会将保存停止为弹出窗口,但会在浏览器中显示一堆虚假代码。

任何帮助都会非常感激,因为我现在非常困惑。

1 个答案:

答案 0 :(得分:1)

我相信这就像在代码中删除这一行一样简单:

header("Content-Disposition: attachment; filename=" . urlencode($file));

此行将作为下载服务器提升文件,而不是在浏览器中显示内容。

我希望有所帮助!