How to Convert Files to PDF using PHP
In this article we explain how to use PHP to convert MS-Word, Excel, PowerPoint, HTML and other common file formats to PDF. Read on to learn more about the Muhimbi PDF Converter and how it can assist with the Conversion, Merging, Watermarking, Splitting, Securing and OCR of documents.**
Please note that the example provided in this post requires the Muhimbi PDF Converter Services to be deployed to at least one Windows based server in your environment, after which you can access it from PHP using Windows and non-Windows based systems including Linux. If you do not have access to any Windows servers then please use our REST based cloud service, which requires no custom software deployment. For details see the examples on GitHub or check out guides here.**
Install PHP & PHP Soap Libraries
If you are reading this then you probably already have PHP installed. If not, then you can find details for installing PHP for various Ubuntu distributions as well as Windows on our GitHub sample page.
Please note that, as this solution is Web Services based, you will need to install the PHP Soap Libraries. Instructions can be found on our GitHub sample page
Installing the Muhimbi PDF Converter Services
The provided PHP sample code by itself does very little. It requires a copy of the Muhimbi PDF Converter Services to carry out the actual work. The PHP sample code simply shows how to connect to the conversion service and send commands to it.
So, as a first step Download and Install the Muhimbi PDF Converter Services on a Windows Server as described in Chapter 2 of the Administration guide.
Please use the included Diagnostics Tool to verify that your installation is correct.
Download the sample code
The PHP Sample code is deployed alongside the Muhimbi PDF Converter Services. You can access it via the Windows Start Menu. However, it may be easier to download the latest sample code directly from GitHub as described here.
Running the sample code
With the prerequisites installed, and the sample code downloaded, it is time to run the example. For details about how to do this, read these instructions in our GitHub repository. It is all very straight forward, just make sure you read the instructions, especially when you are running the PHP sample from a system that is not the one that is running the Conversion Service itself.
The sample provided is fairly basic. It converts a file to PDF and applies a couple of test watermarks. The full object model, including details about merging, splitting and watermarking files, is available in the Developer Guide.
If you need more information, or get stuck, then please drop us a line as we love to help.
Generating proxies
The previous sections describe how to access and execute the sample code. The following is only provided for those people who want to know more and generate their own Web Service proxy classes in PHP. Everyone else can stop reading now.
Although out-of-the-box PHP comes with a SoapClient class to interact with web services, it is much easier to pre-generate proxy classes to talk to the web service.
Several tools are available for generating PHP proxies. The one that we are using in this tutorial is wsdl2phpgenerator. Pre-generated proxies are included in the Muhimbi PDF Converter Services’s Sample Code folder. You can also generate your own proxies using the following steps:
-
Download wsdl2phpgenerator and unzip it to a location of your choice.
-
Make sure PHP is added to your path.
-
Open a command prompt and navigate to the location where wsdl2phpgenerator was unzipped.
-
Execute the following command to generate the PHP proxies:
php wsdl2php.php -s -i "https://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl" -o documentConverterServices ```vices If the Conversion Service is running on a remote machine, then please replace ‘localhost’ with the name or IP of that machine.
-
Copy the newly generated documentConverterServices.php file to the folder that holds your PHP code.
SOAP / Web Service Debugging
The Muhimbi Conversion Service is a Windows Service based on the Microsoft Windows Communication Foundation (WCF) framework. This comprehensive framework is used to expose a standards based Web Services interface that can be consumed by many different platforms including .NET, Java, PHP, SAP, Documentum and many others.
Even though WCF Web Services are standards-based, standards are not interpreted the same by everyone so from time to time you may need to do some troubleshooting when programming against the PDF Converter Web Service, especially from non-Microsoft platforms.
For details about how to debug Web Service / SOAP messages, see this Knowledge Base Article.
If you have any questions then leave a comment below or contact us.
Clavin is a Microsoft Business Applications MVP who supports 1,000+ high-level enterprise customers with challenges related to PDF conversion in combination with SharePoint on-premises Office 365, Azure, Nintex, K2, and Power Platform mostly no-code solutions.