PDF2Office - Convert PDF to DOCX, XSLX - PHP Sample Code

Sample code for using Apryse SDK to programmatically convert generic PDF documents to Word, Excel, PowerPoint; provided in Python, C++, C#, Go, Java, Node.js (JavaScript), PHP, Ruby and VB.

To run this sample:

  1. Complete the Get started with Server SDK process in your language/framework.
  2. After you complete the Get Started with Server SDK work in your language/framework from Step 1 above, next, download the Structured Output Module.

Learn more about our Server SDK and PDF to Office Conversion

1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10//---------------------------------------------------------------------------------------
11// The following sample illustrates how to use the PDF::Convert utility class to convert
12// documents and files to Word, Excel and PowerPoint.
13//
14// The Structured Output module is an optional PDFNet Add-on that can be used to convert PDF
15// and other documents into Word, Excel, PowerPoint and HTML format.
16//
17// The PDFTron SDK Structured Output module can be downloaded from
18// https://docs.apryse.com/core/info/modules/
19//
20// Please contact us if you have any questions.
21//---------------------------------------------------------------------------------------
22
23function main()
24{
25 // Relative path to the folder containing the test files.
26 $inputPath = getcwd()."/../../TestFiles/";
27 $outputPath = $inputPath."Output/";
28
29 // The first step in every application using PDFNet is to initialize the
30 // library. The library is usually initialized only once, but calling
31 // Initialize() multiple times is also fine.
32 global $LicenseKey;
33 PDFNet::Initialize($LicenseKey);
34 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
35
36 //-----------------------------------------------------------------------------------
37
38 PDFNet::AddResourceSearchPath("../../../PDFNetC/Lib/");
39
40 if (!StructuredOutputModule::IsModuleAvailable()) {
41 echo(nl2br("\n"));
42 echo(nl2br("Unable to run the sample: PDFTron SDK Structured Output module not available.\n"));
43 echo(nl2br("-----------------------------------------------------------------------------\n"));
44 echo(nl2br("The Structured Output module is an optional add-on, available for download\n"));
45 echo(nl2br("at https://docs.apryse.com/core/info/modules/. If you have already\n"));
46 echo(nl2br("downloaded this module, ensure that the SDK is able to find the required files\n"));
47 echo(nl2br("using the PDFNet::AddResourceSearchPath() function.\n"));
48 echo(nl2br("\n"));
49 return;
50 }
51
52 //-----------------------------------------------------------------------------------
53
54 try {
55 // Convert PDF document to Word
56 echo(nl2br("Converting PDF to Word\n"));
57
58 $outputFile = $outputPath."paragraphs_and_tables.docx";
59
60 Convert::ToWord($inputPath."paragraphs_and_tables.pdf", $outputFile);
61
62 echo(nl2br("Result saved in " . $outputFile . "\n"));
63 }
64 catch(Exception $e) {
65 echo(nl2br("Unable to convert PDF document to Word, error: " . $e->getMessage() . "\n"));
66 }
67
68 //-----------------------------------------------------------------------------------
69
70 try {
71 // Convert PDF document to Word with options
72 echo(nl2br("Converting PDF to Word with options\n"));
73
74 $outputFile = $outputPath."paragraphs_and_tables_first_page.docx";
75
76 $wordOutputOptions = new WordOutputOptions(); // Convert::WordOutputOptions();
77
78 // Convert only the first page
79 $wordOutputOptions->SetPages(1, 1);
80
81 Convert::ToWord($inputPath."paragraphs_and_tables.pdf", $outputFile, $wordOutputOptions);
82
83 echo(nl2br("Result saved in " . $outputFile . "\n"));
84 }
85 catch(Exception $e) {
86 echo(nl2br("Unable to convert PDF document to Word, error: " . $e->getMessage() . "\n"));
87 }
88
89 //-----------------------------------------------------------------------------------
90
91 try {
92 // Convert PDF document to Excel
93 echo(nl2br("Converting PDF to Excel\n"));
94
95 $outputFile = $outputPath."paragraphs_and_tables.xlsx";
96
97 Convert::ToExcel($inputPath."paragraphs_and_tables.pdf", $outputFile);
98
99 echo(nl2br("Result saved in " . $outputFile . "\n"));
100 }
101 catch(Exception $e) {
102 echo(nl2br("Unable to convert PDF document to Excel, error: " . $e->getMessage() . "\n"));
103 }
104
105 //-----------------------------------------------------------------------------------
106
107 try {
108 // Convert PDF document to Excel with options
109 echo(nl2br("Converting PDF to Excel with options\n"));
110
111 $outputFile = $outputPath."paragraphs_and_tables_second_page.xlsx";
112
113 $excelOutputOptions = new ExcelOutputOptions(); // Convert::ExcelOutputOptions();
114
115 // Convert only the second page
116 $excelOutputOptions->SetPages(2, 2);
117
118 Convert::ToExcel($inputPath."paragraphs_and_tables.pdf", $outputFile, $excelOutputOptions);
119
120 echo(nl2br("Result saved in " . $outputFile . "\n"));
121 }
122 catch(Exception $e) {
123 echo(nl2br("Unable to convert PDF document to Excel, error: " . $e->getMessage() . "\n"));
124 }
125
126 //-----------------------------------------------------------------------------------
127
128 try {
129 // Convert PDF document to PowerPoint
130 echo(nl2br("Converting PDF to PowerPoint\n"));
131
132 $outputFile = $outputPath."paragraphs_and_tables.pptx";
133
134 Convert::ToPowerPoint($inputPath."paragraphs_and_tables.pdf", $outputFile);
135
136 echo(nl2br("Result saved in " . $outputFile . "\n"));
137 }
138 catch(Exception $e) {
139 echo(nl2br("Unable to convert PDF document to PowerPoint, error: " . $e->getMessage() . "\n"));
140 }
141
142 //-----------------------------------------------------------------------------------
143
144 try {
145 // Convert PDF document to PowerPoint with options
146 echo(nl2br("Converting PDF to PowerPoint with options\n"));
147
148 $outputFile = $outputPath."paragraphs_and_tables_first_page.pptx";
149
150 $powerPointOutputOptions = new PowerPointOutputOptions(); // Convert::PowerPointOutputOptions();
151
152 // Convert only the first page
153 $powerPointOutputOptions->SetPages(1, 1);
154
155 Convert::ToPowerPoint($inputPath."paragraphs_and_tables.pdf", $outputFile, $powerPointOutputOptions);
156
157 echo(nl2br("Result saved in " . $outputFile . "\n"));
158 }
159 catch(Exception $e) {
160 echo(nl2br("Unable to convert PDF document to PowerPoint, error: " . $e->getMessage() . "\n"));
161 }
162
163 //-----------------------------------------------------------------------------------
164
165 PDFNet::Terminate();
166 echo(nl2br("Done.\n"));
167}
168
169main();
170?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales