Home Backend Development PHP Tutorial How to open a PDF file in a web browser using PHP?

How to open a PDF file in a web browser using PHP?

Mar 18, 2019 am 10:51 AM
pdf file php Browser

PHP uses standard code to display pdf files in web browsers. The process of displaying a PDF file involves the location of the PDF file on the server. It uses various types of header files to define the content composition in the form of type, configuration, transfer encoding, etc.

PHP passes the PDF file to read it on the browser. The browser either displays it or downloads it from the localhost server and then displays the pdf.

Note: PHP does not actually read PDF files. It does not recognize files in pdf format. It just passes the PDF file to the browser for reading in the browser. If you copy the pdf file into XAMPP's htdocs folder, you don't need to specify the file path.

Example 1:

Display pdf files on the browser.

<?php 
  
// 将文件名存储到变量中
$file = &#39;filename.pdf&#39;; 
$filename = &#39;filename.pdf&#39;; 
 
header(&#39;Content-type: application/pdf&#39;); 
  
header(&#39;Content-Disposition: inline; filename="&#39; . $filename . &#39;"&#39;); 
  
header(&#39;Content-Transfer-Encoding: binary&#39;); 
  
header(&#39;Accept-Ranges: bytes&#39;); 
  
// 读取文件
@readfile($file);
Copy after login

Output:

How to open a PDF file in a web browser using PHP?

Example 2:

<?php 
  
// PDF文件在服务器上的位置
$filename = "/path/to/the/file.pdf"; 
  
// Header content type 
header("Content-type: application/pdf"); 
  
header("Content-Length: " . filesize($filename)); 
  
// 将文件发送到浏览器。
readfile($filename);
Copy after login

Output:

How to open a PDF file in a web browser using PHP?

Recommended related video tutorials: "PHP Tutorial"

This article is about how to use PHP to open PDF files in a web browser. Simple and easy to understand, I hope it will be helpful to friends in need!

The above is the detailed content of How to open a PDF file in a web browser using PHP?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

Sesame Open Door Official Website Sesame Open Door App Latest Entering Website Sesame Open Door Official Website Sesame Open Door App Latest Entering Website Feb 28, 2025 am 11:18 AM

Sesame Open Door Official Website Sesame Open Door App Latest Entering Website

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

The latest version of Sesame Open Door Official Website Entrance gate.io Official Website Link Address Entrance The latest version of Sesame Open Door Official Website Entrance gate.io Official Website Link Address Entrance Feb 28, 2025 am 11:21 AM

The latest version of Sesame Open Door Official Website Entrance gate.io Official Website Link Address Entrance

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

PHP Program to Count Vowels in a String

gate.io sesame door download Chinese tutorial gate.io sesame door download Chinese tutorial Feb 28, 2025 am 10:54 AM

gate.io sesame door download Chinese tutorial

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

How do you parse and process HTML/XML in PHP?

Sesame Open Door Exchange official website gate.io exchange official website entrance Sesame Open Door Exchange official website gate.io exchange official website entrance Feb 28, 2025 am 10:45 AM

Sesame Open Door Exchange official website gate.io exchange official website entrance

See all articles