首頁  >  文章  >  後端開發  >  如何使用 PHP 或 Perl 在瀏覽器中顯示具有點擊追蹤和位置隱藏功能的 PDF?

如何使用 PHP 或 Perl 在瀏覽器中顯示具有點擊追蹤和位置隱藏功能的 PDF?

Susan Sarandon
Susan Sarandon原創
2024-10-19 18:13:01441瀏覽

How to Display PDFs in Browsers with Click Tracking and Location Concealment Using PHP or Perl?

使用PHP 或Perl 在使用者瀏覽器中顯示PDF 檔案

問題:使用者需要查看PDF 的能力

解決方案:

PHP 和Perl 都提供了直接渲染PDF 文件的方法在瀏覽器中。以下是所涉及的基本步驟:

PHP:

<code class="php">header('Content-type: application/pdf');
readfile('the.pdf');</code>

Perl:

<code class="perl">open(PDF, "the.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:

    Perl:
  • 其他注意事項:

要將PDF 嵌入頁面中,請將Content-Disposition 標頭設定為內聯; filename="the.pdf"。

確保使用者安裝了必要的 PDF 閱讀器外掛程式(例如 Adob​​e Reader)。

要停用載入進度欄,請停用 Accept-Ranges:位元組標頭。

<code class="php">$file = './path/to/the.pdf';
$filename = 'Custom file name for the.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>

範例程式碼:

<code class="perl">use strict;
use warnings;

my $file = 'the.pdf';
my $filename = 'Custom file name for the.pdf';

open(PDF, "<$file>") or die "Could not open PDF: $!";
binmode PDF;

my $size = -s PDF;

print "Content-type: application/pdf\n";
print "Content-Disposition: inline; filename=\"$filename\"\n";
print "Content-Transfer-Encoding: binary\n";
print "Content-Length: $size\n\n";

print while <PDF>;</code>
PHP(完整):

):注意:瀏覽器設定可能會覆蓋這些技術並強制PDF 下載或在外部應用程式中開啟。

以上是如何使用 PHP 或 Perl 在瀏覽器中顯示具有點擊追蹤和位置隱藏功能的 PDF?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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