首页  >  文章  >  后端开发  >  如何使用 PHP 和 Perl 在浏览器中显示 PDF 文件:综合指南

如何使用 PHP 和 Perl 在浏览器中显示 PDF 文件:综合指南

Susan Sarandon
Susan Sarandon原创
2024-10-19 18:18:02628浏览

How to Display PDF Files in the Browser with PHP and Perl: A Comprehensive Guide

使用 PHP 和 Perl 在浏览器中显示 PDF 文件

背景:

为了跟踪点击并隐藏由于 PDF 文档的真实位置,您需要一种在用户浏览器中显示 PDF 文件的解决方案。尽管进行了大量的互联网搜索,但您还没有找到直接的方法。

使用 PHP 的解决方案:

<code class="php">header('Content-type: application/pdf');
header('Content-Disposition: inline; filename=example.pdf');
@readfile('path/to/example.pdf');</code>

使用 Perl 的解决方案:

<code class="perl">open(PDF, 'path/to/example.pdf') or die "Could not open PDF [$!]";
binmode PDF;
my $output = do { local $/; <PDF> };
close(PDF);

print "Content-Type: application/pdf\n";
print "Content-Length: " . length($output) . "\n\n";
print $output;</code>

疑难解答:

  • Adobe Reader X 中缺少加载进度条: 确保“内容传输编码:二进制” ' 标头已设置。
  • 其他浏览器注意事项:浏览器可能具有强制 PDF 下载或在外部应用程序中打开它们的设置。

最终的 PHP代码:

<code class="php">$file = './path/to/example.pdf';
$filename = 'Custom file name for example.pdf';

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');

@readfile($file);</code>

此代码应允许在用户浏览器中显示 PDF 文件,同时允许您跟踪点击并保持所需的隐私级别。

以上是如何使用 PHP 和 Perl 在浏览器中显示 PDF 文件:综合指南的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn