首页 > 解决方案 > 在 HTML 文件中使用 PHP 显示 PDF

问题描述

我想保护我的 PDF 的显示:首先为用户隐藏 PDF 的源链接,其次使 PDF 无法下载。
我在第一点,我用过:

<?php 
$file = "./src/file.pdf";
header("Content-type: application/pdf"); 
header("Content-Length: " . filesize($file)); 
readfile($file);
?>

这在没有任何 HTML 的页面上工作得很好,但如果我只添加:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>TEST</title>
</head>
<body>
  <div>
    <!-- Some elements -->
  </div>
  <div>
    <?php 
    $file = "./src/file.pdf";
    header("Content-type: application/pdf"); 
    header("Content-Length: " . filesize($file)); 
    readfile($file);
    ?>
  </div>
</body>
</html>

那根本不起作用
那么我该怎么办?因为我想要这个地方的 PDF,被 HTML 包围。

如果有人有解决方案,或者更多,隐藏PDF源链接的解决方案,其次使PDF下载变得不可能。

我非常感谢您的时间和耐心!

标签: phphtmlpdf

解决方案


正如 Ken Lee 指出的那样,调用header.

<?php
    $file = "./src/file.pdf";
    header("Content-type: application/pdf"); 
    header("Content-Length: " . filesize($file));
?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <title>TEST</title>
</head>
<body>
  <div>
    <!-- Some elements -->
  </div>
  <div>
    <?php readfile($file); ?>
  </div>
</body>
</html>

推荐阅读