PDF online solution (2) -- convert files to PDF

Source: Internet
Author: User
Related Topics:

PDF online solution (1) -- File Upload

PDF online solution (2) -- convert files to PDF

As mentioned in the previous article, there are a large number of examples of file upload on the network, because a complete solution is provided for reference only; this article describes how to convert common files to PDF files.

1. convert Word and Excel to PDF

Baidu has learned that there are three main formats for Converting Office to PDF: Jacob, jcom, and OpenOffice, the first two methods depend on the Windows platform and the office software (jcom also needs the dedicated bat_pro and has been registered), which is costly and limited. The third method supports cross-platform use using open-source OpenOffice, currently, most web applications are deployed on Linux or UNIX platforms. Therefore, the third method is the best implementation solution. The following describes the implementation of the third solution (based on the Windows platform, the installation and startup of OpenOffice on Linux platform are slightly different, but the code called by Java is the same. Later, we will provide a special section to introduce the installation and startup of OpenOffice on Linux platform ), the first two methods will provide the demo below for your reference.

Install OpenOffice software and start it as a service before conversion:

OpenOffice: http://www.openoffice.org/download/index.html

After the installation is successful, locate the program directory in cmd and execute the command: soffice-Headless-accept = "socket, host =, Port = 8100; URP;"-nofirststartwizard, and then execute: netstat-ano | findstr "8100". If a red line is displayed, the startup is successful.


Call OpenOffice code:

To call OpenOffice for conversion, you need to use the jar package provided by OpenOffice: jodconverter, juh, jurt, ridl, slf4j-api, slf4j-jdk14, unoil, xstream, Apache Io package, you can reference it in the project, where the com. artofsolving. there is a document-formats.xml file under the jodconverter path, which defines the file conversion types supported by OpenOffice, Here we only select the doc, docx, xls, XLSX four types, obtain the OpenOffice connection, define the format of the input and output files, execute the conversion, and close the connection. The detailed comments in the Code are not described here.

/*** Call OpenOffice to convert office to PDF * @ Param instream input stream * @ Param Fos output stream * @ Param extensionname extension */public static void converteroffiec2pdf (inputstream instream, fileoutputstream FOS, string extensionname) {openofficeconnection connection = NULL; try {connection = new socketopenofficeconnection (8100); // obtain the OpenOffice connection documentconverter converter = new openofficedocumentconverter (connection); // create op Enoffice file conversion class: documentformat inputformat = NULL; documentformat PDF = new documentformat ("Portable Document Format", "application/pdf", "pdf "); // specify the target file format information // specify the output filter parameter PDF. setexportfilter (documentfamily. drawing, "draw_1__export"); PDF. setexportfilter (documentfamily. presentation, "impress_pdf_export"); PDF. setexportfilter (documentfamily. spreadsheet, "calc_1__export"); PDF. setexportfilter (document Family. text, "writer_pdf_export"); // specify the definition information of the input file format in docment_formats.xml if (constans. fileextname. doc. repeated signorecase (extensionname) {inputformat = new documentformat ("Microsoft Word", documentfamily. text, "application/MSWord", "Doc"); inputformat. setexportfilter (documentfamily. text, "MS Word 97");} else if (constans. fileextname. docx. using signorecase (extensionname) {inputformat = new using E Ntformat ("Microsoft Word 2007 XML", documentfamily. text, "application/vnd.openxmlformats-officedocument.wordprocessingml.doc ument", "docx");} else if (constans. fileextname. XLS. repeated signorecase (extensionname) {inputformat = new documentformat ("Microsoft Excel", documentfamily. spreadsheet, "application/vnd. MS-excel "," xls "); inputformat. setexportfilter (documentfamily. spreadsheet, "MS Excel 97 ");} Else if (constans. fileextname. XLSX. repeated signorecase (extensionname) {inputformat = new documentformat ("Microsoft Excel 2007 XML", documentfamily. spreadsheet, "application/vnd. openxmlformats-officedocument.spreadsheetml.sheet "," XLSX ");} // execute file conversion converter. convert (instream, inputformat, FOS, PDF);} finally {If (connection! = NULL) {connection. Disconnect (); connection = NULL;} closestream (instream, FOS );}}



2. Converting images to PDF

The open-source jar package itextpdf of itext is used for converting images to PDF. The itextpdf can be used to create and modify PDF files. Here we use itextpdf to create a new pdf file and then add the image to achieve the effect of converting images to PDF files; the Code step is: first create a document, get the PDF writer, open the document, set the content format, write the content to the document, and close the document.

/*** Convert image to PDF * @ Param instream * @ Param Fos * @ throws malformedurlexception * @ throws ioexception * @ throws */public static void converterimg2pdf (inputstream instream, fileoutputstream FOS) throws malformedurlexception, ioexception {// create a new document DOC = new document (); try {// PDF writer to load the document and output stream into writer. getinstance (Doc, FOS); Doc. open (); // open the document to prepare for writing. The document must be opened before it can be written into bufferedimage = ImageIO. read (instream); float H = bufferedimage. getheight (); float W = bufferedimage. getwidth (); image = image. getinstance (bufferedimage, null); image. setalignment (image. middle); // align the image. scalepercent (getpercent2 (H, W); // image compression ratio Doc. add (image); // Add the image Doc. close (); // close the document. The document must be properly closed} catch (incluentexception e) {e. printstacktrace ();}}


Download Related Files

Jcom, Jacob Demo: http://pan.baidu.com/s/1dD297rz

PDF online solution (2) -- convert files to PDF

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.