How to convert HTML to PDF using Dompdf library in PHP?

藏色散人
Release: 2023-04-05 14:42:02
Original
5681 people have browsed it

When we develop large-scale PHP applications, we usually need to generate some PDF files. In this tutorial, we will introduce you to an example of using the DomPDF library to convert html to pdf. With the DomPDF library, we can simply render html layouts into PDF files. Through the DomPDF library we can write external style sheets, inline style tags, font size, font color, etc. DomPDF will help customize PDF files.

How to convert HTML to PDF using Dompdf library in PHP?

So, today I want to share with you an example of how to use the DomPDF library to generate PDF files from HTML layout.

In this example, I will mainly do three things, as follows:

1) Set up the dompdf library

2) Create the index .php file

3) Create pdf_generate.php file

Step 1: Installation and Setup

In the first step, we have to download a library and two A dependent dompdf.

1) Dompdf: We can download the Dompdf library from GitHub, download link: https://github.com/dompdf/dompdf. After downloading extract it to the root folder and rename it to "dompdf".

2) php-font-lib: Now download php-font-lib from GitHub, download link: https://github.com/PhenX/php-font-lib. After downloading, extract it to the "dompdf/lib/" folder and rename it to "php-font-lib".

3) php-svg-lib: Finally download php-svg-lib from GitHub, download link: https://github.com/PhenX/php-svg-lib. After downloading, extract it to the "dompdf/lib/" folder and rename it to "php-svg-lib".

Step 2: Create the index.php file

In this step, I will create the index.php file in the root directory, in In this file, I created a simple form using bootstrap.

The code is as follows:

index . php

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>PHP使用Dompdf库将HTML文件转换为PDF</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>

<div class="container">
    <h2>生成PDF的信息表单</h2>
    <form action="pdf_generate.php" method="POST">
        <div class="form-group">
            <label>名称:</label>
            <input type="text" name="name" class="form-control" placeholder="输入名称" required>
        </div>
        <div class="form-group">
            <label>Email:</label>
            <input type="email" name="email" class="form-control" placeholder="输入Email" required>
        </div>
        <div class="form-group">
            <label>网址:</label>
            <input type="url" name="url" class="form-control" placeholder="输入URL" required>
        </div>
        <div class="form-group">
            <label>内容:</label>
            <textarea name="say" class="form-control" placeholder="输入内容"></textarea>
        </div>
        <div class="form-group">
            <button class="btn btn-success">生成PDF</button>
        </div>
    </form>
</div>

</body>
</html>
Copy after login

The rendering is as follows:

How to convert HTML to PDF using Dompdf library in PHP? Step 3: Create pdf_generate.php file

Here we will get the published data and generate a pdf file for download.

The code is as follows:

pdf_generate.php

<?php
require_once &#39;dompdf/autoload.inc.php&#39;;

/* 引用Dompdf命名空间*/
use Dompdf\Dompdf;

/* 实例化并使用dompdf类 */
$dompdf = new Dompdf();

$html = &#39;<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
		<h1>欢迎来到PHP中文网</h1>
		<table class="table table-bordered">
			<tr>
				<th colspan="2">信息表</th>
			</tr>
			<tr>
				<th>名称</th>
				<td>&#39;.$_POST[&#39;name&#39;].&#39;</td>
			</tr>
			<tr>
				<th>Email</th>
				<td>&#39;.$_POST[&#39;email&#39;].&#39;</td>
			</tr>
			<tr>
				<th>网址</th>
				<td>&#39;.$_POST[&#39;url&#39;].&#39;</td>
			</tr>
			<tr>
				<th>内容</th>
				<td>&#39;.nl2br($_POST[&#39;say&#39;]).&#39;</td>
			</tr>
		</table>&#39;;

$dompdf->loadHtml($html);

/* 将HTML呈现为PDF*/
$dompdf->render();

/*将生成的PDF输出到浏览器 */
$dompdf->stream();
Copy after login

Related recommendations: "PHP Tutorial"

OK, that’s it for PHP’s method of converting HTML files to PDF using the Dompdf library! Friends who need it can directly download the code in this article and test it locally. I hope it will be helpful to everyone!

The above is the detailed content of How to convert HTML to PDF using Dompdf library in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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