DOCX, XSLX to PDF Conversion - OfficeToPDF - PHP Sample Code

Sample code for using Apryse Server SDK to convert Office documents to PDF (including Word, Excel, PowerPoint and Publisher) without needing any external dependencies or MS Office licenses. Office to PDF conversion can be performed on a Linux or Windows server to automate Office-centric workflows, or entirely in the user's client (web browser, mobile device). The conversion functionality can be combined with our Viewer to display or annotate Office files (docx, xlsx, pptx) on all major platforms, including Web, Android, iOS, Xamarin, UWP, and Windows. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB.

Learn more about our Server SDK and Office Document Conversion Library.

1<?php
2//------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult legal.txt regarding legal and license information.
5//------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7include("../../../PDFNetC/Lib/PDFNetPHP.php");
8include("../../LicenseKey/PHP/LicenseKey.php");
9
10// Relative path to the folder containing the test files.
11$input_path = getcwd()."/../../TestFiles/";
12$output_path = $input_path."Output/";
13
14//------------------------------------------------------------------------------
15// The following sample illustrates how to use the PDF::Convert utility class
16// to convert MS Office files to PDF
17//
18// This conversion is performed entirely within the PDFNet and has *no*
19// external or system dependencies dependencies -- Conversion results will be
20// the same whether on Windows, Linux or Android.
21//
22// Please contact us if you have any questions.
23//------------------------------------------------------------------------------
24
25
26function SimpleDocxConvert($input_filename, $output_filename)
27{
28 global $input_path, $output_path;
29
30 // Start with a PDFDoc (the conversion destination)
31 $pdfdoc = new PDFDoc();
32
33 // perform the conversion with no optional parameters
34 Convert::OfficeToPDF($pdfdoc, $input_path.$input_filename, NULL);
35
36 // save the result
37 $pdfdoc->Save($output_path.$output_filename, SDFDoc::e_linearized);
38
39 // And we're done!
40 echo nl2br("Saved ".$output_filename . "\n");
41}
42
43
44function FlexibleDocxConvert($input_filename, $output_filename)
45{
46 global $input_path, $output_path;
47
48 // Start with a PDFDoc (the conversion destination)
49 $pdfdoc = new PDFDoc();
50
51 $options = new OfficeToPDFOptions(); //ConversionOptions();
52
53 // set up smart font substitutions to improve conversion results
54 // in situations where the original fonts are not available
55 $options->SetSmartSubstitutionPluginPath($input_path);
56
57 // create a conversion object -- this sets things up but does not yet
58 // perform any conversion logic.
59 // in a multithreaded environment, this object can be used to monitor
60 // the conversion progress and potentially cancel it as well
61 $conversion = Convert::StreamingPDFConversion($pdfdoc, $input_path.$input_filename, $options);
62
63 // Print the progress of the conversion.
64 /*
65 echo "Status: "$conversion->GetProgress()*100 . "%, ".
66 $conversion->GetProgressLabel();
67 */
68
69 // actually perform the conversion
70 // this particular method will not throw on conversion failure, but will
71 // return an error status instead
72 while ($conversion->GetConversionStatus() == DocumentConversion::eIncomplete)
73 {
74 $conversion->ConvertNextPage();
75 // print out the progress status as we go
76 /*
77 echo (nl2br("Status: " . $conversion->GetProgress()*100 . "%, ".
78 $conversion->GetProgressLabel() ));
79 */
80 }
81
82 if($conversion->GetConversionStatus() == DocumentConversion::eSuccess)
83 {
84 $num_warnings = $conversion->GetNumWarnings();
85
86 // print information about the conversion
87 for ($i = 0; $i < $num_warnings; ++$i)
88 {
89 echo(nl2br("Conversion Warning: ".$conversion->GetWarningString($i) ));
90 }
91
92 // save the result
93 $pdfdoc->Save($output_path . $output_filename, SDFDoc::e_linearized);
94 // done
95 echo(nl2br("Saved " . $output_filename ."\n"));
96 }
97 else
98 {
99 echo(nl2br("Encountered an error during conversion: " . $conversion->GetErrorString() ));
100 }
101
102}
103
104
105
106
107function main()
108{
109 // The first step in every application using PDFNet is to initialize the
110 // library. The library is usually initialized only once, but calling
111 // Initialize() multiple times is also fine.
112
113 global $LicenseKey;
114 PDFNet::Initialize($LicenseKey);
115 PDFNet::SetResourcesPath("../../../Resources");
116
117 // first the one-line conversion function
118 SimpleDocxConvert("simple-word_2007.docx", "simple-word_2007.pdf");
119
120 // then the more flexible line-by-line conversion API
121 FlexibleDocxConvert("the_rime_of_the_ancient_mariner.docx", "the_rime_of_the_ancient_mariner.pdf");
122 PDFNet::Terminate();
123 echo(nl2br("Done.\n"));
124}
125
126main()
127
128?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales