search
Create PDF in PHPFeb 28, 2024 am 10:16 AM
php programmingBackend Development

Creating PDF in PHP is a very useful skill that can help website developers generate various types of dynamic PDF files such as invoices, reports, certificates, etc. By using libraries and extensions in PHP, developers can easily convert data into PDF format and make it available on the website for users to download or print. This article will explain how to create PDF files in PHP using common libraries and extensions, as well as some practical tips and considerations.


Create PDF using mpdf library in php

We can create PDF in PHP using external library mpdf. We can retrieve data from the database, store them in a PDF, and then download the PDF. Using this library we can create PDF from html documents. HTML documents should be encoded in UTF-8. We can retrieve the data to be added to the PDF from the database in HTML format. We can use the library by downloading it from the project directory via the command composer require mpdf/mpd. This command will install the mpdf library in the project directory. A vendor file will be created and we need to use the require() function to include the file autoload.php located inside the vendor folder. We have to make sure that the directory where the library is installed should have write permission.

We will create an object from the Mpdf() constructor and use methods like WriteHTML() and output() to create the PDF. We can output PDF in different modes. We can specify the mode in the second parameter of the output() method. The different modes are represented by the D, I, F, and S options. Option D will force a download of the PDF after the script is run. After the script runs, option I will display the PDF in the browser. Also, option F will save the downloaded PDF in a folder relative to the PHP file. Finally, option F will output a pdf in the browser only if the output() method is assigned to a variable.

For example, we have a database named phprow which contains a table named Persons. Table Persons contains the following data.

<code>
<code class="language-sql hljs" data-lang="sql"><span style="display:flex;"><span><span style="color:#666">+</span><span style="color:#408080;font-style:italic">----------+----------+-----------+
</span></span></span><span style="display:flex;"><span><span style="color:#666">|</span><span style="color:#bbb"> </span>PersonID<span style="color:#bbb"> </span><span style="color:#666">|</span><span style="color:#bbb"> </span>Name<span style="color:#bbb"> </span><span style="color:#666">|</span><span style="color:#bbb"> </span>Address<span style="color:#bbb"> </span><span style="color:#666">|</span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#666">+</span><span style="color:#408080;font-style:italic">----------+----------+-----------+
</span></span></span><span style="display:flex;"><span><span style="color:#666">|</span><span style="color:#bbb"> </span><span style="color:#666">22</span><span style="color:#bbb"> </span><span style="color:#666">|</span><span style="color:#bbb"> </span>Harry<span style="color:#bbb"> </span>M<span style="color:#bbb"></span><span style="color:#666">|</span><span style="color:#bbb"> </span>England<span style="color:#bbb"> </span><span style="color:#666">|</span><span style="color:#bbb">
</span></span></span><span style="display:flex;"><span><span style="color:#666">|</span><span style="color:#bbb"> </span><span style="color:#666">32</span><span style="color:#bbb"> </span><span style="color:#666">|</span><span style="color:#bbb"> </span>Paul<span style="color:#bbb"> </span>P<span style="color:#bbb"> </span><span style="color:#666">|</span><span style="color:#bbb"> </span>France<span style="color:#bbb"></span><span style="color:#666">|</span><span style="color:#bbb"> 
</span></span></span><span style="display:flex;"><span><span style="color:#666">+</span><span style="color:#408080;font-style:italic">----------+----------+-----------+
</span></span></span></code></code>

First, include the vender/autoload.php file using the require() function. Then, create and establish a database connection, run a sql query to select data from the database, and create a table in the $html variable. Use the . operator to join the $html variable with the body of the table. Create a table with headers ID, Name, and Address. The table is then populated by retrieving the above data from the Persons table.

Sample code:

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">require</span>(<span style="color:#ba2121">'vendor/autoload.php'</span>);
</span></span><span style="display:flex;"><span><span style="color:#19177c">$con</span><span style="color:#666">=</span><strong class="keylink">Mysql</strong>i_connect(<span style="color:#ba2121">'localhost'</span>,<span style="color:#ba2121">'root'</span>,<span style="color:#ba2121">''</span>,<span style="color:#ba2121">'phprow'</span>);
</span></span><span style="display:flex;"><span><span style="color:#19177c">$res</span><span style="color:#666">=</span><strong class="keylink">mysql</strong>i_query(<span style="color:#19177c">$con</span>,<span style="color:#ba2121">"select * from Persons"</span>);
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">if</span>(mysqli_num_rows(<span style="color:#19177c">$res</span>)<span style="color:#666">></span><span style="color:#666">0</span>){
</span></span><span style="display:flex;"><span><span style="color:#19177c">$html</span><span style="color:#666">=</span><span style="color:#ba2121">'<table>';
<span style="display:flex;"><span><span style="color:#19177c">$html</span><span style="color:#666">.=</span><span style="color:#ba2121">'<tr>
<td>ID</td>
<td>Name</td>
<td>Address</td>';
<span style="display:flex;"><span><span style="color:#008000;font-weight:bold">while</span>(<span style="color:#19177c">$row</span><span style="color:#666">=</span>mysqli_fetch_assoc(<span style="color:#19177c">$res</span>)){
</span></span><span style="display:flex;"><span><span style="color:#19177c">$html</span><span style="color:#666">.=</span><span style="color:#ba2121">'<tr>
<td>'<span style="color:#666">.</span><span style="color:#19177c">$row</span>[<span style="color:#ba2121">'PersonID'</span>]<span style="color:#666">.</span><span style="color:#ba2121">'</span>
</td>
<td>'<span style="color:#666">.</span><span style="color:#19177c">$row</span>[<span style="color:#ba2121">'Name'</span>]<span style="color:#666">.</span><span style="color:#ba2121">'</span>
</td>
<td>'<span style="color:#666">.</span><span style="color:#19177c">$row</span>[<span style="color:#ba2121">'Address'</span>]<span style="color:#666">.</span><span style="color:#ba2121">'</span>
</td>
</tr>'</span>;
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#19177c">$html</span><span style="color:#666">.=</span><span style="color:#ba2121">'</span></span></span>
</tr></span></span></span>
</table>'</span>;
</span></span><span style="display:flex;"><span>}
</span></span></code></code>

In the above example, we are storing data from the database in the variable $html. We used the . operator to join all table elements. So, we are ready to write HTML document to PDF.

Next, create a variable named $mpdf. Assign the object of the Mpdf() constructor to a variable using the new keyword. Call the WriteHTML() function using the $html variable as the parameter of the object. Then create another variable $file to store the PDF. Concatenate files/ with the time() function and concatenate it again with .pdf to create the filename. Store it in the $file variable. Finally, call the output() function with $file as the first argument and option I as the second argument.

Therefore, we retrieved the data from the database and created a PDF using the data. The following example creates a file named with the current time and with the extension .pdf in the files folder. After the script runs, the PDF will be displayed in the browser. We can download the PDF from the browser.

Sample code:

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#19177c">$mpdf</span><span style="color:#666">=</span><span style="color:#008000;font-weight:bold">new</span> \Mpdf\Mpdf();
</span></span><span style="display:flex;"><span><span style="color:#19177c">$mpdf</span><span style="color:#666">-></span><span style="color:#7d9029">WriteHTML</span>(<span style="color:#19177c">$html</span>);
</span></span><span style="display:flex;"><span><span style="color:#19177c">$file</span><span style="color:#666">=</span><span style="color:#ba2121">'files/'</span><span style="color:#666">.</span>time()<span style="color:#666">.</span><span style="color:#ba2121">'.pdf'</span>;
</span></span><span style="display:flex;"><span><span style="color:#19177c">$mpdf</span><span style="color:#666">-></span><span style="color:#7d9029">output</span>(<span style="color:#19177c">$file</span>,<span style="color:#ba2121">'I'</span>);
</span></span></code></code>

Create PDF in PHP using the dompdf

library

dompdf library is also an option for creating and downloading PDFs in PHP. It lets us load HTML into PDF. This library is very similar to the mpdf library; just the approach is different. We will use methods like loadHtml(), render() and stream(). We need to download the library to our working directory using the command composer require dompdf/dompdf. It will create the vendor folder and the composer.<strong class="keylink">JSON</strong> and composer.lock files just like the first method.

例如,要求 vendor/autoload.php 作为程序中代码的第一行。然后编写 use 关键字以将 Dompdf 类导入为 use Dompdf/Dompdf。我们可以使用与上述方法相同的 HTML 表来加载 PDF。

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">require</span> <span style="color:#ba2121">'vendor/autoload.php'</span>;
</span></span><span style="display:flex;"><span><span style="color:#008000;font-weight:bold">use</span> Dompdf\Dompdf;
</span></span></code></code>

将 HTML 存储在变量 $html 中后,创建另一个变量 $dompdf 以创建类 Dompdf 的对象。然后使用 $html 作为参数调用 loadHtml() 方法。接下来,调用 render() 函数,然后使用 $dompdf 对象调用 stream() 函数。

下面的示例将使用第一种方法中的表格创建 PDF。render() 方法将 HTML 呈现为 PDF 文件,而 stream() 方法将呈现的 HTML 输出到浏览器。因此,我们可以使用 PHP 中的 dompdf 库创建 PDF。

示例代码:

<code>
<code class="language-php hljs" data-lang="php"><span style="display:flex;"><span><span style="color:#19177c">$dompdf</span> <span style="color:#666">=</span> <span style="color:#008000;font-weight:bold">new</span> Dompdf();
</span></span><span style="display:flex;"><span><span style="color:#19177c">$dompdf</span><span style="color:#666">-></span><span style="color:#7d9029">loadHtml</span>(<span style="color:#19177c">$html</span>);
</span></span><span style="display:flex;"><span><span style="color:#19177c">$dompdf</span><span style="color:#666">-></span><span style="color:#7d9029">render</span>();
</span></span><span style="display:flex;"><span><span style="color:#19177c">$dompdf</span><span style="color:#666">-></span><span style="color:#7d9029">stream</span>();
</span></span></code></code>

The above is the detailed content of Create PDF in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:编程网. If there is any infringement, please contact admin@php.cn delete
如何在PHP中实现SEO优化如何在PHP中实现SEO优化May 20, 2023 pm 01:30 PM

随着互联网的发展,SEO(SearchEngineOptimization,搜索引擎优化)已经成为了网站优化的重要一环。如果您想要使您的PHP网站在搜索引擎中获得更高的排名,就需要对SEO的内容有一定的了解了。本文将会介绍如何在PHP中实现SEO优化,内容包括网站结构优化、网页内容优化、外部链接优化,以及其他相关的优化技巧。一、网站结构优化网站结构对于S

如何在PHP中实现多语言网站如何在PHP中实现多语言网站May 22, 2023 am 11:31 AM

随着互联网的日益普及,越来越多的网站需要支持多语言。这是因为网站的受众群体可能来自不同的地区和文化背景,如果只提供单一语言的网站,可能会限制访问者的数量和体验。本文将介绍如何在PHP中实现多语言网站。一、语言文件的创建和设计语言文件是存储所有文本字符串及其对应翻译的文件,需要以特定的格式创建。在创建语言文件时,需要考虑以下几个方面:1.命名和存储位置文件名应

PHP中的加密和解密技术PHP中的加密和解密技术May 11, 2023 am 08:03 AM

PHP是一种被广泛应用的Web开发语言,其加密和解密技术在数据安全性方面具有重要意义。本文将介绍PHP中的加密和解密技术,并探讨其在Web应用程序中的实际应用。一、加密技术加密技术是一种将普通文本转换为加密文本的过程。在PHP中,加密技术主要应用于传输数据的安全性,例如用户的登录信息、交易数据等。PHP中常见的加密技术如下:哈希加密哈希加密是将一个任意长度的

如何在PHP中实现ERP系统如何在PHP中实现ERP系统May 20, 2023 pm 06:21 PM

随着电子商务和企业管理的发展,许多企业开始寻找更好的方法来处理其日常业务流程。ERP系统是一种能够整合企业各种业务流程的软件工具。它提供了全面的功能,包括生产、销售、采购、库存、财务等方面,帮助企业提高效率、控制成本和提高客户满意度。而在PHP编程语言中,也能够实现ERP系统,这就需要我们掌握一些基本的知识和技术。下面,我们将深入探讨如何在PHP中实现ERP

PHP中的即时通讯技术指南PHP中的即时通讯技术指南May 22, 2023 pm 12:31 PM

近年来,随着互联网技术的不断发展,即时通讯技术成为了各个领域中不可或缺的一部分,而在Web开发中,PHP作为一种广泛应用的服务器端脚本语言,也开始探索并应用即时通讯技术。本文将围绕PHP中的即时通讯技术,从通讯协议、技术方案、应用场景三个方面进行介绍和指南。一、通讯协议HTTP协议HTTP协议是Web开发中最常用的协议之一,适用于上传、下载、浏览网站等场景。

如何在PHP中使用闭包函数如何在PHP中使用闭包函数May 18, 2023 pm 05:30 PM

PHP闭包函数是指在声明函数时所定义的函数体内部所使用的变量和外部环境中的变量形成一个封闭的作用域,这种函数又被称为匿名函数。闭包函数在PHP中被广泛应用,可以用于实现事件处理、回调等一系列功能。本文将介绍如何在PHP中使用闭包函数,以及一些使用闭包函数的最佳实践。一、如何定义一个闭包函数定义一个闭包函数非常简单,只需要使用函数关键字followedby

如何在PHP中使用机器人函数如何在PHP中使用机器人函数May 18, 2023 pm 10:00 PM

最近,随着人工智能技术的快速发展,机器人技术也逐渐得到了广泛的应用,其中,机器人函数成为了PHP编程语言中一个非常实用的工具。本文将介绍如何在PHP中使用机器人函数。什么是机器人函数机器人函数指在PHP编程语言中用于模拟机器人行为的一组函数。这些函数包括move()、turn()等,可以让我们编写出模拟机器人运动、转向等相关操作的代码。在实际应用中,机器人函

PHP实现数据库集群异常处理的方法PHP实现数据库集群异常处理的方法May 15, 2023 pm 02:40 PM

随着互联网的不断发展,越来越多的企业和组织开始规划数据库集群来满足其数据处理需求。数据库集群可能包含数百甚至数千个节点,因此在节点之间确保数据同步和协调非常重要。在该环境下,存在着很多的异常情况,如单节点故障,网络分区,数据同步错误等,并且需要实现实时检测和处理。本文将介绍如何使用PHP实现数据库集群异常处理。数据库集群的概述在数据库集群中,一个单独的

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment