gpt4 book ai didi

php - 如何显示在 php 中使用 file_get_contents 检索到的 pdf?

转载 作者:行者123 更新时间:2023-12-05 09:16:21 24 4
gpt4 key购买 nike

我想显示嵌入在 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;

否则,另一种更大文件的解决方案(由于缓冲读取/输出而不将内容存储在 RAM 中,在 RAM 上更容易)使用 readfile

<?php
header("Content-Type: application/pdf");
readfile("../unreachable_file_outside_webserver.pdf");

关于php - 如何显示在 php 中使用 file_get_contents 检索到的 pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50736205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com