下载并安装 Dompdf
- 从 GitHub 下载 dompdf(稳定版)的存档版本。
https://github.com/dompdf/dompdf/releases
提取 Dompdf 包并将其放置在应用程序目录中。
注意:不需要单独下载Dompdf库,所有需要的文件都包含在源代码中。
- 包括自动加载器以在 PHP 脚本中加载 dompdf 库和辅助函数。
//Include autoloader require_once 'dompdf/autoload.inc.php';
水印图像配置
我们可以更改 PDF 中水印图像的位置、宽度、高度和分辨率。
查看 Canvas 类的 'image()' 函数的可用选项。
/** * Add an image to the pdf. * * The image is placed at the specified x and y coordinates with the * given width and height. * * @param string $img_url the path to the image * @param float $x x position * @param float $y y position * @param int $w width (in pixels) * @param int $h height (in pixels) * @param string $resolution The resolution of the image */ image($img_url, $x, $y, $w, $h, $resolution = "normal");
将水印添加到 PDF(图像)
以下示例代码使用 PHP 生成 PDF 并将水印图像添加到带有 Dompdf 库的 PDF 文件中。
除了图像设置之外,该过程与文本水印代码(上图)相同。
- 指定要添加水印的图像路径。
- 使用 Canvas 类的 image() 方法将水印图像添加到 PDF 文档。
//Reference the Dompdf namespace use Dompdf\Dompdf; //Reference the Options namespace use Dompdf\Options; //Set options to enable embedded PHP $options = new Options(); $options->set('isPhpEnabled', 'true'); //Instantiate dompdf class $dompdf = new Dompdf($options); //Load HTML content $dompdf->loadHtml('<h1>Welcome to onitroad.com</h1>'); //(Optional) Setup the paper size and orientation $dompdf->setPaper('A4', 'landscape'); //Render the HTML as PDF $dompdf->render(); //Instantiate canvas instance $canvas = $dompdf->getCanvas(); //Get height and width of page $w = $canvas->get_width(); $h = $canvas->get_height(); //Specify watermark image $imageURL = 'images/onitroad-logo.png'; $imgWidth = 200; $imgHeight = 20; //Set image opacity $canvas->set_opacity(.5); //Specify horizontal and vertical position $x = (($w-$imgWidth)/2); $y = (($h-$imgHeight)/2); //Add an image to the pdf $canvas->image($imageURL, $x, $y, $imgWidth, $imgHeight); //Output the generated PDF (1 = download and 0 = preview) $dompdf->stream('document.pdf', array("Attachment" => 0));
水印文字配置
我们可以更改 PDF 中水印文本的位置、字体大小、颜色、间距和旋转(角度)。
查看 Canvas 类的“text()”函数的可用选项。
/** * Writes text at the specified x and y coordinates. * * @param float $x * @param float $y * @param string $text the text to write * @param string $font the font file to use * @param float $size the font size, in points * @param array $color * @param float $word_space word spacing adjustment * @param float $char_space char spacing adjustment * @param float $angle angle */ text($x, $y, $text, $font, $size, $color = array(0, 0, 0), $word_space = 0.0, $char_space = 0.0, $angle = 0.0);
当我们希望允许用户下载 Web 应用程序文件中的文本或者 HTML 内容时,动态 PDF 生成非常有用。
在这种情况下,下载前需要将 HTML 内容转换为 PDF 文件。
我们可以使用 Dompdf 在 PHP 中轻松地将 HTML 转换为 PDF。
Dompdf 是使用 PHP 创建带有动态数据的 PDF 文档的最简单方法。
Dompdf 库有助于创建 PDF 文件并使用 PHP 将 HTML 内容添加到 PDF。
Dompdf 提供了各种配置选项来增强 PDF 创建功能。
将水印添加到 PDF 是其中最有用的功能之一。
在本教程中,我们将向我们展示如何使用 PHP 将 HTML 转换为 PDF 并使用 Dompdf 为 PDF 添加水印。
水印是可以出现在 PDF 文档内容前面或者后面的图像或者文本。
在示例代码中,我们将展示在 PHP 中使用 Dompdf 为 PDF 文档添加文本和图像水印的两种方法。
将水印添加到 PDF(文本)
以下示例代码使用 PHP 生成 PDF 并将水印文本添加到带有 Dompdf 库的 PDF 文件中。
- 使用 Dompdf、Options 和 FontMetrics 命名空间的引用。
- 设置 'isPhpEnabled' 选项以启用嵌入式 PHP。
- 实例化 dompdf 类。
- 添加 HTML 内容。
- 将 HTML 呈现为 PDF。
- 使用 Dompdf 类的 getCanvas() 方法实例化画布实例。
- 实例化字体度量类。
- 使用 Canvas 类的 get_width() 和 get_height() 获取页面高度和宽度。
- 使用 FontMetrics 类的 getFont() 方法获取族文件并指定文本字体。
- 指定要添加水印的文本。
- 使用 FontMetrics 类的 getFontHeight() 和 getTextWidth() 获取文本的高度和宽度。
- 指定文本的水平和垂直位置。
- 使用 Canvas 类的 text() 方法将水印文本写入 PDF 文档。
- 使用 Dompdf 类的 stream() 函数输出生成的 PDF。
//Reference the Dompdf namespace use Dompdf\Dompdf; //Reference the Options namespace use Dompdf\Options; //Reference the Font Metrics namespace use Dompdf\FontMetrics; //Set options to enable embedded PHP $options = new Options(); $options->set('isPhpEnabled', 'true'); //Instantiate dompdf class $dompdf = new Dompdf($options); //Load HTML content $dompdf->loadHtml('<h1>Welcome to onitroad.com</h1>'); //(Optional) Setup the paper size and orientation $dompdf->setPaper('A4', 'landscape'); //Render the HTML as PDF $dompdf->render(); //Instantiate canvas instance $canvas = $dompdf->getCanvas(); //Instantiate font metrics class $fontMetrics = new FontMetrics($canvas, $options); //Get height and width of page $w = $canvas->get_width(); $h = $canvas->get_height(); //Get font family file $font = $fontMetrics->getFont('times'); //Specify watermark text $text = "CONFIDENTIAL"; //Get height and width of text $txtHeight = $fontMetrics->getFontHeight($font, 75); $textWidth = $fontMetrics->getTextWidth($text, $font, 75); //Set text opacity $canvas->set_opacity(.2); //Specify horizontal and vertical position $x = (($w-$textWidth)/2); $y = (($h-$txtHeight)/2); //Writes text at the specified x and y coordinates $canvas->text($x, $y, $text, $font, 75); //Output the generated PDF (1 = download and 0 = preview) $dompdf->stream('document.pdf', array("Attachment" => 0));