package com.sunxung.factoring.component.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;
import com.sunxung.factoring.component.exception.BusinessException;

/**
 * doc,docx,xls,xlsx 转换成pdf工具类 注意： 安装openOffice最新版，使用jodconverter-2.2.2.jar的jar包
 * 
 * @author Administrator
 */
public class Doc2HtmlUtil {

    private static Doc2HtmlUtil doc2HtmlUtil;

    /**
     * 获取Doc2HtmlUtil实例
     */
    public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
        if (doc2HtmlUtil == null) {
            doc2HtmlUtil = new Doc2HtmlUtil();
        }
        return doc2HtmlUtil;
    }

    /**
     * 转换文件成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();
        /**
         * 由fromFileInputStream构建输入文件
         */
        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("文件转换出错，请检查OpenOffice服务是否启动。");
        }
        // convert
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(docInputFile, htmlOutputFile);
        connection.disconnect();
        // 转换完之后删除word文件
        docInputFile.delete();
        return htmFileName;
    }

    /**
     * 转换文件成pdf
     * 
     * @param fromFileInputStream:
     * @throws IOException
     */
    public String file2pdf(InputStream fromFileInputStream, String toFilePath, String type) throws IOException {
        String docFileName = null;
        String htmFileName = null;
        if ("doc".equals(type)) {
            docFileName = FileUtil.randomFileName() + ".doc";
            htmFileName = FileUtil.randomFileName() + ".pdf";
        } else if ("docx".equals(type)) {
            docFileName = FileUtil.randomFileName() + ".docx";
            htmFileName = FileUtil.randomFileName() + ".pdf";
        } else if ("xls".equals(type)) {
            docFileName = FileUtil.randomFileName() + ".xls";
            htmFileName = FileUtil.randomFileName() + ".pdf";
        } else if ("xlsx".equals(type)) {
            docFileName = FileUtil.randomFileName() + ".xlsx";
            htmFileName = FileUtil.randomFileName() + ".pdf";
        } else if ("ppt".equals(type)) {
            docFileName = FileUtil.randomFileName() + ".ppt";
            htmFileName = FileUtil.randomFileName() + ".pdf";
        } else if ("pptx".equals(type)) {
            docFileName = FileUtil.randomFileName() + ".pptx";
            htmFileName = FileUtil.randomFileName() + ".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();
        /**
         * 由fromFileInputStream构建输入文件
         */
        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("文件转换出错，请检查OpenOffice服务是否启动。");
        }
        // convert
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(docInputFile, htmlOutputFile);
        connection.disconnect();
        // 转换完之后删除word文件
        docInputFile.delete();
        return htmFileName;
    }

    /**
     * 转换成html文件
     */
    public File fileToHtml(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 (".xlsx".equals(type)) {
            docFileName = "xlsx_" + timesuffix + ".xlsx";
            htmFileName = "xlsx_" + timesuffix + ".html";
        } else if (".ppt".equals(type)) {
            docFileName = "ppt_" + timesuffix + ".ppt";
            htmFileName = "ppt_" + timesuffix + ".html";
        } else if (".txt".equals(type)) {
            docFileName = "txt_" + timesuffix + ".txt";
            htmFileName = "txt_" + timesuffix + ".html";
        } else {
            return null;
        }
        File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
        File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
        /**
         * 由fromFileInputStream构建输入文件
         */
        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("文件转换出错，请检查OpenOffice服务是否启动。");
            throw new BusinessException(CodeUtil.FAIL, "文件转换出错，请检查OpenOffice服务是否启动。");
        }
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(docInputFile, htmlOutputFile);
        connection.disconnect();
        if (docInputFile.exists()) {
            docInputFile.delete();
        }
        return htmlOutputFile;
    }

    /**
     * 转换成html文件
     */
    public File fileToHtmlNew(File docFile, String toFilePath, String type) throws IOException {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timesuffix = sdf.format(date);
        String htmFileName = null;
        if (".doc".equals(type)) {
            htmFileName = "doc_" + timesuffix + ".html";
        } else if (".docx".equals(type)) {
            htmFileName = "docx_" + timesuffix + ".html";
        } else if (".xls".equals(type)) {
            htmFileName = "xls_" + timesuffix + ".html";
        } else if (".xlsx".equals(type)) {
            htmFileName = "xlsx_" + timesuffix + ".html";
        } else if (".ppt".equals(type)) {
            htmFileName = "ppt_" + timesuffix + ".html";
        }else {
            return null;
        }
        File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
        try {
            connection.connect();
        } catch (ConnectException e) {
            System.err.println("文件转换出错，请检查OpenOffice服务是否启动。");
            throw new BusinessException(CodeUtil.FAIL, "文件转换出错，请检查OpenOffice服务是否启动。");
        }
        DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
        converter.convert(docFile, htmlOutputFile);
        connection.disconnect();
        if (docFile.exists()) {
           // docFile.delete();
        }
        return htmlOutputFile;
    }

    public static void main(String[] args) throws IOException {
        Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance();
        File file = null;
        FileInputStream fileInputStream = null;
        file = new File("D:/poi-test/test2.doc");
        fileInputStream = new FileInputStream(file); //
        coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/doc", ".doc");
    }
}
