{{製品名}} 経由の PDF フォーマットコンバーター

PHP via Java を使用して PDF を Word、Excel、PowerPoint、画像、HTML、および固定レイアウト形式にエクスポートします

概要

解析データをPDF形式で利用できる状態で、PDF以外の文書を操作する必要があるケースはほとんどありません。そのため、このようなアプリケーションには、ソリューションに PDF 解析機能を追加する方法と、PDF 変換機能を追加してデータをサポートされている形式で操作する方法の 2 つのシナリオがあります。2つ目のシナリオとして、PDFをWord、Excel、HTML、画像、または任意の必要な形式に変換するというシナリオでは、PHP リーダーと PDF 用コンバーター コードを Java ベースのコードに実装するのは簡単です。ここでは、プログラマーがこれらの変換コードスニペットを要件に応じて変更できるように、いくつかのケースについて説明します。

PDF からマイクロソフトワードへの変換

PHP の例:PDF からワードへの変換のためのコード

// Include the required libraries
require_once ("java/Java.inc");
require_once ("lib/aspose.pdf.php");

// Import the necessary classes from the Aspose.PDF for Java library
use com\aspose\pdf\License;
use com\aspose\pdf\Document;
use com\aspose\pdf\DocSaveOptions;
use com\aspose\pdf\DocSaveOptions_DocFormat;
use com\aspose\pdf\DocSaveOptions_RecognitionMode;

// Set the license file for Aspose.PDF for Java
$license = "Aspose.PDF.PHPviaJava.lic";
$licenceObject = new License();
$licenceObject->setLicense($license);

// Set the input and output file paths
$dataDir = getcwd() . DIRECTORY_SEPARATOR . "samples";
$inputFile = $dataDir . DIRECTORY_SEPARATOR . "sample.pdf";
$outputFile = $dataDir . DIRECTORY_SEPARATOR . 'result-pdf-to-docx.docx';

// Load the PDF document
$document = new Document($inputFile);

// Create the save options for converting to DOCX format
$saveOption = new DocSaveOptions();
$saveOption->setMode(DocSaveOptions_RecognitionMode::$EnhancedFlow);
$saveOption->setFormat(DocSaveOptions_DocFormat::$DocX);

// Save the document in DOCX format
$document->save($outputFile, $saveOption);

Aspose.PDF for PHP ライブラリは PDF から Word へのすべての変換をサポートしています。特別な設定を行わずに Microsoft Word ドキュメントを変換するだけの場合は、Document クラスの Save メソッドを使用して PDF ファイルをロードし、出力 Word ドキュメントパスと SaveFormat をパラメータとして使用します。線の距離や画像の解像度などの設定を強化する必要がある特殊なケースに備えて、API にはそのような設定をすべて公開する DocSaveOptions クラスが用意されています。

PDF をエクセルファイルとして保存する

PHP の例:PDF から Excel への変換のためのコード

// Include the required libraries
require_once ("java/Java.inc");
require_once ("lib/aspose.pdf.php");

// Import the necessary classes from the Aspose.PDF for Java library
use com\aspose\pdf\Document;
use com\aspose\pdf\ExcelSaveOptions;
use com\aspose\pdf\ExcelSaveOptions_ExcelFormat;
use com\aspose\pdf\License;

// Set the path to the Aspose.PDF license file
$license = "Aspose.PDF.PHPviaJava.lic";

// Create a new License object and set the license file
$licenceObject = new License();
$licenceObject->setLicense($license);

// Set the path to the input PDF file
$dataDir = getcwd() . DIRECTORY_SEPARATOR . "samples";
$inputFile = $dataDir . DIRECTORY_SEPARATOR . "sample.pdf";

// Set the path to the output Excel file
$outputFile = $dataDir . DIRECTORY_SEPARATOR . 'sample.xlsx';

// Create a new Document object and load the input PDF file
$document = new Document($inputFile);

// Create a new ExcelSaveOptions object
$saveOption = new ExcelSaveOptions();

// Set the output format to XLSX
$saveOption->setFormat(ExcelSaveOptions_ExcelFormat::$XLSX);

// Save the document as an Excel file using the specified save options
$document->save($outputFile, $saveOption);

PDF を特定の Microsoft Excel XLS XLSX 出力形式に保存するための特殊な SaveFormat.Excel 列挙型を使用できます。さらに、PHP/Java PDF Libraryには特定の ExcelSaveOptions クラス もあります。このクラスは Excel 形式への保存だけでなく、正確な出力形式、ワークシートの数の最小化など、さまざまな属性を設定するためのさまざまな関数やプロパティを提供します。

PDF をパワーポイントプレゼンテーションに変換

PHP の例:PDF からパワーポイントへのコード変換

// Include the required Java and Aspose.PDF for PHP libraries
require_once ("java/Java.inc");
require_once ("lib/aspose.pdf.php");

// Import the necessary classes from the Aspose.PDF for PHP library
use com\aspose\pdf\Document;
use com\aspose\pdf\PptxSaveOptions;
use com\aspose\pdf\License;

// Set the path to the Aspose.PDF license file
$license = "Aspose.PDF.PHPviaJava.lic";

// Create a new License object and set the license file
$licenceObject = new License();
$licenceObject->setLicense($license);

// Set the path to the input PDF file
$dataDir = getcwd() . DIRECTORY_SEPARATOR . "samples";
$inputFile = $dataDir . DIRECTORY_SEPARATOR . "sample.pdf";

// Set the path to the output PPTX file
$outputFile = $dataDir . DIRECTORY_SEPARATOR . "results" . DIRECTORY_SEPARATOR . 'sample.pptx';

// Load the input PDF document
$document = new Document($inputFile);

// Create an instance of PptxSaveOptions
$saveOption = new PptxSaveOptions();

// Save the PDF document as a PPTX file
$document->save($outputFile, $saveOption);

PHP APIは、スライドを画像としてレンダリングすることにより、PDFページを、選択可能なテキストまたは画像を含むPowerPointプレゼンテーションスライドに変換することをサポートしています。ポータブル・ドキュメント・フォーマットをPowerPointに保存するパターンはほぼ同じで、Documentクラスを使用してファイルをロードし、出力ファイル・パスとSaveFormatをパラメータとしてSaveメソッドを呼び出します。特別な表示オプションを使用してレンダリングする場合、プログラマは PptxSaveOptions クラス を関連する特定のレンダリングオプションとともに使用できます。save メソッドを呼び出し、オプションをパラメーターとして渡します。

PDF から HTML への変換

PHP サンプル:PDF から HTML への変換のためのコード

// Include the required libraries
require_once ("java/Java.inc");
require_once ("lib/aspose.pdf.php");

// Import the necessary classes from the Aspose.PDF library
use com\aspose\pdf\Document;
use com\aspose\pdf\HtmlSaveOptions;
use com\aspose\pdf\License;

// Set the path to the license file
$licensePath = "Aspose.PDF.PHPviaJava.lic";

// Create a new License object and set the license using the provided file path
$license = new License();
$license->setLicense($licensePath);

// Set the path to the input PDF file
$dataDir = getcwd() . DIRECTORY_SEPARATOR . "samples";
$inputFile = $dataDir . DIRECTORY_SEPARATOR . "sample.pdf";

// Set the path to the output HTML file
$outputFile = $dataDir . DIRECTORY_SEPARATOR . 'pdf-to-html.html';

// Create a new Document object and load the input PDF file
$document = new Document($inputFile);

// Create a new HtmlSaveOptions object for saving the document as HTML
$saveOption = new HtmlSaveOptions();

// Save the document as HTML using the specified save options
$document->save($outputFile, $saveOption);

PDF 解析ライブラリは、PDF 全体を HTML に保存するだけでなく、画像などの埋め込みリソースと一緒に保存することもできます。ソース文書を読み込み、出力 HTML ファイルパスと SaveFormat.Html をパラメーターとして Save メソッドを呼び出すなど、一般的なケースでは PDF から他の形式への変換手順と同じです。埋め込みリソースを使用して保存する場合、HTMLSaveOptions クラス があります。変換時に特定のフォルダーに画像を保存する、生成された HTML を複数のページに分割するなど、複数のオプションがあります。

PDF を画像に変換

PHP の例:PDF から画像への変換のコード

// Include the required libraries
require_once ("java/Java.inc");
require_once ("lib/aspose.pdf.php");

// Import the necessary classes from the Aspose.PDF for PHP via Java library
use com\aspose\pdf\Document;
use com\aspose\pdf\devices_Resolution;
use com\aspose\pdf\devices_JpegDevice;
use com\aspose\pdf\License;

// Create a License object and set the license file
$licenceObject = new License();
$licenceObject->setLicense("Aspose.PDF.PHPviaJava.lic");

// Set the path to the input PDF file
$dataDir = getcwd() . DIRECTORY_SEPARATOR . "samples";
$inputFile = $dataDir . DIRECTORY_SEPARATOR . "sample.pdf";

// Set the path and template for the output JPEG files
$imageFileNameTemplate = $dataDir . DIRECTORY_SEPARATOR . 'pdf-to-jpeg-';

// Open the target document
$document = new Document($inputFile);
$pages = $document->getPages();
$count = $pages->size();

// Create a Resolution object with a resolution of 300 dpi
$resolution = new devices_Resolution(300);

// Create a JpegDevice object with the specified resolution
$imageDevice = new devices_JpegDevice($resolution);

// Loop through each page of the document
for ($pageCount = 1; $pageCount <= $document->getPages()->size(); $pageCount++) {
    // Convert a particular page and save the image to a file
    $imageFileName = $imageFileNameTemplate . $pageCount . '.jpg';
    $page = $document->getPages()->get_Item($pageCount);
    $imageDevice->process($page, $imageFileName);
}

PDFページをPNG、JPEG、TIFF、BMPなどの画像に変換することは、Javaベースのアプリケーション内で以下のコードスニペットを使用して簡単に行えます。開発者は、ファイルを読み込んだ後に PDF ページをループ処理して、1 ページずつ必要な画像形式に変換できます。開発者は 解像度クラス を使用して画像の水平解像度と垂直解像度を設定できます。