PHP calls OpenOffice to convert word to PDF. openofficepdf

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.