Best Practice to PHP Output PDF to Browser Directly – PHP Tutorial

By | August 10, 2019

To make browser show pdf file, we can:

Use html embed

<embed width="100%" height="600" src=""> </embed>

or Open a pdf url by browser.

However, these two methods of displaying pdf file is hard to make some control, for example, if we only want to valid user to read pdf, how to control? To do this, we need output pdf file by php script. In this tutorial, we will show you how to do.

php output pdf

Set charset

<meta charset="utf-8">

Set http response header

header("Content-type: application/pdf");

Ouptput pdf file

pdf = '1.pdf';
echo file_get_contents(pdf);

Then pdf file will be displayed in our browser, we can implment some control in this php page.

Category: PHP