HTML
JPG
PDF
XML
DOCX
PDF
Cloud PHP SDKを使用してPDFドキュメントから添付ファイルを抽出する方法
PDFドキュメントの添付ファイルを扱うために使用するのは、 Aspose.PDF Cloud PHP SDK このCloud SDKは、PHPプログラマーがAspose.PDF REST APIを介してPHPプログラミング言語でクラウドベースの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;
}