HTML 转 PDF,用 PHP 快速实现

当我们开发大型PHP应用程序时,通常需要生成一些PDF文件。

通过DomPDF库,我们可以简单地将html布局呈现为PDF文件。通过DomPDF库我们可以编写外部样式表、内联样式标签、字体大小、字体颜色等。DomPDF将帮助定制PDF文件。

步骤1:安装和设置

在第一步,我们必须下载一个库和两个依赖的dompdf。
1) Dompdf:我们可以从GitHub下载Dompdf库,下载链接:
https://github.com/dompdf/dompdf
下载后将其解压缩到根文件夹并将其重命名为“dompdf”。
2) php-font-lib:现在从GitHub下载php-font-lib,下载链接:
https://github.com/PhenX/php-font-lib
下载后解压到“dompdf/lib/”文件夹,并将其重命名为“php-font-lib”。

3) php-svg-lib:最后从GitHub下载php-svg-lib,下载链接:
https://github.com/PhenX/php-svg-lib
下载后解压到“dompdf/lib/”文件夹,并将其重命名为“php-svg-lib”。

步骤2:创建index.php文件

在这个步骤中,我将在根目录中创建index.php文件,在这个文件中,我使用bootstrap创建了一个简单的表单。
eg: 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>

步骤3:创建pdf_generate.php文件

这里我们将获取发布的数据并生成pdf文件下载。
eg :pdf_generate.php

<?php
require_once 'dompdf/autoload.inc.php';

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

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

$html = '<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>'.$_POST['name'].'</td>
            </tr>
            <tr>
                <th>Email</th>
                <td>'.$_POST['email'].'</td>
            </tr>
            <tr>
                <th>网址</th>
                <td>'.$_POST['url'].'</td>
            </tr>
            <tr>
                <th>内容</th>
                <td>'.nl2br($_POST['say']).'</td>
            </tr>
        </table>';

$dompdf->loadHtml($html);

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

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

OK,PHP就可以使用Dompdf库将HTML文件转换为PDF

本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 3

这个貌似性能不太行,HTML如果很长,内容很多,这个转化太慢了! DOMPDF

3年前 评论

中文乱码问题可以解决么

1年前 评论
Shine-x (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!