Niewiele jest przypadków, w których istnieje potrzeba manipulowania dokumentami innymi niż PDF przy jednoczesnym udostępnieniu danych parsowania w formatach PDF. Tak więc w przypadku takich aplikacji będą dwa scenariusze: albo dodają funkcjonalność parsowania plików PDF w swoim rozwiązaniu, albo dodają funkcję konwersji PDF, aby manipulować danymi jako obsługiwane formaty. W przypadku drugiego scenariusza, aby konwertować plik PDF na Word, Excel, HTML, obrazy lub dowolny wymagany format, wdrożenie czytnika PHP i konwertera dla kodu PDF** w kodzie opartym na Javie jest proste. Omawiamy tutaj kilka przypadków, aby programiści mogli modyfikować te fragmenty kodu konwersji zgodnie z ich wymaganiami.
Konwersja PDF do Microsoft Word
// 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 dla biblioteki PHP obsługuje wszystkie konwersje PDF na Word. W przypadku, gdy po prostu konwertujemy dokumenty Microsoft Word bez żadnych specjalnych ustawień, po prostu ładujemy plik PDF za pomocą metody Zapisz z klasy Dokument i użyjemy z wyjściową ścieżką dokumentu Word i SaveFormat jako parametrów. W szczególnych przypadkach, w których istnieje potrzeba zwiększenia odległości linii, rozdzielczości obrazu i innych ustawień, API ma klasę DocSaveOptions, która ujawnia wszystkie takie ustawienia.
Zapisz PDF jako pliki 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);
Specjalistyczne SaveFormat.excel Wyliczenie dostępne do zapisywania plików PDF w określonych formatach wyjściowych Microsoft Excel XLS XLSX. Co więcej, PHP/Java PDF ma również specyficzną klasę ExcelSaveOptions, która nie tylko zajmuje się zapisywaniem w formatach Excela, ale także zapewnia różne funkcje i właściwości do ustawiania różnych atrybutów, takich jak dokładny format wyjściowy, minimalizowanie liczby arkuszy roboczych i nie tylko.
Konwertuj PDF na prezentacje PowerPoint
// 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 obsługuje konwersję stron PDF na slajdy prezentacji PowerPoint z tekstem lub obrazami, renderując slajdy jako obrazy. Wzór zapisywania formatu dokumentu przenośnego w programie PowerPoint jest prawie taki sam, ładowanie pliku za pomocą klasy Document, a następnie wywoływanie metody Zapisz ze ścieżką pliku wyjściowego i SaveFormat jako parametrami. W przypadku renderowania ze specjalnymi opcjami prezentacji programiści mogą używać klasy pptxsaveOptions z dowolnymi odpowiednimi konkretnymi opcjami renderowania. Wywołanie metody zapisu i przekazywanie opcji jako parametru.
Konwersja PDF do 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);
Biblioteka parsowania plików PDF obsługuje zapisywanie plików PDF w formacie HTML jako całości, a także z osadzonymi zasobami, w tym obrazami. Procedura konwersji jest taka sama jak PDF do innych formatów w przypadku ogólnych przypadków, takich jak ładowanie dokumentu źródłowego i wywołanie metody Zapisz z wyjściową ścieżką pliku HTML i SaveFormat.Html jako parametrami. W przypadku zapisywania z osadzonymi zasobami istnieje klasa HTMLSaveOptions, która ma wiele opcji, takich jak zapisywanie obrazów w określonym folderze podczas konwersji, dzielenie wynikowego HTML na wiele stron i nie tylko.
Konwertuj PDF na obrazy
// 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);
}
Konwersja stron PDF na obrazy, w tym PNG, JPEG, TIFF, BMP itp., jest łatwa w aplikacjach opartych na Java przy użyciu fragmentów kodu wymienionych poniżej. Programiści mogą przeglądać strony PDF po załadowaniu pliku i przekonwertować stronę po stronie do wymaganego formatu obrazu. Programiści mogą ustawić poziomą i pionową rozdzielczość obrazów za pomocą Klasa rozdzielczości