Subscribe to News feed

Using PHP to Convert files to PDF (and other formats) with the Muhimbi PDF Converter

Posted at: 18:22 on 07 February 2013 by Muhimbi

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.

 

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’ Sample Code folder. You can also generate your own proxies using the following steps:

  1. Download wsdl2phpgenerator and unzip it to a location of your choice.
  2. Make sure PHP is added to your path.
  3. Open a command prompt and navigate to the location where wsdl2phpgenerator was unzipped.
  4. Execute the following command to generate the PHP proxies:

    php wsdl2php.php -s -i "http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl" -o documentConverterServices

    If the Conversion Service is running on a remote machine, then please replace ‘localhost’ with the name or IP of that machine.
  5. 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.

.

Labels: , , ,

14 Comments:

  • Fatal error: Class 'SoapClient' not found in C:\wamp\www\PHP-Sample-Code\documentConverterServices.php on line 20

    By Anonymous Anonymous, At 05 April, 2014 03:42  

  • Hi Anonymous, please drop support@muhimbi.com a line if you need assistance with the PHP sample code. This example is known to work well, but - as we are no PHP experts - perhaps there is an environmental issue that needs tweaking on your side.

    By Blogger Muhimbi, At 06 April, 2014 08:55  

  • hi,

    I want php code for pdf to excel converter

    By Blogger Jafar m, At 02 April, 2015 09:34  

  • Hi Jafar,

    I am afraid that out-of-the box our software converts Excel to PDF. In order to convert PDF to Excel you need a 3rd party add-on as per http://blog.muhimbi.com/2014/07/choices-choices-choices-pdf-to-xlsx-pdf.html

    By Blogger Muhimbi, At 02 April, 2015 09:56  

  • Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl' : failed to load external entity "http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl" in C:\xampp\htdocs\sample\documentConverterServices.php:99 Stack trace: #0 C:\xampp\htdocs\sample\documentConverterServices.php(99): SoapClient->SoapClient('http://localhos...', Array) #1 C:\xampp\htdocs\sample\convert.php(64): DocumentConverterService->__construct(Array, 'http://localhos...') #2 {main} thrown in C:\xampp\htdocs\sample\documentConverterServices.php on line 99

    By Blogger RAGHVENDRA KUMAR, At 10 June, 2015 08:52  

  • Hi Raghvendra,

    It sounds like you are running the PHP code on a system that is not running our Conversion Service. Please install the Muhimbi PDF Converter Services on one of your systems. If it is the same system that you run your PHP code on then it should work fine out of the box. If the Conversion Service is running on a remote system then change 'localhost' in the code to the name or ip-address of the appropriate system.

    If you have any further questions then please contact support@muhimbi.com.

    By Blogger Muhimbi, At 10 June, 2015 09:41  

  • Hi Muhimbi,

    We have encountered a problem upon executing the following command to generate the PHP proxies:

    php wsdl2php.php -s -i "http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl" -o documentConverterServices

    The error says that :

    Fatal error: Cannot redeclare class Generator in "Path of the wsdl2phpgenerator-master" \src\Generator.php on line 30

    Also there have an error "Class 'SoapClient' not found " upon converting a document using index.php.
    We already applied the instruction from the "https://support.muhimbi.com/entries/23660787-Generating-Web-Service-proxies-against-a-remote-machine-fails" but we still getting the same error.

    Please note that when we run "http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl" on the browser, It is running correctly and give you XML an output data.

    We are using PHP 5.5.38.

    Thanks for your help

    By Anonymous Anonymous, At 14 September, 2016 09:29  

  • Hi,

    I am afraid we are no PHP or wsdl2php experts. However, our software comes with pregenerated PHP proxies (in the 'Sample Code' folder), why not use those?

    If you are getting warnings when executing our sample then see the 'troubleshooting' section (in red) in the main blog post above.

    I am afraid that we cannot provide PHP programming assistance beyond our sample code, but feel free to reach out to support@muhimbi.com if you have any further questions.

    By Blogger Muhimbi, At 14 September, 2016 09:39  

  • Is there any way that we can install this application via composer, bcz .exe file will not be run on server. If possible pls provide me the command which we can run this on server.
    Thank you...

    By Blogger Anik Roy, At 16 February, 2017 06:30  

  • Hi Anik,

    I am afraid that is not possible. You need to install this software on a Windows Server. The good news is that it can be any Windows Server, once installed you can access the API remotely from another systems.

    What may be a better option is to use our new Hosted Facility (We host the servers) and access the various functionality via a REST interface. We are about to release this, please contact support@muhimbi.com if you would like more details.

    By Blogger Muhimbi, At 16 February, 2017 08:58  

  • This error shows on my page i am installed on myhosting. what i do with script please tell me ASAP.

    Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl' : failed to load external entity "http://localhost:41734/Muhimbi.DocumentConverter.WebService/?wsdl" in /home/fdcpa/public_html/demo/test/documentConverterServices.php:99 Stack trace: #0 /home/fdcpa/public_html/demo/test/documentConverterServices.php(99): SoapClient->SoapClient('http://localhos...', Array) #1 /home/fdcpa/public_html/demo/test/convert.php(64): DocumentConverterService->__construct(Array, 'http://localhos...') #2 {main} thrown in /home/fdcpa/public_html/demo/test/documentConverterServices.php on line 99

    By Blogger Unknown, At 17 April, 2017 21:22  

  • Hi,

    Did you follow the full instructions above and deploy our Conversion Service to one of your servers as well? It looks like you just copied the script without deploying the prerequisites.

    If you prefer a hosted service (where you don't need to install anything) then please check out our REST based service. Many PHP samples can be found at https://github.com/Muhimbi/PDF-Converter-Services-Online/tree/master/clients/v1/php

    By Blogger Muhimbi, At 18 April, 2017 09:51  

  • Fatal error: Uncaught ArgumentCountError: Too few arguments to function OpenOptions::__construct(), 0 passed in C:\xampp\htdocs\test_code\converter\convert.php on line 15 and exactly 3 expected in C:\xampp\htdocs\test_code\converter\documentConverterServices.php:3089 Stack trace: #0 C:\xampp\htdocs\test_code\converter\convert.php(15): OpenOptions->__construct() #1 {main} thrown in C:\xampp\htdocs\test_code\converter\documentConverterServices.php on line 3089

    By Blogger adi mahajan, At 12 December, 2018 07:24  

  • Hi Adi,

    Thanks for getting in contact. Not sure why you are getting a problem, perhaps not all prerequisites are in place.

    I have uploaded a slightly different version of the sample code that suppresses warnings, in case your own php.ini is a bit strict about this.

    Also, please make sure that, as per the message in the sample conversion screen, the PHP Soap libraries are installed, https://stackoverflow.com/questions/11391442/fatal-error-class-soapclient-not-found

    I have verified everything works correctly by downloading the Muhimbi Conversion Service and installing it on a Windows server (which is a requirement) (http://www.muhimbi.com/Products/PDF-Converter-Services/summary.aspx)

    I then downloaded the sample code linked to in the main article (http://www.muhimbi.com/Images---Files/Blog/PHP-Sample-Code.aspx) and installed it on a Windows Server with PHP enabled. In this case the Conversion Service and PHP sample code are located on the same machine, if they are on different systems edit 'convert.php' and change 'localhost' with the name or IP of the server that runs the conversion Service.

    I have also double checked it works correctly on an Ubuntu Linux System (16.04) with PHP 7.0.32.

    The steps used are as follows:
    mkdir pdf_convert
    cd pdf_convert
    curl -o muhimbi_php.zip http://www.muhimbi.com/Images---Files/Blog/PHP-Sample-Code.aspx
    unzip muhimbi_php.zip
    nano convert.php
    # Search (ctrl-W) for localhost and change it to the name / ip-address of the server that runs the conversion service (if on a different machine)
    # Save changes (ctrl-o) and exit (ctrl-x)
    php -S localhost:8080 -t . -c .
    # Open a web browser http://localhost:8080/. Browser for a file and click the button.

    By Blogger Muhimbi, At 12 December, 2018 14:15  

Post a Comment

Subscribe to Post Comments [Atom]

Need support from experts?

Access our Forum

Download Free Trials