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

Susan Sarandon
發布: 2024-10-19 18:13:01
原創
439 人瀏覽過

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中文網其他相關文章!

來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!