PHP 을 통한 PDF 형식 변환기

PHP via Java 을 사용하여 PDF를 워드, 엑셀, 파워포인트, 이미지, HTML 및 고정 레이아웃 형식으로 내보내기

개요

파싱 데이터를 PDF 형식으로 사용할 수 있는 상태에서 PDF 이외의 문서를 조작해야 하는 경우는 거의 없습니다.따라서 이러한 응용 프로그램에는 두 가지 시나리오가 있습니다. 하나는 솔루션 내에 PDF 구문 분석 기능을 추가하는 것이고, 다른 하나는 데이터를 지원되는 형식으로 조작하는 PDF 변환 기능을 추가하는 것입니다.PDF를 Word, Excel, HTML, 이미지 또는 기타 필수 형식으로 변환하려는 두 번째 시나리오의 경우 Java 기반 코드 내에서 PDF 코드용 PHP 리더 및 변환기를 구현하는 것은 간단합니다.여기서는 프로그래머가 요구 사항에 따라 이러한 변환 코드 스니펫을 수정할 수 있도록 몇 가지 사례를 논의하고 있습니다.

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

PHP용 Aspose.PDF 라이브러리는 모든 PDF를 워드로의 변환을 지원합니다.특별한 설정 없이 마이크로소프트 워드 문서를 변환하는 경우에는 Document 클래스에서 Save 메서드를 사용하여 PDF 파일을 로드하고 출력 Word 문서 경로와 SaveFormat을 매개 변수로 사용합니다.선 거리, 이미지 해상도 및 기타 설정을 향상시켜야 하는 특수한 경우를 위해 API에는 이러한 모든 설정을 노출하는 DocSaveOptions 클래스가 있습니다.

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\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를 특정 마이크로소프트 엑셀 XLS XLSX 출력 형식으로 저장하는 데 사용할 수 있는 열거입니다.또한 PHP/Java PDF 라이브러리에는 Excel 형식으로 저장할 뿐만 아니라 정확한 출력 형식, 워크시트 수 최소화 등과 같은 다양한 속성을 설정하기 위한 다양한 함수와 속성을 제공하는 특정 ExcelSaveOptions 클래스 도 있습니다.

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

Java 기반 애플리케이션에서는 아래 나열된 코드 스니펫을 사용하여 PDF 페이지를 PNG, JPEG, TIFF, BMP 등의 이미지로 쉽게 변환할 수 있습니다.개발자는 파일을 로드한 후 PDF 페이지를 반복하여 페이지별로 필요한 이미지 형식으로 변환할 수 있습니다.개발자는 해상도 클래스 를 사용하여 이미지의 가로 및 세로 해상도를 설정할 수 있습니다.