ตัวแปลงรูปแบบ PDF ผ่าน PHP via Java

ส่งออก PDF ไปยัง Word, Excel, PowerPoint, รูปภาพ, HTML และรูปแบบเค้าโครงคงที่โดยใช้ PHP via Java

ภาพรวม

มีบางกรณีที่จำเป็นต้องจัดการเอกสารอื่นนอกเหนือจาก PDF ในขณะที่มีข้อมูลการแยกวิเคราะห์ในรูปแบบ PDFดังนั้นสำหรับแอปพลิเคชันดังกล่าวจะมีสองสถานการณ์: พวกเขาเพิ่มฟังก์ชันการวิเคราะห์ไฟล์ PDF ภายในโซลูชันของพวกเขาหรือเพิ่มฟังก์ชันการแปลง PDF เพื่อจัดการข้อมูลเป็นรูปแบบที่รองรับสำหรับสถานการณ์ที่สองเพื่อแปลง PDF เป็น Word, Excel, HTML, รูปภาพ หรือรูปแบบที่จำเป็น การใช้โปรแกรมอ่าน PHP และตัวแปลงสำหรับรหัส PDF** ภายในโค้ดที่ใช้ Java นั้นง่ายมากเรากำลังพูดถึงบางกรณีที่นี่เพื่อให้โปรแกรมเมอร์สามารถแก้ไขตัวอย่างโค้ดการแปลงเหล่านี้ตามความต้องการของพวกเขา

การแปลง PDF เป็น Microsoft Word

ตัวอย่าง PHP: รหัสสำหรับการแปลง PDF เป็น 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 สำหรับไลบรารี PHP รองรับการแปลง PDF เป็น Word ทั้งหมดในกรณีที่เราเพิ่งแปลงเอกสาร Microsoft Word โดยไม่ต้องตั้งค่าพิเศษเราเพียงแค่โหลดไฟล์ PDF โดยใช้วิธีบันทึกจากคลาสเอกสารและจะใช้กับเส้นทางเอกสาร Word เอาต์พุตและ SaveFormat เป็นพารามิเตอร์สำหรับกรณีพิเศษที่จำเป็นต้องเพิ่มระยะทางเส้นความละเอียดของภาพและการตั้งค่าอื่น ๆ API มีคลาส DocSaveOptions ที่เปิดเผยการตั้งค่าดังกล่าวทั้งหมด

บันทึก PDF เป็นไฟล์ Excel

ตัวอย่าง 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);

การนับเฉพาะ SaveFormat.excel พร้อมใช้งานสำหรับการบันทึก PDF ไปยังรูปแบบเอาต์พุต Microsoft Excel XLS XLSX ที่เฉพาะเจาะจงนอกจากนี้ PHP/Java PDF Library ยังมีข้อมูลจำเพาะ ExcelSaveOptions class ที่ไม่เพียง แต่จัดการกับการบันทึกในรูปแบบ Excel เท่านั้น แต่ยังมีฟังก์ชันและคุณสมบัติที่แตกต่างกันสำหรับการตั้งค่าแอตทริบิวต์ที่แตกต่างกัน เช่น รูปแบบเอาต์พุตที่แน่นอน ลดจำนวนแผ่นงานและอื่น ๆ

แปลง PDF เป็นงานนำเสนอ PowerPoint

ตัวอย่าง PHP: การแปลงรหัส PDF เป็น 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 รองรับการแปลงหน้า PDF เป็นสไลด์นำเสนอ PowerPoint พร้อมข้อความหรือรูปภาพที่เลือกได้โดยการแสดงสไลด์เป็นภาพรูปแบบของการบันทึกรูปแบบเอกสารแบบพกพาไปยัง PowerPoint เกือบจะเหมือนกันโหลดไฟล์โดยใช้คลาสเอกสารจากนั้นเรียกวิธีการบันทึกด้วยเส้นทางไฟล์เอาต์พุตและ SaveFormat เป็นพารามิเตอร์ในกรณีที่มีการแสดงผลด้วยตัวเลือกการนำเสนอพิเศษ โปรแกรมเมอร์สามารถใช้ PPTXSaveOptions class กับตัวเลือกการแสดงผลเฉพาะที่เกี่ยวข้องใด ๆเรียกวิธีการบันทึกและส่งตัวเลือกเป็นพารามิเตอร์

การแปลง 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 Parsing Library รองรับการบันทึก PDF เป็น HTML โดยรวมรวมถึงทรัพยากรที่ฝังตัวรวมถึงรูปภาพขั้นตอนการแปลงเหมือนกับ PDF เป็นรูปแบบอื่น ๆ สำหรับกรณีทั่วไปเช่นการโหลดเอกสารต้นฉบับและเรียกวิธีการบันทึกด้วยเส้นทางไฟล์ HTML เอาต์พุตและ SaveFormat.Html เป็นพารามิเตอร์ในกรณีที่บันทึกด้วยทรัพยากรแบบฝังตัว มี คลาส 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 หลังจากโหลดไฟล์และแปลง Page by Page เป็นรูปแบบภาพที่ต้องการนักพัฒนาสามารถตั้งค่าความละเอียดแนวนอนและแนวตั้งของภาพโดยใช้ ระดับความละเอียด