Java implementation of online preview sample code of openOffice implementation

  • 2020-11-26 18:49:20
  • OfStack

Introduction to the

I've written about poi for online preview, and it's also mentioned that you can do it with openOffice, so I'll give you more details here.

My implementation logic has two types:

1. Use jodconverter(based on OpenOffice service) to convert files (.doc,.docx,.xls,.ppt) to html format.

2. Use jodconverter(based on OpenOffice services) to convert files (.doc,.docx,.xls,.ppt) to pdf format.

Convert to html format, which everyone can understand, so that you can view directly in the browser, which enables the online preview function; To convert to pdf, you will need to install Adobe Reader XI, and you will find that if you drag pdf directly to the browser page, you will be able to open the preview directly, thus enabling you to preview online.

Convert the file to html or pdf format

Without further ado, just code it.


package com.pdfPreview.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.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
/**
 *  using jodconverter( Based on the OpenOffice service ) The file (*.doc , *.docx , *.xls , *.ppt) into html Format or pdf Format, 
 *  Please check before use OpenOffice Whether the service is enabled , OpenOffice Process name: soffice.exe | soffice.bin
 * 
 * @author yjclsx
 */
public class Doc2HtmlUtil {

  private static Doc2HtmlUtil doc2HtmlUtil;

  /**
   *  To obtain Doc2HtmlUtil The instance 
   */
  public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
    if (doc2HtmlUtil == null) {
      doc2HtmlUtil = new Doc2HtmlUtil();
    }
    return doc2HtmlUtil;
  }

  /**
   *  Convert file to html
   * 
   * @param fromFileInputStream:
   * @throws IOException 
   */
  public String file2Html(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String timesuffix = sdf.format(date);
    String docFileName = null;
    String htmFileName = null;
    if("doc".equals(type)){
      docFileName = "doc_" + timesuffix + ".doc";
      htmFileName = "doc_" + timesuffix + ".html";
    }else if("docx".equals(type)){
      docFileName = "docx_" + timesuffix + ".docx";
      htmFileName = "docx_" + timesuffix + ".html";
    }else if("xls".equals(type)){
      docFileName = "xls_" + timesuffix + ".xls";
      htmFileName = "xls_" + timesuffix + ".html";
    }else if("ppt".equals(type)){
      docFileName = "ppt_" + timesuffix + ".ppt";
      htmFileName = "ppt_" + timesuffix + ".html";
    }else{
      return null;
    }

    File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
    File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
    if (htmlOutputFile.exists())
      htmlOutputFile.delete();
    htmlOutputFile.createNewFile();
    if (docInputFile.exists())
      docInputFile.delete();
    docInputFile.createNewFile();
    /**
     *  by fromFileInputStream Build input file 
     */
    try {
      OutputStream os = new FileOutputStream(docInputFile);
      int bytesRead = 0;
      byte[] buffer = new byte[1024 * 8];
      while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
        os.write(buffer, 0, bytesRead);
      }

      os.close();
      fromFileInputStream.close();
    } catch (IOException e) {
    }

    OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
    try {
      connection.connect();
    } catch (ConnectException e) {
      System.err.println(" File conversion error, please check OpenOffice Whether the service is started. ");
    }
    // convert
    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
    converter.convert(docInputFile, htmlOutputFile);
    connection.disconnect();
    //  Delete after conversion word file 
    docInputFile.delete();
    return htmFileName;
  }

  /**
   *  Convert file to pdf
   * 
   * @param fromFileInputStream:
   * @throws IOException 
   */
  public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    String timesuffix = sdf.format(date);
    String docFileName = null;
    String htmFileName = null;
    if("doc".equals(type)){
      docFileName = "doc_" + timesuffix + ".doc";
      htmFileName = "doc_" + timesuffix + ".pdf";
    }else if("docx".equals(type)){
      docFileName = "docx_" + timesuffix + ".docx";
      htmFileName = "docx_" + timesuffix + ".pdf";
    }else if("xls".equals(type)){
      docFileName = "xls_" + timesuffix + ".xls";
      htmFileName = "xls_" + timesuffix + ".pdf";
    }else if("ppt".equals(type)){
      docFileName = "ppt_" + timesuffix + ".ppt";
      htmFileName = "ppt_" + timesuffix + ".pdf";
    }else{
      return null;
    }

    File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
    File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
    if (htmlOutputFile.exists())
      htmlOutputFile.delete();
    htmlOutputFile.createNewFile();
    if (docInputFile.exists())
      docInputFile.delete();
    docInputFile.createNewFile();
    /**
     *  by fromFileInputStream Build input file 
     */
    try {
      OutputStream os = new FileOutputStream(docInputFile);
      int bytesRead = 0;
      byte[] buffer = new byte[1024 * 8];
      while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
        os.write(buffer, 0, bytesRead);
      }

      os.close();
      fromFileInputStream.close();
    } catch (IOException e) {
    }

    OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
    try {
      connection.connect();
    } catch (ConnectException e) {
      System.err.println(" File conversion error, please check OpenOffice Whether the service is started. ");
    }
    // convert
    DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
    converter.convert(docInputFile, htmlOutputFile);
    connection.disconnect();
    //  Delete after conversion word file 
    docInputFile.delete();
    return htmFileName;
  }

  public static void main(String[] args) throws IOException {
    Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance();
    File file = null;
    FileInputStream fileInputStream = null;

    file = new File("D:/poi-test/exportExcel.xls");
    fileInputStream = new FileInputStream(file);
//   coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/xls","xls");
    coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/xls","xls");

    file = new File("D:/poi-test/test.doc");
    fileInputStream = new FileInputStream(file);
//   coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/doc","doc");
    coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/doc","doc");

    file = new File("D:/poi-test/ Weekly report template .ppt");
    fileInputStream = new FileInputStream(file);
//   coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");
    coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");

    file = new File("D:/poi-test/test.docx");
    fileInputStream = new FileInputStream(file);
//   coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/docx","docx");
    coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/docx","docx");

  }

}

Conversion to html and conversion to pdf are almost identical, except that when the output of File is created, the former named XXX.html and the latter named XXX.pdf are executed in ES53en.convert (docInputFile, htmlOutputFile); , jodconverter converts itself to the corresponding file based on the file type name.

Note that in the main method, both file2Html and file2pdf are called, and an error will be reported, either to html or to pdf, only one can be selected. In addition, before execution, you need to start openOffice's service: execute ES70en-ES71en-ES72en = "socket,host=127.0.0.1,port=8100 in the command window under openOffice directory; urp;" -ES77en can be started.


Related articles: