In order to convert .doc file to .pdf file in cake PHP I have used ZEND library here. Following steps are needed to be done:
- You need to make sure SOAP extension is enable on your server.
- Use ZEND library as third party. Put ZEND library inside the vendor folder in app folder.
- Create an account here which will be used in controller
https://www.livedocx.com/user/account_registration.aspx
- Inside app/Vendor folder create a file named zend_include_path.php. Now open this file and write the following code inside it:
<?php ini_set('include_path',ini_get('include_path').PATH_SEPARATOR . dirname(__FILE__));?>
In the controller write the following code:
YourController:
class UsersController extends AppController {
var $name = 'Users';
function convertDocToPdf(){
// set the document path
$docPath = 'path to test.docx';
// set the pdf path
$PdfPath = 'path to pdf';
// import library
App::import('Vendor', 'zend_include_path');
App::import('Vendor', 'Zend_Service_LiveDocx_MailMerge', true, false, 'Zend/Service/LiveDocx/MailMerge.php');
App::import('Vendor', 'Zend_Service_LiveDocx_Exception', true, false, 'Zend/Service/LiveDocx/Exception.php');
// call the class mailmerge
$mailMerge = new Zend_Service_LiveDocx_MailMerge();
// sets the login username & password here to access services
$mailMerge->setUsername('username')->setPassword('*****');
$mailMerge->setLocalTemplate($docPath); // sets the docx file.
$mailMerge->createDocument();
$document = $mailMerge->retrieveDocument('pdf');
if(file_put_contents($PdfPath, $document)){
return true;
}else{
return false;
}
}
}
Now call the function convertDocToPdf().
Please remember to provide the doc file path to be converted and put the credentials you got after registering in the link provided above.
0 Comment(s)