>  기사  >  백엔드 개발  >  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를 볼 수 있는 기능이 필요함 클릭 수를 추적하고 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>

추가 고려 사항:

  • 페이지 내에 PDF를 포함하려면 Content-Disposition 헤더를 인라인으로 설정하세요. filename="the.pdf".
  • 사용자에게 필요한 PDF 리더 플러그인(예: Adobe Reader)이 설치되어 있는지 확인하세요.
  • 로딩 진행률 표시줄을 비활성화하려면 Accept-Ranges를 비활성화하세요. 바이트 헤더.

예제 코드:

PHP(완전):

<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>

Perl(전체):

<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>

참고: 브라우저 설정은 이러한 기술을 무시하고 강제로 PDF를 다운로드하거나 외부 응용 프로그램에서 열도록 할 수 있습니다.

위 내용은 PHP 또는 Perl을 사용하여 클릭 추적 및 위치 숨김 기능을 갖춘 브라우저에 PDF를 표시하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.