PHP – convert Microsoft word doc to pdf
by Gabi SolomonA lot of people are searching for solution to do this conversion on the fly. I dont know any other programing languages except PHP so i will write only about how you can do this in PHP.
Now to do this there is 2 major situations:
1. Doing it on a windows Server
2. Doing it on a linux Server
In both cases the chanses are that you need to have root access to the server in order to do all the settings required for this conversion.
In case of windows you can use COM objects to acces either Word or OpenOffice and do the conversion. Here peek of a code to access OpenOffice API to do the conversion:
-
<?php
-
function MakePropertyValue($name,$value,$osm){
-
$oStruct = $osm->Bridge_GetStruct("com.sun.star.beans.PropertyValue");
-
$oStruct->Name = $name;
-
$oStruct->Value = $value;
-
return $oStruct;
-
}
-
function word2pdf($doc_url, $output_url){
-
//Invoke the OpenOffice.org service manager
-
$osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n");
-
//Set the application to remain hidden to avoid flashing the document onscreen
-
//Launch the desktop
-
$oDesktop = $osm->createInstance("com.sun.star.frame.Desktop");
-
//Load the .doc file, and pass in the "Hidden" property from above
-
$oWriterDoc = $oDesktop->loadComponentFromURL($doc_url,"_blank", 0, $args);
-
//Set up the arguments for the PDF output
-
//Write out the PDF
-
$oWriterDoc->storeToURL($output_url,$export_args);
-
$oWriterDoc->close(true);
-
}
-
$output_dir = "C:/dev/openofficeintegration/docconverter/";
-
$doc_file = "C:/dev/openofficeintegration/docconverter/DpmR5Reqv1.20.doc";
-
$pdf_file = "DpmR5Reqv1.20.pdf";
-
$output_file = $output_dir . $pdf_file;
-
$doc_file = "file:///" . $doc_file;
-
$output_file = "file:///" . $output_file;
-
word2pdf($doc_file,$output_file);
-
?>
Now in case of linux things get a little more tricky, not by much but they do. You cant user COM objects any more but you can still install OpenOffice and its API in extension with a python script.
PyODConverter, for Python OpenDocument Converter, is a Python script that automates office document conversions from the command line using OpenOffice.org.
The script does basically the same thing as the command line tool that comes with JODConverter but is much simpler. In fact the Python script was released for the people who use JODConverter only from the command line (not as a Java library or web service) and would like a simpler alternative.
To check out the full documentation and download the script go here.
Covertor for php5
As i was doing research to do this post i found out of yet another solution, but this is only available for php5, but it works for both lynux and windows. Its a php module written in C++ called Punno.
This project is a PHP5 module written in C++ that brings the OpenOffice.org UNO Programming API to the PHP userspace.
You can use it to write scripts that create, modify, read and save OpenOffice.org documents (Writer, Spreadsheet, Drawing). Also, you can export these documents in various formats, like PDF or HTML for example.
It can be installed on any Linux/Unix or Windows platform where PHP5 and OpenOffice.org are also available.
It is released under PHP License 3.01.
That is it.
Do you know of any other solutions to do a doc to pdf conversion using php ?
Related Posts
-
santy
-
Gabi Solomon
-
santy

