lianhua/superpdf

此包已被 废弃 且不再维护。未建议替代包。

一个简单的PDF文件操作库

1.1.5 2023-11-07 06:14 UTC

This package is auto-updated.

Last update: 2023-11-07 06:15:42 UTC


README

一个简单的PDF文件操作库

Build Status BCH compliance License: GPL v3

概览

一个用于在PHP中操作PDF文件的简单库

兼容性

此库已测试过PHP 7.3及更高版本。依赖于TCPDF的功能目前不能在PHP 8上工作!

安装

只需在项目中使用composer

composer require lianhua/superpdf

如果您不使用composer,请克隆或下载此仓库,所有需要的文件都在src目录中,您需要FPDI库。

使用方法

打开PDF文件

您可以使用新的SuperPDF打开PDF文档

$pdf = new SuperPDF("/path/to/pdf/file");

计算页数

您可以请求页数

$pagesCount = $pdf->getPageCount();

提取页面

范围

如果您想将一系列页面提取到PDF文件中

$pdf->extractPageRange(3, 7, "path/to/pdf/output");

列表

如果您想将一系列页面提取到PDF文件中

$pdf->extractPageList([1, 3, 6, 8, 9], "path/to/pdf/output");

插入PDF文档

如果您想将PDF文件的内容插入到文档中

$pdf->insertPages("/path/to/pdf/to/insert", 5); // Inserts the PDF at page 5
$pdf->insertPages("/path/to/pdf/to/insert", SuperPDF::AFTER_EACH_PAGE); // Inserts the PDF after each page
$pdf->insertPages("/path/to/pdf/to/insert", SuperPDF::AFTER_ODD_PAGES); // Inserts the PDF after odd pages
$pdf->insertPages("/path/to/pdf/to/insert", SuperPDF::AFTER_EVEN_PAGES); // Inserts the PDF after even pages
$pdf->insertPages("/path/to/pdf/to/insert", SuperPDF::AT_THE_END); // Inserts the PDF after the last page

如果您不想覆盖文档,您可以提供一个输出路径

$pdf->insertPages("/path/to/pdf/to/insert", 5, "/path/to/output");

添加背景

如果您想在文档上添加背景

$pdf->addBackground("/path/to/background/pdf", 5); // Adds a background on page 5
$pdf->addBackground("/path/to/background/pdf", SuperPDF::ON_LAST_PAGE); // Adds a background on last page
$pdf->addBackground("/path/to/background/pdf", SuperPDF::ON_ODD_PAGES); // Adds a background on odd pages
$pdf->addBackground("/path/to/background/pdf", SuperPDF::ON_EVEN_PAGES); // Adds a background on even pages
$pdf->addBackground("/path/to/background/pdf", SuperPDF::ON_EACH_PAGE); // Adds a background on each page

像之前一样,如果您不想覆盖文档,您可以提供一个输出路径

$pdf->addBackground("/path/to/background/pdf", 5, "/path/to/output");

写入文本

如果您想在文档上写入一些文本

$params = [
    "font" => "arial", // Font family
    "color" => ["r" => 30, "g" => 30, "b" => 30], // Font color
    "pos" => ["x" => 10, "y" => 20], // Text position
    "size" => 15 // Font size
];

$pdf->writeText("Some text", $params, 5); // Writes a text on page 5
$pdf->writeText("Some text", $params, SuperPDF::ON_LAST_PAGE); // Writes a text on last page
$pdf->writeText("Some text", $params, SuperPDF::ON_ODD_PAGES); // Writes a text on odd pages
$pdf->writeText("Some text", $params, SuperPDF::ON_EVEN_PAGES); // Writes a text on even pages
$pdf->writeText("Some text", $params, SuperPDF::ON_EACH_PAGE); // Writes a text on each page

像之前一样,如果您不想覆盖文档,您可以提供一个输出路径

$pdf->writeText("Some text", $params, 5, "/path/to/output");

绘制图像

如果您想在文档上绘制图像(PNG、JPG、GIF或SVG)

$params = [
    "x" => 30, // The X position
    "y" => 50, // The Y position
    "w" => 60 // The width (you can also use h)
];

$pdf->drawImage("/path/to/image/file", $params, 5); // Draws an image on page 5
$pdf->drawImage("/path/to/image/file", $params, SuperPDF::ON_LAST_PAGE); // Draws an image on last page
$pdf->drawImage("/path/to/image/file", $params, SuperPDF::ON_ODD_PAGES); // Draws an image on odd pages
$pdf->drawImage("/path/to/image/file", $params, SuperPDF::ON_EVEN_PAGES); // Draws an image on even pages
$pdf->drawImage("/path/to/image/file", $params, SuperPDF::ON_EACH_PAGE); // Draws an image on each page

像之前一样,如果您不想覆盖文档,您可以提供一个输出路径

$pdf->drawImage("/path/to/image/file", $params, 5, "/path/to/output");