filippo-toso / pdf-watermarker
支持Laravel的简单PDF水印工具
v1.1.1
2024-06-27 05:22 UTC
Requires
- php: >=7.1.0
- illuminate/support: >=7.0
- setasign/fpdf: ^1.8
- setasign/fpdi: ^2.3
README
PDFWatermarker 允许您将文本或图片作为水印添加到现有的PDF文件中。它使用 FPDF 允许您编写PDF文件,以及 FPDI 允许您将现有的PDF文档导入到FPDF中。
使用它,您可以
- 使用文本和TTF字体创建水印
- 使用96 DPI分辨率的jpg和png(带有alpha通道)图片作为水印
- 轻松地将水印定位在PDF文件的页面中
安装
使用Composer安装
composer require filippo-toso/pdf-watermarker
用法
在纯PHP中,您可以通过以下方式使用图片给PDF加水印
<?php use FilippoToso\PdfWatermarker\Support\Pdf; use FilippoToso\PdfWatermarker\Watermarks\ImageWatermark; use FilippoToso\PdfWatermarker\PdfWatermarker; // Specify path to the existing pdf $pdf = new Pdf('input.pdf'); // Specify path to image. The image must have a 96 DPI resolution. $watermark = new ImageWatermark('watermark.png'); // Create a new watermarker $watermarker = new PDFWatermarker($pdf, $watermark); // Save the new PDF to its specified location $watermarker->save('output.pdf');
选项
您也可以指定其他选项
use FilippoToso\PdfWatermarker\Support\Position; // Set the position of the watermark including optional X/Y offsets $position = new Position(Position::BOTTOM_CENTER, -50, -10); // All possible positions can be found in Position::options $watermarker->setPosition($position); // Place watermark behind original PDF content. Default behavior places it over the content. $watermarker->setAsBackground(); // Only Watermark specific range of pages // This would only watermark page 3 and 4 $watermarker->setPageRange(3, 4);
输出方法
您可以通过以下方式获取加水印的PDF
// The filename is optional for all output options $watermarker->save(); // Start a download of the PDF $watermarker->download('output.pdf'); // Send the PDF to standard out $watermarker->output('output.pdf');
Laravel支持
在Laravel中,您可以使用ImageWatermarker和TextWatermarker这两个门面
use FilippoToso\PdfWatermarker\Facades\ImageWatermarker; use FilippoToso\PdfWatermarker\Support\Position ImageWatermarker::input('input.pdf') ->watermark('watermark.png') ->output('laravel-image.pdf') ->position(Position::BOTTOM_CENTER, -50, -10) ->asBackground() ->pageRange(3, 4) ->resolution(300) // 300 dpi ->save();
use FilippoToso\PdfWatermarker\Facades\TextWatermarker; use FilippoToso\PdfWatermarker\Support\Position TextWatermarker::input('input.pdf') ->output('laravel-text.pdf') ->position(Position::BOTTOM_CENTER, -50, -10) ->asBackground() ->pageRange(3, 4) ->text('Hello World') ->angle(25) ->font('arial.ttf') ->size('25') ->color('#CC00007F') // 7F is the alpha channel (transparency or opacity of color) 0 - 255 in hex ->resolution(300) // 300 dpi ->save();
您还可以生成有效的响应以流式传输或下载文件
use FilippoToso\PdfWatermarker\Facades\ImageWatermarker; // Within a controller action return ImageWatermarker::input('input.pdf') ->watermark('watermark.png') ->download('example.pdf');
use FilippoToso\PdfWatermarker\Facades\ImageWatermarker; // Within a controller action return ImageWatermarker::input('input.pdf') ->watermark('watermark.png') ->stream('example.pdf');
鸣谢
此包基于以下存储库
- https://github.com/drinksandco/pdf-watermarker
- https://github.com/jeroenvdgulik/pdf-watermarker
- https://github.com/binarystash/pdf-watermarker
我决定重构并增强它,因为这些包已经多年没有更新了。