Home > Backend Development > PHP Tutorial > How to Display PDF Files in User Browsers with PHP and Perl?

How to Display PDF Files in User Browsers with PHP and Perl?

DDD
Release: 2024-10-19 18:13:30
Original
581 people have browsed it

How to Display PDF Files in User Browsers with PHP and Perl?

Displaying PDF Files in User Browsers via PHP/Perl

This question addresses the need to display PDF files within user browsers, enabling click tracking and concealing the PDF's actual location. Existing PHP and Perl solutions have been found useful in creating PDFs and triggering save dialogues, but not for direct display.

PHP Solution

To correctly display the PDF in a browser, make the following adjustments to your code:

<code class="php">header('Content-Disposition: inline; filename="the.pdf"');</code>
Copy after login

Perl Solution

Similarly, adjust the Perl code to include:

<code class="perl">print "Content-Disposition: inline; filename=\"the.pdf\"\n";</code>
Copy after login

Additional Considerations

Some browsers automatically download or open PDFs in external applications. To prevent this, the following header can be added to both the PHP and Perl solutions:

header('Content-Transfer-Encoding: binary');
Copy after login

Solved Issue: Loading Progress Bar

To display the loading progress bar in Adobe Reader X, add the following header:

header('Accept-Ranges: bytes');
Copy after login

Solved Issue: Final Code

The final, fully resolved PHP code is as follows:

<code class="php">$file = './path/to/the.pdf';
$filename = 'Custom file name for the.pdf'; /* Note: Always use .pdf at the end. */

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>
Copy after login

This updated code ensures that PDF files are displayed correctly in user browsers, with click tracking and URL masking as desired.

The above is the detailed content of How to Display PDF Files in User Browsers with PHP and Perl?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template