Convertitore di formato PDF tramite PHP via Java

Esporta PDF in Word, Excel, PowerPoint, immagini, HTML e formati a layout fisso utilizzando PHP via Java

Panoramica

Ci sono pochi casi in cui è necessario manipolare documenti diversi dal PDF pur avendo i dati di analisi disponibili nei formati PDF. Quindi, per tali applicazioni, ci saranno due scenari: o aggiungono la funzionalità di analisi dei PDF all’interno della loro soluzione o aggiungono la funzionalità di conversione PDF per manipolare i dati come formati supportati. Nel secondo scenario per convertire PDF in Word, Excel, HTML, immagini o qualsiasi formato richiesto, l’implementazione del lettore e convertitore PHP per codice PDF all’interno di codice basato su Java è semplice. Stiamo esaminando alcuni casi in modo che i programmatori possano modificare questi frammenti di codice di conversione in base alle loro esigenze.

Conversione da PDF a Microsoft Word

Esempio PHP: codice per la conversione da PDF a 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);

La libreria Aspose.PDF per PHP supporta tutte le conversioni da PDF a Word. Nel caso in cui stiamo semplicemente convertendo documenti Microsoft Word senza impostazioni speciali, caricheremo semplicemente il file PDF utilizzando il metodo Save dalla classe Document e utilizzeremo con output il percorso del documento Word e SaveFormat come parametri. Per i casi speciali in cui è necessario migliorare la distanza delle linee, la risoluzione dell’immagine e altre impostazioni, l’API ha la classe DocSaveOptions che espone tutte queste impostazioni.

Salva PDF come file Excel

Esempio PHP: codice per la conversione da PDF a 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);

Enumerazione specializzata SaveFormat.Excel Disponibile per il salvataggio di PDF in formati di output XLS XLSX specifici di Microsoft Excel. Inoltre, PHP/Java PDF Library ha anche una specifica classe ExcelSaveOptions che non solo si occupa del salvataggio nei formati Excel, ma fornisce anche diverse funzioni e proprietà per l’impostazione di attributi diversi come il formato di output esatto, la riduzione al minimo del numero di fogli di lavoro e altro ancora.

Converti presentazioni da PDF a PowerPoint

Esempio PHP: conversione del codice da PDF a 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);

L’API PHP supporta la conversione di pagine PDF in diapositive di presentazione PowerPoint con testo o immagini selezionabili mediante il rendering delle diapositive come immagini. Lo schema di salvataggio di Portable Document Format in PowerPoint è quasi lo stesso: caricamento del file utilizzando la classe Document e quindi richiamando il metodo Save con il percorso del file di output e SaveFormat come parametri. In caso di rendering con opzioni di presentazione speciali, i programmatori possono utilizzare classe PPTxSaveOptions con qualsiasi opzione di rendering specifica pertinente. Chiamando il metodo save e passando le opzioni come parametro.

Conversione da PDF a HTML

Esempio PHP: codice per la conversione da PDF a 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 Parsing Library supporta il salvataggio di PDF in HTML nel suo insieme e con risorse incorporate, comprese le immagini. La procedura di conversione è la stessa del PDF in altri formati per casi generici, come il caricamento del documento sorgente e la chiamata al metodo Save con il percorso del file HTML di output e SaveFormat.Html come parametri. In caso di salvataggio con risorse integrate, esiste una classe HTMLSaveOptions con più opzioni come salvare le immagini in una cartella specifica durante la conversione, suddividere l’HTML risultante in più pagine e altro ancora.

Convertire PDF in immagini

Esempio PHP: codice per la conversione da PDF a immagini

// 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);
}

La conversione di pagine PDF in immagini tra cui PNG, JPEG, TIFF, BMP ecc. è facile all’interno delle applicazioni basate su Java utilizzando i frammenti di codice elencati di seguito. Gli sviluppatori possono scorrere le pagine PDF dopo aver caricato il file e convertire pagina per pagina nel formato di immagine richiesto. Gli sviluppatori possono impostare la risoluzione orizzontale e verticale delle immagini utilizzando Resolution class