PHP calls OpenOffice to convert word to PDF. openofficepdf
Recently, I have been studying PHP Word documents to convert to PDF files, and I have searched many similar documents on the Internet, most of which are converted through OpenOffice.
The core code is as follows:
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) {$ osm = new COM ("com. sun. star. serviceManager ") or die (" Please be sure that OpenOffice.org is installed. n "); $ args = array (MakePropertyValue (" Hidden ", true, $ osm); $ oDesktop = $ osm-> createInstance (" com. sun. star. frame. desktop "); $ oWriterDoc = $ oDesktop-> loadComponentFromURL ($ doc_url," _ blank ", 0, $ args); $ export_args = array (MakePropertyValue (" FilterName ", "writer_pdf_Export", $ osm); $ oWriterDoc-> storeToURL ($ output_url, $ export_args); $ oWriterDoc-> close (true );} $ doc_file = dirname (_ FILE __). "/11.doc"; // The source FILE, DOC or WPS can all be $ output_file = dirname (_ FILE __). "/11.20."; // name of the file to be converted to PDF $ doc_file =" file :///". $ doc_file; $ output_file = "file :///". $ output_file; $ document-> word2pdf ($ doc_file, $ output_file );
I found that the Code has always reported an error.
(! ) Fatal error: Uncaught exception 'com _ exception' with message' <B> Source: </B> [automation bridge] <br/> <B> Description: </B> com. sun. star. task. errorCodeIOException: 'In I: \ phpStudy \ WWW \ DocPreview \ test2.php on line 27
(! ) Com_exception: <B> Source: </B> [automation bridge] <br/> <B> Description: </B> com. sun. star. task. errorCodeIOException: in I: \ phpStudy \ WWW \ DocPreview \ test2.php on line 27
Finally, the original transfer path problem was found: the transfer path of the above Code was obtained through debugging. $ output_file is file: // I: \ phpStudy \ WWW \ DocPreview \ sdds.pdf.
However, the required path in the storeToURL method is file: // I:/phpStudy/WWW/DocPreview/sdds.pdf.
Therefore, you only need to replace "\" of $ output_file with "/".
$ Doc_file = dirname (_ FILE __). "/11.doc"; // The source FILE, DOC or WPS can all be $ output_file = dirname (_ FILE __). "/11.20."; // name of the file to be converted to PDF $ output_file = str_replace (" \ ","/", $ output_file); $ doc_file =" file :///". $ doc_file; $ output_file = "file :///". $ output_file; $ document-> word2pdf ($ doc_file, $ output_file );
The above PHP call OpenOffice to implement word-to-PDF conversion is all the content shared by the editor. I hope to give you a reference, and I hope you can provide more support to the customer's house.