tarfin-labs/easy-pdf

简化PDF处理。

v2.10.0 2024-04-08 13:26 UTC

README

Latest Version on Packagist GitHub Workflow Status Quality Score Total Downloads

简介

easy-pdf 是 Laravel 6.x, 7.x, 8.x, 9.x, 10.x 的 tcpdf 包装器。

安装

您可以通过 composer 安装此包。

composer require tarfin-labs/easy-pdf

用法

使用 HTML 创建 PDF。

您可以使用 HTML 创建 PDF。Easy-pdf 还提供了与 tcpdf PDF 设置和信息的简单配置。

$pdf = EasyPdf::withInformation([
    'Creator'   => 'Tarfin',
    'Author'    => 'Faruk Can',
    'Title'     => 'EasyPdf',
    'Keywords'  => 'easy, pdf',
    'AutoPageBreak' => [true, 0],
])
    ->withConfig([
        'ImageScale' => PDF_IMAGE_SCALE_RATIO,
])
    ->setFont('times', 16) // use default fonts
    ->loadHtml($html) // each load html creates a new page
    ->content(); // return pdf content as a string

这将返回一个字符串作为 PDF 内容。如果您想保存 PDF,请使用 save 方法。

// This will save pdf to given path.
$pdf->save($filePath);

您还可以使用 stream 方法直接将 PDF 流式传输到浏览器。

// This will stream pdf to the directly browser.
$pdf->stream();

您可以使用 addFont 添加自定义 TTF 字体或使用默认字体。

// Add custom font using font path
$pdf->addFont($fontPath, $fontSize);

// Use default fonts with supported font name
$pdf->setFont('helvetica', 16);

如果您想使用默认字体,以下是一个列表: courier, courierB, courierBI, courierI, helvetica, helveticaB, helveticaBI, helveticaI, symbol, times, timesB, timesBI, timesI, zapfdingbats

Easy pdf 提供条形码和二维码支持。

// This will add barcode to the pdf with given dimensions.
$code = '[111011101110111][010010001000010][010011001110010][010010000010010][010011101110010]';
$pdf->addBarcode($code, 80, 60, 30, 20); // x-y coordinates and width-height

// // This will add qrcode with best error correction to the pdf with given dimensions.
$pdf->addQrcode('tarfin', 80, 60, 30, 20); // x-y coordinates and width-height

您可以使用尺寸添加图像到 PDF。

// This will add image to the pdf with given dimensions.
$pdf->addImage($imagePath, 80, 60, 30, 20); // x-y coordinates and width-height

您可以使用 setPage() 方法设置活动页面。

// This will set the active page as 1.
$pdf->setPage(1);

您可以使用 setMargins() 方法设置边距。

// This will set margin as 10 for left, 15 for top, 20 for right
// and overwrite the default margins.  
$pdf->setMargins(10, 15, 20, true);

您可以使用 setHeaderData() 方法在页眉中添加图像。

// $textColor and $lineColor must be RGB as array format. '[255, 255, 255]'
$pdf->setHeaderData($image, $width, $textColor, $lineColor);

您可以使用 setHeaderMargin() 方法设置页眉边距。

// This will set the minimum distance between header and top page margin. 
$pdf->setHeaderMargin(10);

您可以使用 setFooterData() 方法设置页脚的测试和线条颜色。

// The first parameter is text color and the last one is line color.
$pdf->setFooterData([0, 64, 255], [0, 64, 128]);

您可以使用 setFooterMargin() 方法设置页脚边距。

// This will set the minimum distance between footer and bottom page margin. 
$pdf->setFooterMargin(10);

您可以使用 setFooterFontSize() 方法设置页脚字体大小。

// This will set the footer font size to 10.
$pdf->setFooterFontSize(10);

您可以在打印对话框中打印文件时设置纸张处理选项。

// This will set the print to single sided. 
$pdf->setDuplex('Simplex'); 

// This will set duplex and flip on the short edge of the sheet
$pdf->setDuplex('DuplexFlipShortEdge'); 

// This will set duplex and flip on the long edge of the sheet
$pdf->setDuplex('DuplexFlipLongEdge'); 

您还可以添加

解析 PDF

您可以解析 PDF 并获取所需的页面。

// This will return pdf page count.
// $file can be path, url or blob.
$fileCount = EasyPdf::parser($file)->count();

// You can use stream or content method here as well.
$parsedPdf = EasyPdf::parser($file)
    ->setPage(1)
    ->save(storage_path('app/imports/new.pdf'));

合并 PDF

您可以使用 easy-pdf 将多个 PDF 合并成一个。

// Pdf paths.
// Pdf paths can be path, url or blob.
$files = [
    '/path/to/the/file.pdf',
    '/path/to/the/anotherFile.pdf',
];

// You can use stream or content method here as well.
$pdf = EasyPdf::merge($files)
            ->content();

拆分 PDF

您可以使用 easy-pdf 将 PDF 文件拆分成多个 PDF 文件。

// Pdf path.
// Pdf path can be path, url or blob.

$file = '/path/to/the/file.pdf';

// You can use splitTo method to get new pdf contents
// chunkSize argument determines that how many page should contain every pdf file
// For example if you want to split your pdf file as each file includes 10 page
// Then you must set chunkSize argument to 10
$pdfs = EasyPdf::parser($file)
            ->splitTo(10);

foreach($pdfs as $pdfContent) {
    // Your code here
}

重置实例

如果您尝试在 Laravel 队列中生成 PDF,有时可能会出现类似 undefined property: TCPDF::$h 的错误。

错误发生在您已经创建了一个 PDF 之后再次使用 EasyPdf 门面时。由于 EasPdf 服务被注册为单例到服务容器中,它会在第二次使用时返回相同的实例,而 somehow 它已损坏。

为了避免上述错误,您可以在开始时使用 reset() 方法。这将返回一个新的 TCPDF 实例。

$pdf = EasyPdf::reset()
    ->withInformation([
        'Creator'   => 'Tarfin',
        'Author'    => 'Faruk Can',
        'Title'     => 'EasyPdf',
        'Keywords'  => 'easy, pdf',
        'AutoPageBreak' => [true, 0],
    ])
    ->withConfig([
            'ImageScale' => PDF_IMAGE_SCALE_RATIO,
    ])
    ->setFont('times', 16) // use default fonts
    ->loadHtml($html) // each load html creates a new page
    ->content(); // return pdf content as a string

PDF 页眉和页脚设置器

在为每个页面创建新的 Tcpdf 实例后使用 reset() 方法,将自动向 PDF 添加页眉行。无论您是否想使用 setHeader 打印页眉或 setFooter 打印页脚内容,都可以使用。

$pdf = EasyPdf::reset()
    ->setHeader(false);

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近更改的更多信息。

贡献

欢迎拉取请求。对于主要更改,请首先提交一个问题以讨论您想更改的内容。

请确保适当地更新测试。

安全性

如果您发现任何安全相关的问题,请通过电子邮件发送至 development@tarfin.com 而不是使用问题跟踪器。

鸣谢

许可

easy-pdf 是开源软件,采用 MIT 许可证授权。