首頁  >  文章  >  後端開發  >  如何使用 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>
使用Perl 的解:

  • 疑難排解:
Adobe Reader X 中缺少載入進度條:

確保「內容傳輸編碼:二進位」 ' 標頭已設定。

其他瀏覽器注意事項:
<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代碼:此程式碼應允許在使用者瀏覽器中顯示PDF 文件,同時允許您追蹤點擊並保持所需的隱私等級。

以上是如何使用 PHP 和 Perl 在瀏覽器中顯示 PDF 檔案:綜合指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn