Convert DOC to PDF in PHP


Since the initial release of LiveDocx 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 LiveDocx 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 LiveDocx, there will be an additional component called Zend_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.

Copy from http://www.phplivedocx.org/2009/02/06/convert-doc-to-pdf-in-php/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s