[Original] Java realizes word to pdf

  Recently I encountered a project that needs to convert word into pdf. Baidu has a lot of solutions on the Internet, such as virtual printing, installing extension plug-ins for word, etc. These solutions all rely on the ms word program, and in the java code, such as jacob must also be used Or a java com bridge like jcom, which makes the server development limited to the win platform, and it is also very troublesome to deploy. Later, I saw a conversion scheme of openoffice+jodconverter in a forum, which can complete the conversion of word to PDF. The server development side needs to install openoffice, but it requires an additional operation-a port on the server development needs to provide an openoffice service, which is a bit cumbersome to deploy and doesn't seem to be very secure.
  By chance, I found that the PageOffice component can also realize the function of word to pdf. And not only simply convert word to pdf format, but also support dynamic filling of data into word template files and then convert them to pdf files.
  The following is the introduction of the official website:

  

  The core code for calling the FileMaker object of the PageOffice component to dynamically fill data into the word template and convert it to pdf is as follows:

    FileMakerCtrl fmCtrl = new FileMakerCtrl(request);
    fmCtrl.setServerPage(request.getContextPath()+"/poserver.zz");
    WordDocument doc = new WordDocument(); 
    doc.openDataRegion("PO_company").setValue("Beijing XX Co., Ltd." ); //Assign the data area, that is, fill the data into the corresponding position in the template 
    fmCtrl.setSaveFilePage( " /savepdf"); // Save pdf action or RequestMapping method 
    fmCtrl.setWriter(doc);
    fmCtrl.fillDocumentAsPDF( "doc/template.doc", DocumentOpenType.Word, "123.pdf"); // fill word template and convert to pdf
  The code for the action or RequestMapping method (/savepdf) to save the pdf:
    FileSaver fs = new FileSaver(request, response);
    fs.saveToFile(request.getSession().getServletContext().getRealPath("doc") +"/"+ fs.getFileName());
    fs.close();

  Sample source code and effect: http://www.zhuozhengsoft.com/dowm/, download PageOffice for Java, after decompression, copy the Samples4 folder to the Webapps directory of Tomcat, visit: http://localhost:8080/Samples4/index .html, see the example: 2. 34. FileMaker converts a single document to PDF (take Word as an example) 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325946580&siteId=291194637