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.
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.