PHP在浏览器窗口中嵌入了PDF

时间:2016-11-09 15:39:17

标签: php html pdf download

此代码在空白的浏览器窗口中打开PDF,没有额外的样式。

header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="'.basename($complete_path).'"');
header('Cache-Control: private, max-age=0, must-revalidate');
header('Pragma: public');
header('Content-Length: ' .filesize($complete_path));

$success = readfile($complete_path);

但是我想要应用更多信息和一些样式。所以我尝试了这段代码:

echo '<!DOCTYPE html>';
echo '<head>';
    echo '<meta charset="UTF-8">';
    echo '<title>'.$file_name.'</title>';
    echo '<link rel="stylesheet" type="text/css" href="css/docs.css" />';
echo '</head>';

echo '<body >';

    echo'<div class="frame">';
        $success = readfile($complete_path);
    echo'</div>';    

echo '</body>';

显然这失败了,这是一个快速的尝试。那么如何在样式化的浏览器页面中嵌入PDF文件呢?我不想在磁盘上下载它。

1 个答案:

答案 0 :(得分:0)

要嵌入,您可以使用<embed>显示PDF而无需下载:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Embeded PDF</title>
  </head>
  <body>
    <embed src="MY_PDF_FILE.PDF" type="application/pdf" height="25%" width="25%">
   </body>             ▲
</html>

现在的PHP方式:

<?php
$pdf = "MY_PDF_FILE.PDF";        ◄
echo <<<BLOCK
  <!DOCTYPE html>
  <html>
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <title>Embeded PDF</title>
    </head>
    <body>
      <embed src="$pdf" type="application/pdf" height="25%" width="25%">
     </body>        ▲
  </html>
BLOCK;
?>