06
Feb
Written by: Jonathan Maron. Stored in: Samples.

Since the initial release of phpLiveDocx last week, one of the most common questions that I keep receiving via e-mail is:

How can I convert DOC to PDF in PHP?

Although it was not the original intention of phpLiveDocx to offer such file format conversion, it is very possible. In this post, I would like to present a little class, which does exactly that.

In a future version of phpLiveDocx, there will be an additional component called Tis_Service_LiveDocx_Convert, which will offer a more efficient method of document conversion. When this version is released, it will make redundant the code in this post, but until then, here is a really easy way to convert DOC to PDF in PHP.

Note: You can use this technique to convert between any supported template formats to any supported document formats, not just DOC to PDF.

Step 1: Download Zend Framework

Download Zend Framework 1.10 or newer. All source code mentioned in this post is in the directory /demos/Zend/Service/LiveDocx/MailMerge/convert/.

Step 2: Convert DOC to PDF

In this demo application, we use Zend_Service_LiveDocx_MailMerge to convert document.doc to document.pdf.

In a future version of the LiveDocx service, a converter component will be made available.

Consider the following PHP code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$mailMerge = new Zend_Service_LiveDocx_MailMerge();
 
$mailMerge->setUsername('myUsername')
          ->setPassword('myPassword');
 
$mailMerge->setLocalTemplate('document.doc');
 
// necessary as of LiveDocx 1.2
$mailMerge->assign('dummyFieldName', 'dummyFieldValue');
 
$mailMerge->createDocument();
 
$document = $mailMerge->retrieveDocument('pdf');
 
file_put_contents('document.pdf', $document);
 
unset($mailMerge);

The converted file has been saved at document.pdf.

Bookmark and Share

59 Responses to “Convert DOC to PDF in PHP”

  1. 網站製作學習誌 » [Web] 連結分享 Says:

    [...] Convert DOC to PDF in PHP [...]

  2. Hidayat Says:

    hi.. this is awesome, I’m looking for this tools like this for convert my RTF file that was generated from my application. now my application is faster than ever.

    Thank U..

  3. Jonathan Maron Says:

    I am glad you like phpLiveDocx and are using it your application.

    May I ask: What does your application do? How does it use phpLiveDocx?

  4. Benito Estrada Says:

    Hi,

    Thanks a lot for your work!

    This stuff is real useful!

  5. Goo Says:

    Can it conver excel. ppp to PDF?
    Away

  6. Jonathan Maron Says:

    phpLiveDocx converts and merges word processing file formats only. It does not support PowerPoint, nor Excel.

  7. Ivan Says:

    Unfortunately, this doesn’t work for me. I have installed Zend framework but when i run test script it simply prints ERROR (Converting document.doc to document.pdf… ERROR).

    How can I go deeper into error to see what exactly went wrong?

  8. Jonathan Maron Says:

    I think your web server environment may be the problem. Can I please ask you to refer to this blog post, which describes the “check-environment.php” script:

    http://tinyurl.com/mmqdj7

    However, to answer your question. Just open up “Converter.php” and replace line 44:

    } catch (Tis_Service_LiveDocx_Exception $e) { }

    with

    } catch (Tis_Service_LiveDocx_Exception $e) {

    Zend_Debug::dump($e);

    }

    That will show you the exception that is being thrown.

  9. Rahul Says:

    I dont have Zend Framework installed. I simply wish to use the code above without Zend Framework. Is that possible??

  10. Jonathan Maron Says:

    Hello Rahul. Yes, it is! You can use LiveDocx without the Zend Framework. I will be publishing a brief tutorial showing how in the next few days. However, installing the ZF is no hassle. Regards, Jonathan Maron

  11. Jonathan Maron Says:

    Hello again

    Please take a look at this article, which shows how to use LiveDocx in PHP without the Zend Framework:

    Does this work for you?

    Jonathan Maron

  12. Convert DOCX, DOC & RTF to HTML in PHP Says:

    [...] phpLiveDocx 1.2, it is possible to generate documents in XHTML 1.0 Transitional. Thus, you can now convert any supported template file format (DOCX, DOC and RTF) to [...]

  13. Nandish Says:

    Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in /home/unosoft/public_html/silibi/phplivedocx/library/Tis/Service/LiveDocx/MailMerge.php on line 116

  14. Jonathan Maron Says:

    Hello Nadish

    Are you sure that your web server has the correct permission to access the file? Did you try ‘chmod 775′?

    Can you see the contents of the file?

    Did you set the include path as described at http://is.gd/2qff8 ?

    Have you run the check-environment.php script, as described at http://is.gd/2qfme ?

    Pls post the output into the support forum http://is.gd/2qfqS .

    Thank you :-)

    Jonathan

  15. prokvk Says:

    Hi Jonathan,

    I have a problem. I followed all instructions and by what i can tell i got it to work. When i run my script i get the message:

    Converting file.doc to file.pdf…

    but nothing else no DONE nor ERROR is printed.

    Also the pdf file is not created.

    I used just this code you published there.

    Thanky you for your answer

  16. prokvk Says:

    I noticed now that i have 2 fails in the check-enviroment script.

    2: Checking SAPI (apache2handler)……………………………………FAIL
    3: Checking PHP version (5.2.2)……………………………………..FAIL

    But for the start …. how does my php 5.2.2 fail ??

  17. Jonathan Maron Says:

    Hello Prokvk

    Can you please open up “Converter.php” in a text editor and replace:

    } catch (Tis_Service_LiveDocx_Exception $e) { }

    with

    } catch (Tis_Service_LiveDocx_Exception $e) {

    Zend_Debug::dump($e);

    }

    That will show you the exception that is being thrown.

    Please post this error here.

  18. Jonathan Maron Says:

    Hello Prokvk

    Zend recommends the most current release of PHP for critical security and performance enhancements. Zend Framework currently supports PHP 5.2.4 or later. Hence, your version of PHP is too old.

    More info here: http://is.gd/2rqCs

  19. prokvk Says:

    Thank you for your response Jonathan .. I got it to work switching to newer version of PHP :)

  20. akhil kumar Says:

    is it necessary……………….to work on Zend framework to use “phplivedocx”.If not then tell me how can i use phplivedocx in WAMP.

  21. akhil kumar Says:

    I used this code and found this error ………

    #!/usr/bin/php Converting document.doc to document.pdf…
    Fatal error: Uncaught exception ‘Tis_Service_LiveDocx_Exception’ with message ‘Cannot connect to LiveDocx service at https://api.livedocx.com/1.2/mailmerge.asmx?WSDL // SOAP extension is not loaded.’ in C:\wamp\www\doc2pdf\phplivedocx\library\Tis\Service\LiveDocx.php:187 Stack trace: #0 C:\wamp\www\doc2pdf\phplivedocx\library\Tis\Service\LiveDocx.php(88): Tis_Service_LiveDocx::throwException(Object(Zend_Soap_Client_Exception), ‘Cannot connect …’) #1 C:\wamp\www\doc2pdf\phplivedocx\library\Tis\Service\LiveDocx\MailMerge.php(83): Tis_Service_LiveDocx->initSoapClient(‘https://api.liv…’) #2 C:\wamp\www\doc2pdf\phplivedocx\samples\Converter.php(38): Tis_Service_LiveDocx_MailMerge->__construct(”, ”) #3 C:\wamp\www\doc2pdf\phplivedocx\samples\mail-merge\convert\convert-document.php(20): Converter::convert(‘C:\wamp\www\doc…’, ‘pdf’) #4 C:\wamp\www\doc2pdf\doc2pdf.php(2): include_once(‘C:\wamp\www\doc…’) #5 {main} thrown in C:\wamp\www\doc2pdf\phplivedocx\library\Tis\Service\LiveDocx.php on line 187

    Can u short out the problem…..please.

  22. Jonathan Maron Says:

    Hello Akhil

    You do not have the SOAP extension in your PHP installation. Please consult the documentation of WAMP or ask your system administrator to find out how to install and activate SOAP in your PHP installation.

    Once you have done this, can I please ask you to follow the steps described at http://is.gd/3pJ77 and then post the output of check-environment.php here in the blog.

    Thanks

    Jonathan Maron

  23. Jonathan Maron Says:

    Hello Akhill

    You do not have to use the Zend framework to use phpLiveDocx. There are two other methods. Please refer to point 2 and 3 at http://is.gd/3pJm2 .

    Thanks

    Jonathan Maron

  24. akhil kumar Says:

    I am using third method, to convert doc to pdf i.e [SOAP connection via PHP 4 and PHP 5 NuSOAP.]

    When I run the script ,two PDF files get created and a display on screen is [Starting sample #1 (license-agreement)...DONE. Starting sample #2 (telephone-bill)...DONE.].

    But when I open the PDF file ,it shows ERROR MSG: [Format error-not a pdf file or corrupted].

    I have done all the setting like enabling SOAP, I have also downloaded the NuSOAP and kept in the same folder in which I have to run the script. PLEASE HELP ME TO SOLVE THE PROBLEM.

    My code is same as u have given on ur site, I have only changed the password and user name. U CAN SEE:

    [...]

    Code taken from http://is.gd/3wK7G

  25. Jonathan Maron Says:

    Which PDF viewing problem are you using?

    How many bytes are the produced PDF files?

    Can you please send me the produced PDF files?

    E-Mail them to support @ textcontrol . com and write my name in the e-mail body.

    Thanks!

  26. akhil kumar Says:

    sorry…….i am not able to send the PDF file as it size is showing 0KB. now what i should do.

  27. Jonathan Maron Says:

    It seems that you are writing files to disk with no content. I think that the issues you are having are related to NuSOAP. I would suggest that you post a support case in the NuSOAP forum: http://is.gd/3wPKP

  28. akhil kumar Says:

    “writing files to disk with no content” Is this problem because i am running the code on localhost?

  29. Jonathan Maron Says:

    No. The problem is that NuSOAP is not getting any data (0 bytes). You are then writing this 0 bytes to the hard disk, hence the resulting PDF files are 0 bytes in size. What did the NuSOAP community say?

  30. akhil kumar Says:

    i am not able to find NuSOAP community. Your BLOG is too good , because i am getting the reply very soon.Thanks for this service.
    Please see the path setting of my NuSOAP in my php file:-
    /////////////////
    set_include_path (dirname(__FILE__) . ‘/nusoap-0.7.3/lib’);
    /////////////////

    my NuSOAP folder name is nusoap-0.7.3 and it is in the same folder from where i am running the script.IS it fine?and give me idea to handle this problem by my self.

  31. Jonathan Maron Says:

    Here is the NuSOAP community – http://is.gd/3wPKP – click on “Help” in the section “Project Forums”. Please post your issue in this forum. The NuSOAP community is specialized in NuSOAP. We are specialized in the Zend Framework component.

  32. Yung Says:

    This tool was very usefull in a project I´m working.
    Thank you!

  33. Ramakrishnan Says:

    Hi,
    i want to convert pdf/doc to html in a webpage.
    Then how to do this in php

    Thanks
    Ramakrishnant

  34. Vlad Says:

    Can I convert Doc – > PDF on your phpLiveDocx without the Internet?
    Thanks

  35. pdizz Says:

    Hey, I like the ability of this to work.. but I don’t wish to do this with actual files. I was wondering, is it possible to pipe data from a mysql BLOB (that I have files stored in…) and use the converter?

  36. สุขภาพ Says:

    Hello Sir,

    It’s very useful for me.

    But I have one problem before. I want to convert from html to pdf you have anyway because now I convert html to doc and I will convert doc to pdf.

    I weak in English.

    If you have way to solution my problem. Please send email to me.

    Thank.

  37. Jonathan Maron Says:

    @สุขภาพ: Try converting from HTML to internal TX Text Control format (tx) and then to DOC. I know that involves 2 conversions, but there is no other way to do this, using LiveDocx at the moment.

  38. Jai Says:

    Hi,

    I used above code, but i got SOAP-ERROR: Parsing WSDL from http://api.livedocx.com/1.2/mailmerge.asmx?WSDL.

    please give me solution.

    Thanks.

  39. Jonathan Maron Says:

    The WSDL is located at https://api.livedocx.com/1.2/mailmerge.asmx?wsdl

  40. Jai Says:

    When i executed ‘check-environment.php’ i got 2 fail msg

    phpLiveDocx Environment Checker
    1: Checking OS (WINNT)……………………………………………..PASS
    2: Checking SAPI apache2handler)……………………………………FAIL
    3: Checking PHP version (5.2.5)……………………………………..PASS 4: Checking memory limit (32M)………………………………………PASS 5: Checking HTTP stream rapper……………………………………..PASS
    6: Checking HTTPS stream rapper…………………………………….FAIL 7: Checking Zend Framework path……………………………………..PASS
    8: Checking Zend Framework version (1.9.6)……………………………PASS
    9: Checking SOAP extension………………………………………….PASS 10: Checking DOM extension…………………………………………..PASS 11: Checking SimpleXML extension……………………………………..PASS 12: Checking libXML extension………………………………………..PASS 13: Checking your IP address (124.125.46.22)…………………………..PASS 14: Checking your city (Gulbarga)…………………………………….PASS 15: Checking your region (Karnataka)………………………………….PASS 16: Checking your country (India)…………………………………….PASS

  41. Jai Says:

    Can u give me solution for those above 2 fail msg.

    Thanks

  42. Nisarg Says:

    thanks. i am ver thank ful. pleas, also keep the .ppxt to .ppt convertor

  43. Jonathan Maron Says:

    LiveDocx is all about word processing and word processing formats. Therefore, we currently do not support these file formats.

  44. Solow Says:

    I’m having some trouble getting started… I can’t find the files. I downloaded Zend framework, but I can’t find them anywhere. Help would be well appreciated.

    solow

  45. jeanson Says:

    Hello everybody,

    I’m trying to use the example code of converting doc to pdf but I always get :”Fatal error: Class ‘Converter’ not found “. I’ve downloaded the Zend framework and i run successfully the check-environment.php.
    It looks like I can’t find the Converter class. Where is it located ?
    Could someone help me, please ?

  46. Jonathan Maron Says:

    @Solow Go to http://www.zendframework.com/download/latest and download “1.10.0 Full”. Zend_Service_LiveDocx is contained within!

  47. Jonathan Maron Says:

    @jeanson You can download Converter.php here: http://pastebin.com/f43bf23ed. However, I have simplified the above post so that it no longer requires the said file :-)

  48. Solow Says:

    So, Zend 1.9.7 full doesn’t have phpLiveDocx?

  49. Jonathan Maron Says:

    Correct! It is only available as of ZF 1.10. You can read the documentation here: http://framework.zend.com/manual/1.10/en/zend.service.livedocx.html

  50. Solow Says:

    ZendFramework-1.10.0rc1

    I downloaded it. I hope I’m not to annoying but, How do I use this? I NEVER used zend before. so I have absolutely no idea what to do. everybody keeps giving me url’s, which don’t make any sence. I just need someone explaining to me what it does exactly, and how to use it. Do I upload ALL files? or just some I use? or… whatever :P

    I hope u can do this for me :)

  51. Solow Says:

    Ah never mind. I installed zend, now I want to use this, to convert my doc to pdf.

    couple of questions: What if the file is docx?
    How do I convert *now I finally installed zend.*
    $phpLiveDocx->setUsername(‘username’)
    ->setPassword(‘password’);
    what is this?

  52. dexta Says:

    dear Jonathan,

    Though I got to like the application, is there a way to have the zend engine stripped off the un-necessary parts [eg currency converter, json etc] just to have base components and the converter, well of course keeping the ease of use as is currently.

  53. Vinay Says:

    hi Jonathan

    i am able to use this code on localhost but when i use it on server it says Fatal error: Class ‘Zend_Service_LiveDocx_MailMerge’ not found in..
    Plz Help

  54. Jonathan Maron Says:

    @Vinay: This is probably because your include path on the server is different from that on your localhost. Update the include path to fix the problem.

  55. uks Says:

    Hello,

    In my application , I have a form in which user enter values and at the end of the process we have to generate a pdf so that user can take print out. I am using dompdf html to pdf converter in php. Since the form is rich in images and lots of nested tables it is not working properly. The form html is generated from a ms word file, which is the paper form. Will LiveDocx will help me ? or any other suggestions ?

    Thanks
    uks

  56. Jonathan Maron Says:

    @uks This is exactly a use-case for LiveDocx. To get started, please take a look at the documentation. It contains many sample applications:

    http://www.phplivedocx.org/articles/brief-introduction-to-phplivedocx/
    http://www.phplivedocx.org/articles/pdf-generation-with-zend-framework/
    http://www.zendframework.com/manual/en/zend.service.livedocx.html

  57. Raza Says:

    Hello plz tell me what is the problem i got this error

    Fatal error: Call to undefined method Zend_Locale::setdefault() in C:\xampp\htdocs\Converter\common.php on line 17

    Its urgent

  58. Asep Khoirudin Says:

    can i get script converter php to pdf

  59. subh Says:

    I have a little complicated word files with the nested tables inside. The placement of words is messed up in pdf files. I am using free version. Could be better if i use premium package?.

    Please

Leave a Reply