通过 PHP 向 PDF 添加附件

如何使用 PHP 以编程方式在 PDF 中添加附件。

如何使用 PHP via Java 库管理附件

为了在 PDF 文件中添加附件,我们将使用 Aspose.PDF API,这是一款功能丰富、功能强大且易于使用的适用于 php-java 平台的文档处理 API。打开 NuGet 软件包管理器,搜索 aspose.pdf 然后安装。您也可以从软件包管理器控制台使用以下命令。

通过 PHP 在 PDF 文件中添加附件


你需要 Aspose.PDF 才能在你的环境中试用代码。

  1. 创建一个新的 PHP 项目。
  2. 添加对 Aspose.PDF DLL 的引用。
  3. 创建文档对象。
  4. 使用要添加的文件和文件描述创建一个 FileSpecification 对象。
  5. 使用集合的 Add 方法将 FileSpecification 对象添加到 Document 对象的 EmbeddedFiles 集合中
  6. 保存 PDF 文件。

将附件添加到 PDF 文档。


// Open document
$document = new Document($inputFile);

// Set up a new file to be added as attachment
$fileSpecification = new FileSpecification("sample.txt", "Sample text file");
// Add an attachment to document's attachment collection
$document->getEmbeddedFiles()->add($fileSpecification);

// Save resulting PDF document.    
$document->save($outputFile);
$document->close();