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.
February 12th, 2009 at 07:32
[...] Convert DOC to PDF in PHP [...]
April 3rd, 2009 at 07:48
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..
April 3rd, 2009 at 10:40
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?
May 20th, 2009 at 01:52
Hi,
Thanks a lot for your work!
This stuff is real useful!
July 28th, 2009 at 05:54
Can it conver excel. ppp to PDF?
Away
July 30th, 2009 at 07:29
phpLiveDocx converts and merges word processing file formats only. It does not support PowerPoint, nor Excel.
August 3rd, 2009 at 12:53
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?
August 3rd, 2009 at 13:52
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.
August 8th, 2009 at 09:24
I dont have Zend Framework installed. I simply wish to use the code above without Zend Framework. Is that possible??
August 10th, 2009 at 05:44
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
August 10th, 2009 at 09:12
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
August 13th, 2009 at 06:41
[...] 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 [...]
August 20th, 2009 at 15:15
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
August 20th, 2009 at 16:04
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
August 20th, 2009 at 20:34
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
August 20th, 2009 at 21:54
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 ??
August 21st, 2009 at 08:42
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.
August 21st, 2009 at 08:45
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
September 14th, 2009 at 11:05
Thank you for your response Jonathan .. I got it to work switching to newer version of PHP :)
September 18th, 2009 at 13:53
is it necessary……………….to work on Zend framework to use “phplivedocx”.If not then tell me how can i use phplivedocx in WAMP.
September 18th, 2009 at 15:01
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.
September 18th, 2009 at 15:07
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
September 18th, 2009 at 15:09
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
September 21st, 2009 at 15:20
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
September 21st, 2009 at 15:32
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!
September 21st, 2009 at 15:40
sorry…….i am not able to send the PDF file as it size is showing 0KB. now what i should do.
September 21st, 2009 at 16:13
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
September 22nd, 2009 at 07:27
“writing files to disk with no content” Is this problem because i am running the code on localhost?
September 22nd, 2009 at 07:53
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?
September 22nd, 2009 at 09:26
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.
September 22nd, 2009 at 09:35
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.
September 30th, 2009 at 21:20
This tool was very usefull in a project I´m working.
Thank you!
November 15th, 2009 at 11:17
Hi,
i want to convert pdf/doc to html in a webpage.
Then how to do this in php
Thanks
Ramakrishnant
November 23rd, 2009 at 10:28
Can I convert Doc – > PDF on your phpLiveDocx without the Internet?
Thanks
November 26th, 2009 at 05:39
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?
December 8th, 2009 at 11:22
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.
December 8th, 2009 at 13:00
@สุขภาพ: 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.
January 8th, 2010 at 14:29
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.
January 8th, 2010 at 14:59
The WSDL is located at https://api.livedocx.com/1.2/mailmerge.asmx?wsdl
January 9th, 2010 at 08:21
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
January 9th, 2010 at 08:23
Can u give me solution for those above 2 fail msg.
Thanks
January 20th, 2010 at 09:52
thanks. i am ver thank ful. pleas, also keep the .ppxt to .ppt convertor
January 20th, 2010 at 09:55
LiveDocx is all about word processing and word processing formats. Therefore, we currently do not support these file formats.
January 24th, 2010 at 21:36
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
January 25th, 2010 at 01:13
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 ?
January 25th, 2010 at 06:43
@Solow Go to http://www.zendframework.com/download/latest and download “1.10.0 Full”. Zend_Service_LiveDocx is contained within!
January 25th, 2010 at 06:52
@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 :-)
January 25th, 2010 at 07:46
So, Zend 1.9.7 full doesn’t have phpLiveDocx?
January 25th, 2010 at 08:12
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
January 25th, 2010 at 10:48
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 :)
January 27th, 2010 at 08:38
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?
February 15th, 2010 at 13:06
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.
March 15th, 2010 at 09:51
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
March 17th, 2010 at 09:20
@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.
March 23rd, 2010 at 14:48
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
March 31st, 2010 at 15:06
@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
July 6th, 2010 at 15:31
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
August 6th, 2010 at 10:53
can i get script converter php to pdf
August 13th, 2010 at 23:36
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