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>
Perl Solution
Similarly, adjust the Perl code to include:
<code class="perl">print "Content-Disposition: inline; filename=\"the.pdf\"\n";</code>
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');
Solved Issue: Loading Progress Bar
To display the loading progress bar in Adobe Reader X, add the following header:
header('Accept-Ranges: bytes');
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>
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!