php - 在 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下载变得不可能。
我非常感谢您的时间和耐心!
解决方案
正如 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>
推荐阅读
- php - 远程 SQL Server 配置:哪些端口?
- google-analytics - 谷歌分析返回许多“未定义”事件
- python - 在 python 中导入模块时控制命名空间
- selenium - selenium chromedriver - Twitter 检测到可疑活动
- rest - 如何在 ReadyAPI 中的两个 REST 请求之间保持相同的会话?
- sql-server - 无法读取数据集的下一个数据行
- git - 在您的个人资料中包含您为之做出重大贡献的 GitHub 存储库
- java - 整数的输出流编写器java
- django - TypeError:日期类型的对象不是 JSON 可序列化的
- java - 使用android资源时出现“表达式中的数值溢出”