Much time has passed since the writing of the previous article about generating Excel documents from a template, and the task has changed somewhat. A new task was set as follows: from a finished excel or word document to make a template via a web interface. In the process of formation, substitute the desired values ​​into the template, remove and / or “clone” pieces of the template. After formation, the document should be available to the user for visual editing in the browser. The finished document should be saved on the server, be available for download by the user in its extension (* .doc / *. Xls), and in pdf. At the same time, the layout of the downloaded file should be identical to the template that was loaded at the very beginning (without any distortion of fields and print areas).package ru.cpro.uchteno.util; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.logging.Level; import java.util.logging.Logger; import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeConnectionProtocol; import org.artofsolving.jodconverter.office.OfficeManager; public class Libre {// public static void doc2html(InputStream is, OutputStream os) {// doc html try { File inf = File.createTempFile("doc", ".doc"); // FileOutputStream infos = new FileOutputStream(inf); // // int n = 0; byte buff[] = new byte[1024]; while (n >= 0) { n = is.read(buff); if (n > 0) { infos.write(buff, 0, n); } } // is.close(); infos.close(); // File onf = File.createTempFile("doc", ".html"); // jodconverter' ExternalOfficeManagerConfiguration officeConfiguration = new ExternalOfficeManagerConfiguration(); // tcp officeConfiguration .setConnectionProtocol(OfficeConnectionProtocol.SOCKET); // officeConfiguration.setPortNumber(2002); // officeManager OfficeManager officeManager = officeConfiguration .buildOfficeManager(); // officeManager.start(); // OfficeDocumentConverter converter = new OfficeDocumentConverter( officeManager); // converter.convert(inf, onf); // officeManager.stop(); // FileInputStream outfis = new FileInputStream(onf); n = 0; while (n >= 0) { n = outfis.read(buff); if (n > 0) { os.write(buff, 0, n); } } // outfis.close(); os.close(); // inf.delete(); onf.delete(); } catch (IOException ex) { Logger.getLogger(Libre.class.getName()).log(Level.SEVERE, null, ex); } } public static void doc2pdf(InputStream is, OutputStream os) { try { File inf = File.createTempFile("doc", ".doc"); FileOutputStream infos = new FileOutputStream(inf); int n = 0; byte buff[] = new byte[1024]; while (n >= 0) { n = is.read(buff); if (n > 0) { infos.write(buff, 0, n); } } is.close(); infos.close(); File onf = File.createTempFile("doc", ".pdf"); ExternalOfficeManagerConfiguration officeConfiguration = new ExternalOfficeManagerConfiguration(); officeConfiguration .setConnectionProtocol(OfficeConnectionProtocol.SOCKET); officeConfiguration.setPortNumber(2002); OfficeManager officeManager = officeConfiguration .buildOfficeManager(); officeManager.start(); OfficeDocumentConverter converter = new OfficeDocumentConverter( officeManager); converter.convert(inf, onf); officeManager.stop(); FileInputStream outfis = new FileInputStream(onf); n = 0; while (n >= 0) { n = outfis.read(buff); if (n > 0) { os.write(buff, 0, n); } } outfis.close(); os.close(); inf.delete(); onf.delete(); } catch (IOException ex) { Logger.getLogger(Libre.class.getName()).log(Level.SEVERE, null, ex); } } public static void html2doc(InputStream is, OutputStream os) { try { File inf = File.createTempFile("doc", ".html"); FileOutputStream infos = new FileOutputStream(inf); int n = 0; byte buff[] = new byte[1024]; while (n >= 0) { n = is.read(buff); if (n > 0) { infos.write(buff, 0, n); } } is.close(); infos.close(); File onf = File.createTempFile("doc", ".doc"); ExternalOfficeManagerConfiguration officeConfiguration = new ExternalOfficeManagerConfiguration(); officeConfiguration .setConnectionProtocol(OfficeConnectionProtocol.SOCKET); officeConfiguration.setPortNumber(2002); OfficeManager officeManager = officeConfiguration .buildOfficeManager(); officeManager.start(); OfficeDocumentConverter converter = new OfficeDocumentConverter( officeManager); converter.convert(inf, onf); officeManager.stop(); FileInputStream outfis = new FileInputStream(onf); n = 0; while (n >= 0) { n = outfis.read(buff); if (n > 0) { os.write(buff, 0, n); } } outfis.close(); os.close(); inf.delete(); onf.delete(); } catch (IOException ex) { Logger.getLogger(Libre.class.getName()).log(Level.SEVERE, null, ex); } } public static void html2docx(InputStream is, OutputStream os) { try { File inf = File.createTempFile("doc", ".html"); FileOutputStream infos = new FileOutputStream(inf); int n = 0; byte buff[] = new byte[1024]; while (n >= 0) { n = is.read(buff); if (n > 0) { infos.write(buff, 0, n); } } is.close(); infos.close(); File onf = File.createTempFile("doc", ".docx"); ExternalOfficeManagerConfiguration officeConfiguration = new ExternalOfficeManagerConfiguration(); officeConfiguration .setConnectionProtocol(OfficeConnectionProtocol.SOCKET); officeConfiguration.setPortNumber(2002); OfficeManager officeManager = officeConfiguration .buildOfficeManager(); officeManager.start(); OfficeDocumentConverter converter = new OfficeDocumentConverter( officeManager); converter.convert(inf, onf); officeManager.stop(); FileInputStream outfis = new FileInputStream(onf); n = 0; while (n >= 0) { n = outfis.read(buff); if (n > 0) { os.write(buff, 0, n); } } outfis.close(); os.close(); inf.delete(); onf.delete(); } catch (IOException ex) { Logger.getLogger(Libre.class.getName()).log(Level.SEVERE, null, ex); } } public static void html2pdf(InputStream is, OutputStream os) { try { File inf = File.createTempFile("doc", ".html"); FileOutputStream infos = new FileOutputStream(inf); int n = 0; byte buff[] = new byte[1024]; while (n >= 0) { n = is.read(buff); if (n > 0) { infos.write(buff, 0, n); } } is.close(); infos.close(); File onf = File.createTempFile("doc", ".pdf"); ExternalOfficeManagerConfiguration officeConfiguration = new ExternalOfficeManagerConfiguration(); officeConfiguration .setConnectionProtocol(OfficeConnectionProtocol.SOCKET); officeConfiguration.setPortNumber(2002); OfficeManager officeManager = officeConfiguration .buildOfficeManager(); officeManager.start(); OfficeDocumentConverter converter = new OfficeDocumentConverter( officeManager); converter.convert(inf, onf); officeManager.stop(); FileInputStream outfis = new FileInputStream(onf); n = 0; while (n >= 0) { n = outfis.read(buff); if (n > 0) { os.write(buff, 0, n); } } outfis.close(); os.close(); inf.delete(); onf.delete(); } catch (IOException ex) { Logger.getLogger(Libre.class.getName()).log(Level.SEVERE, null, ex); } } } tinymce.init({ selector: "textarea", theme: "modern", fullpage_default_doctype: "<!DOCTYPE xhtml>", plugins: [ "advlist autolink lists link image charmap print preview hr anchor pagebreak", "searchreplace wordcount visualblocks visualchars code fullscreen", "insertdatetime media nonbreaking save table contextmenu directionality", "emoticons template paste textcolor fullpage" ], toolbar1: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image", toolbar2: "print preview media | forecolor backcolor emoticons", image_advtab: true }); tinyMCE.triggerSave();Libre.java library:html2doc()html2docx()html2pdf()Source: https://habr.com/ru/post/224795/
All Articles