package com.gx.pdf.utils.transfer;

import java.io.File;

import org.apache.log4j.Logger;

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.StreamOpenOfficeDocumentConverter;
import com.gx.obe.config.ConfigConstants;
import com.gx.obe.config.utils.PropertiesUtils;

public class TOPDFUtil {
	private static Logger LOG = Logger.getLogger(TOPDFUtil.class);

	/**
	 * @Description: 转换PDF
	 * @author mazc
	 * @param sourceFile 转换文件路径
	 * @param destFile   生成文件路径
	 * @return 
	 */
	public  boolean openOfficeToPDF(String sourceFile, String destFile) {
		return openOfficeToPDF(new File(sourceFile),new File(destFile));
	}
	
	/**
	 * @Description:  转换PDF
	 * @author mazc
	 * @param sourceFile 转换文件
	 * @param destFile 目标存储文件
	 * @return 
	 */
	public  boolean openOfficeToPDF(File inputFile, File outputFile) {
		OpenOfficeConnection connection = null;
		try {
			if (!inputFile.exists()) {
				return false;
			}
 			if (!outputFile.getParentFile().exists()) {
 				outputFile.getParentFile().mkdirs();
			}
//			connection = new SocketOpenOfficeConnection(PropertiesUtils.getProperty("39.97.30.133", ""), Integer.valueOf(PropertiesUtils.getProperty("8100", "8100")));
			connection = new SocketOpenOfficeConnection(PropertiesUtils.getProperty(ConfigConstants.OPENOFFICE_SERVER_URL, ""), Integer.valueOf(PropertiesUtils.getProperty(ConfigConstants.OPENOFFICE_SERVER_port, "8100")));
			connection.connect();
 			DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
			converter.convert(inputFile, outputFile);
			return true;
		} catch (Exception e) {
			LOG.error("调用openoffice结果异常", e);
 			return false;
		}finally {
			if(null != connection) {
				connection.disconnect();
			}
		}
	}
	
	public static void main(String[] args) {
		TOPDFUtil util = new TOPDFUtil();
		util.openOfficeToPDF("D:/test/最终得分汇总表 - 01.xlsx", "D:/test/最终得分汇总表 - 01.xlsx.pdf");
	}
}
