HTML
JPG
PDF
XML
DOCX
PDF
如何使用 Cloud PHP SDK 提取、附加和替换 PDF 文档中的附件
为了处理 PDF 文档中的附件,我们将使用 Aspose.PDF Cloud PHP SDK 此 Cloud SDK SDK 帮助 PHP 程序员使用 PHP 编程语言通过 Aspose.PDF REST API 开发基于云的 PDF 创建、注释、编辑和转换应用程序。打开 NPM 包管理器,搜索 Aspose.PDF Cloud 并安装。
使用 PHP SDK 提取附件的步骤
Aspose.PDF Cloud PHP 开发人员可以轻松提取、附加和替换 PDF 文档中的附件。开发人员只需几行代码。
- 从 JSON 文件加载 API 凭证
- 使用加载的凭证配置 API 客户端
- 将本地 PDF 文件上传到 Aspose 存储
- 执行 PDF 文档的附件提取操作
- 下载每个附件并本地保存
此示例代码展示了如何使用 PDF Cloud PHP SDK 提取 PDF 文档的所有附件
<?php
require_once 'vendor/autoload.php';
use Aspose\PDF\Api\PdfApi;
use Aspose\PDF\Configuration;
use Aspose\PDF\Model\AttachmentInfo;
use Aspose\PDF\Model\AttachmentResponse;
// Load credentials
$credentials = json_decode(file_get_contents("./credentials.json"), true);
$apiKey = $credentials["key"];
$appSID = $credentials["id"];
$config = new Configuration();
$config->setAppKey($apiKey);
$config->setAppSid($appSID);
$pdfApi = new PdfApi(null, $config);
$localPath = "../Samples/";
$localFileName = "../Samples/Attachments/sample_attachment.pdf";
$storageFileName = "sample_attachment.pdf";
try {
$pdfApi->uploadFile($storageFileName, $localFileName);
$result = $pdfApi->getDocumentAttachments($storageFileName);
if ($result->getCode() == 200 && $result->getAttachments()) {
if (empty($result->getAttachments()->getList())) {
echo "Unexpected error: No attachments to download.\n";
return;
}
foreach ($result->getAttachments()->getList() as $index => $attachment) {
try {
$response = $pdfApi->getDocumentAttachmentByIndex($storageFileName, $index);
$filePath = $localPath . $response->getAttachment()->getName();
file_put_contents($filePath, json_encode($response));
} catch (Exception $e) {
echo "Failed to download attachment {$index}: {$e->getMessage()}\n";
return;
}
}
} else {
echo "Failed to retrieve attachments.\n";
return;
}
} catch (Exception $e) {
echo "Error processing PDF attachments: {$e->getMessage()}\n";
return;
}