本文概述
如今, 在任何应用程序上都必须生成PDF文件。尽管有很多库可以让你从PHP中的HTML生成PDF, 但是dompdf无疑是开发人员的最爱库之一, 因为它在使用这种语言创建PDF时既简单又有效。
Dompdf是用PHP编写的CSS 2.1兼容HTML布局和呈现引擎。它是样式驱动的渲染器:它将下载和读取外部样式表, 内联样式标签以及单个HTML元素的样式属性。它还支持大多数演示HTML属性。
在本文中, 我们将向你展示如何在基于Symfony 4的项目中使用Dompdf库从HTML生成PDF文件。
要求
在本教程中, 我们将使用Symfony 4的传统Web应用程序版本, 其中包括Twig, Doctrine等。我们将基于symfony控制器编写示例并生成PDF。我们的示例HTML将存储在/project/templates/default/mypdf.html.twig的Twig文件中, 并包含以下将显示为PDF的HTML:
{# ./templates/default/mypdf.html.twig #}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the PDF</title>
</head>
<body>
<h4>{{ title }}</h4>
<p>Lorem Ipsum</p>
</body>
</html>
我们将从控制器发送一个变量, 即title。知道了这一点, 让我们开始吧!
1.安装Dompdf
处理Symfony 4中依赖性的首选方法是通过Composer。在packagist网站上有一个dompdf软件包。你可以使用以下命令在你的Symfony项目中安装Dompdf库:
composer require dompdf/dompdf
安装后, 你将能够使用该库及其名称空间。有关该库的更多信息, 请访问其在Github上的官方存储库或在此处的官方网站。
2.通过创建PDF文件
作为开发人员, 你将边做边学, 这意味着编写并运行一些代码!我们将向你展示一些你经常需要的传统示例, 这种情况是在磁盘中创建PDF文件(在路径中保存excel文件)和流式响应(用于查看PDF的内联处置文件)在浏览器中)或强制下载pdf文件(附件):
A.生成并强制PDF文件下载
如果你愿意创建PDF文件并将其作为文件使用, 以便用户下载, 则无需将其写入磁盘。你可以改为发送响应:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
// Include Dompdf required namespaces
use Dompdf\Dompdf;
use Dompdf\Options;
class DefaultController extends Controller
{
public function index()
{
// Configure Dompdf according to your needs
$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
// Instantiate Dompdf with our options
$dompdf = new Dompdf($pdfOptions);
// Retrieve the HTML generated in our twig file
$html = $this->renderView('default/mypdf.html.twig', [
'title' => "Welcome to our PDF Test"
]);
// Load HTML to Dompdf
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser (force download)
$dompdf->stream("mypdf.pdf", [
"Attachment" => true
]);
}
}
B.在浏览器中生成并查看PDF
如果要在浏览器中直接查看生成的PDF, 只需在流期间将附件选项设置为false:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
// Include Dompdf required namespaces
use Dompdf\Dompdf;
use Dompdf\Options;
class DefaultController extends Controller
{
public function index()
{
// Configure Dompdf according to your needs
$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
// Instantiate Dompdf with our options
$dompdf = new Dompdf($pdfOptions);
// Retrieve the HTML generated in our twig file
$html = $this->renderView('default/mypdf.html.twig', [
'title' => "Welcome to our PDF Test"
]);
// Load HTML to Dompdf
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser (inline view)
$dompdf->stream("mypdf.pdf", [
"Attachment" => false
]);
}
}
C.生成PDF并将其存储在磁盘中
如果要将PDF保存在存储器中, 只需检索PDF的二进制数据并将其保存到文件中, 就可以使用file_put_contents进行操作:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
// Include Dompdf required namespaces
use Dompdf\Dompdf;
use Dompdf\Options;
class DefaultController extends Controller
{
public function index()
{
// Configure Dompdf according to your needs
$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
// Instantiate Dompdf with our options
$dompdf = new Dompdf($pdfOptions);
// Retrieve the HTML generated in our twig file
$html = $this->renderView('default/mypdf.html.twig', [
'title' => "Welcome to our PDF Test"
]);
// Load HTML to Dompdf
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Store PDF Binary Data
$output = $dompdf->output();
// In this case, we want to write the file in the public directory
$publicDirectory = $this->get('kernel')->getProjectDir() . '/public';
// e.g /var/www/project/public/mypdf.pdf
$pdfFilepath = $publicDirectory . '/mypdf.pdf';
// Write file to the desired path
file_put_contents($pdfFilepath, $output);
// Send some text response
return new Response("The PDF file has been succesfully generated !");
}
}
编码愉快!
评论前必须登录!
注册