我想显示嵌入在 HTML 中的 pdf 文件,该文件位于文档根目录中。
文档根指向 /var/www/web1/web 并且 pdf 文件在 /var//www/web1/docs/pdf 中,所以我无法从 html 中指向它。
我正在使用 PHP,我尝试了一些建议,如 get_file_contents、base64_encode 等,但没有奏效。
我找到的所有示例都是下载 pdf 或将其转换为 jpg,但这不是我需要的。
有什么建议?
只需通过 php 页面加载 pdf 内容,我们称之为viewer.php,然后嵌入 PHP 页面,因为它是文档本身:
<embed src="viewer.php" width="80%" height="900px" />
在 viewer.php 文件中:
<?php
//Load file content
$pdf_content = file_get_contents('../unreachable_file_outside_webserver.pdf');
//Specify that the content has PDF Mime Type
header("Content-Type: application/pdf");
//Display it
echo $pdf_content;
Run Code Online (Sandbox Code Playgroud)
否则,对于更大文件的另一种解决方案(由于缓冲读取/输出而不将内容存储在 RAM 中,因此在 RAM 上更容易)使用 readfile
<?php
header("Content-Type: application/pdf");
readfile("../unreachable_file_outside_webserver.pdf");
Run Code Online (Sandbox Code Playgroud)