Convert Doc/Docx Files to Pdf Using PHP

Convert Word doc, docx and Excel xls, xlsx to PDF with PHP

I found a solution to my issue and after a request, will post it here to help others. Apologies if I missed any details, it's been a while since I worked on this solution.

The first thing that is required is to install on the server. I requested my hosting provider to install the open office RPM on my VPS. This can be done through WHM directly.

Now that the server has the capability to handle MS Office files you are able to convert the files by executing command line instructions via PHP. To handle this, I found PyODConverter:

I created a directory on the server and placed the PyODConverter python file within it. I also created a plain text file above the web root (I named it "adocpdf"), with the following command line instructions in it:

if [ "`ps ax|grep -v grep|grep -c $SERVICE`" -lt 1 ]; then
/usr/bin/soffice -headless -accept="socket,host=,port=8100;urp;" -nofirststartwizard &
sleep 5s
python /home/website/python/ /home/website/$directory$filename$extension /home/website/$directory$filename.pdf

This checks that the libraries are running and then calls the PyODConverter script to process the file and output it as a PDF. The 3 variables on the first three lines are provided when the script is executed from with a PHP file. The delay ("sleep 5s") is used to ensure that has enough to time to initiate if required. I have used this for months now and the 5s gap seems to give enough breathing room.

The script will create a PDF version of the document in the same directory as the original.

Finally, initiating the conversion of a Word / Excel file from within PHP (I have it within a function that checks if the file we are dealing with is a word / excel document)...

$output = array();
$return_var = 0;
exec("/opt/adocpdf {$directory} {$filename} {$extension}", $output, $return_var);

This PHP function is called once the Word / Excel file has been uploaded to the server. The 3 variables in the exec() call relate directly to the 3 at the start of the plain text script above. Note that the $directory variable requires no leading forward slash if the file for conversion is within the web root.

OK, that's it! Hopefully this will be useful to someone and save them the difficulties and learning curve I faced.

Convert Doc/Docx Files To PDF using PHP

Open Office and This Code Helps me :)

function MakePropertyValue($name,$value,$osm){
$oStruct = $osm->Bridge_GetStruct("");
$oStruct->Name = $name;
$oStruct->Value = $value;
return $oStruct;
function word2pdf($doc_url, $output_url){
//echo $output_url;
//Invoke the service manager
$osm = new COM("") or die ("Please be sure that is installed.\n");
//Set the application to remain hidden to avoid flashing the document onscreen
$args = array(MakePropertyValue("Hidden",true,$osm));
//Launch the desktop
$oDesktop = $osm->createInstance("");
//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
$export_args = array(MakePropertyValue("FilterName","writer_pdf_Export",$osm));
//Write out the PDF
$output_dir = "C:/wamp/www/vineesh/";
$doc_file = "C:/wamp/www/vineesh/vineesh.docx";
$pdf_file = "vineesh.pdf";
$output_file = $output_dir . $pdf_file;
$doc_file = "file:///" . $doc_file;
$output_file = "file:///" . $output_file;

how to convert .docx / .doc file to .pdf using php only

This is not an easy task and your rate of success will largely depend on the complexity of your word document. If only basic style elements are used (bold, italic, underline, color) you could use the HTML add on to FPDF. Any more complex elements would require a translation function to FPDF.

I assume you need this for template purposes, may I suggest directly programming your page layout in FPDF. You can create custom tags for layout which can be used in a WYSIWYG editor.

You will lose some of the flexibility compaired to a Word document, but that loss does not outweight the ease of setting up standard FPDF. But again, your best way to go will depend on the functionality you need and your end-goal. I suggest taking a look at the possibilities with FPDF. It's a great script and can make PDF's lightning fast!

Convert docx to pdf using PHP

Open the generated docx with PHPDOCX

    require_once 'phpdocx_pro/classes/';
$docx = new TransformDoc();
$html = $docx->getStrXHTML();

Also, you can export the docx to PDF with


Note this is not a free library

doc,docx file into pdf using shell_exec

simplest wy is to use "unoconv":

sudo apt-get install unoconv
doc2pdf respondus-docx-sample-file.docx

Related Topics

Leave a reply