diff --git a/adc-da-activiti/src/main/java/com/adc/da/workFlow/service/ActSarItemsEOService.java b/adc-da-activiti/src/main/java/com/adc/da/workFlow/service/ActSarItemsEOService.java index 670dde45..57af35b3 100644 --- a/adc-da-activiti/src/main/java/com/adc/da/workFlow/service/ActSarItemsEOService.java +++ b/adc-da-activiti/src/main/java/com/adc/da/workFlow/service/ActSarItemsEOService.java @@ -121,7 +121,7 @@ public class ActSarItemsEOService { } /*** - * @Description: 标准解读流程入库" + * @Description: 国内外标准流程入库" * @Author: yangxuenan * @Date: 2020/12/7 13:48 * @Param: [standJson] diff --git a/adc-da-convert/pom.xml b/adc-da-convert/pom.xml new file mode 100644 index 00000000..43028d98 --- /dev/null +++ b/adc-da-convert/pom.xml @@ -0,0 +1,260 @@ + + + + foton-slrs-system-rest + com.adc + 3.0.0 + + 4.0.0 + + adc-da-convert + + UTF-8 + UTF-8 + 1.7 + 1.7 + 1.7 + 4.3.9.RELEASE + 3.4.0 + 1.4.0-RC2 + 4.1.2 + + + + + adc + http://60.247.58.121:8182/nexus/content/groups/public + + true + + + true + + + + + + + + thirdparty + Nexus Thirdparty Repository + http://60.247.58.121:8182/nexus/content/repositories/thirdparty/ + + + + + + + com.adc + adc-da-swagger + 2.0.0 + + + com.adc + adc-da-base + 2.0.0 + + + com.adc + adc-da-sys + 3.0.0 + + + com.adc + adc-da-util + 2.2.4 + + + org.bouncycastle + bcprov-jdk14 + + + + + org.springframework + spring-tx + ${spring.version} + + + org.springframework + spring-web + ${spring.version} + + + org.mybatis + mybatis + ${mybatis.version} + + + javax.validation + validation-api + 2.0.1.Final + + + + org.apache.shiro + shiro-core + ${shiro.version} + + + + com.adc + adc-da-excel + 2.0.0 + + + + org.apache.poi + poi + ${poi.version} + + + org.apache.poi + poi-ooxml + ${poi.version} + + + org.apache.poi + poi-ooxml-schemas + ${poi.version} + + + org.apache.poi + poi-scratchpad + ${poi.version} + true + + + + + + com.adc + adc-da-file + RELEASE + + + com.adc + adc-da-file + 2.0.0 + + + + + com.servlets + cos + 05Nov2002 + + + + com.artofsolving + jodconverter + system + 2.2.2 + ${basedir}/src/main/lib/jodconverter-2.2.2.jar + + + org.openoffice + juh + 3.0.1 + + + org.openoffice + jurt + 3.0.1 + + + org.openoffice + ridl + 3.0.1 + + + org.slf4j + slf4j-jdk14 + 1.7.21 + test + + + org.openoffice + unoil + 3.0.1 + + + com.thoughtworks.xstream + xstream + 1.3.1 + + + + + + com.itextpdf + kernel + 7.0.4 + + + com.itextpdf + io + 7.0.4 + + + com.itextpdf + layout + 7.0.4 + + + com.itextpdf + forms + 7.0.4 + + + com.itextpdf + pdfa + 7.0.4 + + + com.itextpdf + pdftest + 7.0.4 + + + org.slf4j + slf4j-log4j12 + 1.7.18 + + + + + org.springframework.boot + spring-boot-starter-amqp + + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + ${java.version} + ${project.build.sourceEncoding} + + + + + + \ No newline at end of file diff --git a/adc-da-convert/src/main/java/com/adc/da/convert/common/ConvertStateEnum.java b/adc-da-convert/src/main/java/com/adc/da/convert/common/ConvertStateEnum.java new file mode 100644 index 00000000..9f573722 --- /dev/null +++ b/adc-da-convert/src/main/java/com/adc/da/convert/common/ConvertStateEnum.java @@ -0,0 +1,19 @@ +package com.adc.da.convert.common; + +public enum ConvertStateEnum { + NO_CONVERT(0,"未转换"),CONVERING(1,"转换中"),CONVERT_SUCCESS(2,"转换成功"),CONVERT_ERROR(3,"转换失败"); + + private int value; + private String lable; + + private ConvertStateEnum(int value, String lable) { + this.value = value; + this.lable = lable; + } + public int getValue() { + return value; + } + public String getLable() { + return lable; + } +} diff --git a/adc-da-convert/src/main/java/com/adc/da/convert/common/DocConverter.java b/adc-da-convert/src/main/java/com/adc/da/convert/common/DocConverter.java new file mode 100644 index 00000000..30d1d95e --- /dev/null +++ b/adc-da-convert/src/main/java/com/adc/da/convert/common/DocConverter.java @@ -0,0 +1,265 @@ +package com.adc.da.convert.common; + +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.itextpdf.kernel.pdf.PdfReader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; + +import java.io.*; + +/** + * @Author yangxuenan + * @Description 文档转换 + * Date 2018/8/24 13:44 + * @Param + * @return + **/ +public class DocConverter { + + private static final Logger logger = LoggerFactory.getLogger(DocConverter.class); + // 环境 1:windows 2:linux + private static final int ENVIRONMENT = 1; + // (只涉及pdf2swf路径问题) + private String fileString; + // 输入路径 ,如果不设置就输出在默认的位置 + private String outputPath = ""; + //转化文件名称 + private String fileName; + //转换pdf文件 + private File pdfFile; + //转换成swf文件 + private File swfFile; + //待转换doc文件 + private File docFile; + //为文件加水印后文件路径 + String waterPdfPath; + + @Value("${convert.host}") + private String convertHost; + + public DocConverter(String fileString,String fileType) { + ini(fileString,fileType); + } + + /** + * @Author yangxuenan + * @Description 重新设置file + * Date 2018/8/24 13:50 + * @Param [fileString, fileType] + * @return void + **/ + public void setFile(String fileString,String fileType) { + ini(fileString,fileType); + } + + /** + * @Author yangxuenan + * @Description 初始化 + * Date 2018/8/24 13:50 + * @Param [fileString, fileType] + * @return void + **/ + private void ini(String fileString,String fileType) { + this.fileString = fileString; + fileName = fileString.substring(0, fileString.lastIndexOf(".")); + //判断文件类型,进行不同操作 + if(".doc".equals(fileType) || ".docx".equals(fileType)){ + docFile = new File(fileString); + pdfFile = new File(fileName + ".pdf"); + swfFile = new File(fileName + ".swf"); + } else if (".pdf".equals(fileType)){ + pdfFile = new File(fileName + ".pdf"); + swfFile = new File(fileName + ".swf"); + } + } + + /** + * @Author yangxuenan + * @Description 转为PDF + * Date 2018/8/24 13:50 + * @Param [] + * @return void + **/ + private void doc2pdf() throws Exception { + if (docFile.exists()) { + if (!pdfFile.exists()) { + logger.info("转换服务地址接口:"+convertHost); + //连接openOffice + OpenOfficeConnection connection = new SocketOpenOfficeConnection(convertHost,8100); + try { + connection.connect(); + //使用openOffice将doc文件转换问pdf文件 + DocumentConverter converter = new OpenOfficeDocumentConverter(connection); + converter.convert(docFile, pdfFile); + // 关闭连接 + connection.disconnect(); + logger.info("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****"); + } catch (java.net.ConnectException e) { + logger.error(e.getMessage(),e); + logger.info("****swf转换器异常,openoffice服务未启动!****"); + throw e; + } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { + logger.error(e.getMessage(),e); + logger.info("****swf转换器异常,读取转换文件失败****"); + throw e; + } catch (Exception e) { + logger.error(e.getMessage(),e); + throw e; + } + } else { + logger.info("****已经转换为pdf,不需要再进行转化****"); + } + } else { + logger.info("****swf转换器异常,需要转换的文档不存在,无法转换****"); + } + } + + /** + * @Author yangxuenan + * @Description 转换成 swf + * Date 2018/8/24 13:51 + * @Param [fileType] + * @return void + **/ + /*private void pdf2swf(String fileType) throws Exception { + Runtime r = Runtime.getRuntime(); + if (!swfFile.exists()) { + if (pdfFile.exists()) { + // windows环境处理 + if (ENVIRONMENT == 1) { + try { + //使用swfTools工具将pdf文件转换为swf文件 + //String paperSwfFile = swfFile.getPath().replace(".swf","%.swf"); + Process p = r.exec("G:/swftTools/pdf2swf.exe "+ waterPdfPath + " -o "+ swfFile.getPath() + " -f -T 9 -t -s languagedir=G:/xpdf/xpdf-chinese-simplified"); + logger.info("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); + //如果源文件为doc类文件,将过渡的pdf文件删除 + if(".doc".equals(fileType) || ".docx".equals(fileType)){ + if (pdfFile.exists()) { + if(!pdfFile.delete()){ + logger.error("文件删除失败!!!"); + } + } + } + } catch (IOException e) { + logger.error(e.getMessage(),e); + throw e; + } + } else if (ENVIRONMENT == 2) {// linux环境处理 + try { + Process p = r.exec("pdf2swf " + pdfFile.getPath() + + " -o " + swfFile.getPath() + " -f -T 9 -t -s storeallcharacters"); + logger.info("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); + if (pdfFile.exists()) { + if(!pdfFile.delete()){ + logger.error("文件删除失败!!!"); + } + } + } catch (Exception e) { + logger.error(e.getMessage(),e); + throw e; + } + } + } else { + logger.info("****pdf不存在,无法转换****"); + } + } else { + logger.info("****swf已经存在不需要转换****"); + } + }*/ + + static String loadStream(InputStream in) throws IOException { + + int ptr = 0; + in = new BufferedInputStream(in); + StringBuilder buffer = new StringBuilder(); + + while ((ptr = in.read()) != -1) { + buffer.append((char) ptr); + } + + return buffer.toString(); + } + + /** + * @Author yangxuenan + * @Description 转换主方法 + * Date 2018/8/24 13:51 + * @Param [fileType] + * @return boolean + **/ + public boolean conver(String fileType) { + + if (swfFile.exists()) { + logger.info("****swf转换器开始工作,该文件已经转换为swf****"); + return true; + } + + if (ENVIRONMENT == 1) { + logger.info("****swf转换器开始工作,当前设置运行环境windows****"); + } else { + logger.info("****swf转换器开始工作,当前设置运行环境linux****"); + } + try { + //判断待转换文件类型,如果为doc文件需要先转为pdf类型文件 + if(".doc".equals(fileType) || ".docx".equals(fileType)){ + doc2pdf(); +// pdf2swf(fileType); + } else if (".pdf".equals(fileType)){ +// pdf2swf(fileType); + } + + } catch (Exception e) { + logger.error(e.getMessage(),e); + return false; + } + + if (swfFile.exists()) { + return true; + } else { + return false; + } + } + + /** + * @Author yangxuenan + * @Description 返回文件路径 + * Date 2018/8/24 13:52 + * @Param [] + * @return java.lang.String + **/ + public String getswfPath() { + if (swfFile.exists()) { + //获取转换后的swf文件路径 + String tempString = swfFile.getPath(); + tempString = tempString.replaceAll("\\\\", "/"); + return tempString; + } else { + return ""; + } + + } + + /** + * @Author yangxuenan + * @Description 设置输出路径 + * Date 2018/8/24 13:52 + * @Param [outputPath] + * @return void + **/ + public void setOutputPath(String outputPath) { + this.outputPath = outputPath; + if (!outputPath.equals("")) { + String realName = fileName.substring(fileName.lastIndexOf("/"), + fileName.lastIndexOf(".")); + if (outputPath.charAt(outputPath.length()) == '/') { + swfFile = new File(outputPath + realName + ".swf"); + } + } + } + + +} diff --git a/adc-da-convert/src/main/java/com/adc/da/convert/common/DocConverterPdf.java b/adc-da-convert/src/main/java/com/adc/da/convert/common/DocConverterPdf.java new file mode 100644 index 00000000..6294d314 --- /dev/null +++ b/adc-da-convert/src/main/java/com/adc/da/convert/common/DocConverterPdf.java @@ -0,0 +1,243 @@ +package com.adc.da.convert.common; + +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.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter; +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.io.*; + +public class DocConverterPdf { + + private static final Logger logger = LoggerFactory.getLogger(DocConverterPdf.class); + + private static final int ENVIRONMENT = 1;// 环境 1:windows 2:linux + private String fileString;// (只涉及pdf2swf路径问题) + private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置 + private String fileName; + private File pdfFile; + private File convertPdfFile; + private File swfFile; + private File docFile; + String waterPdfPath; + + private String convertHost; + + public DocConverterPdf(String fileString, String fileType) { + ini(fileString,fileType); + } + + /** + * 重新设置file + * + * @param fileString + */ + public void setFile(String fileString,String fileType) { + ini(fileString,fileType); + } + + /** + * 初始化 + * + * @param fileString + */ + private void ini(String fileString,String fileType) { + this.fileString = fileString; + fileName = fileString.substring(0, fileString.lastIndexOf(".")); + if(".doc".equals(fileType.toLowerCase()) || ".docx".equals(fileType.toLowerCase()) || ".xlsx".equals(fileType.toLowerCase()) || ".xls".equals(fileType.toLowerCase()) || ".ppt".equals(fileType.toLowerCase()) || ".pptx".equals(fileType.toLowerCase())){ + docFile = new File(fileString); + convertPdfFile=new File(fileName+"_C"+ ".pdf"); + pdfFile = new File(fileName + ".pdf"); + swfFile = new File(fileName + ".swf"); + } else if (".pdf".equals(fileType)){ + pdfFile = new File(fileName + ".pdf"); + swfFile = new File(fileName + ".swf"); + } + } + + /** + * 转为PDF + * + * @param + */ + private void doc2pdf() throws Exception { + if (!pdfFile.exists()) { + OpenOfficeConnection connection = null; + logger.info("转换服务地址接口:"+convertHost); + try { + if(StringUtils.isNotBlank(convertHost)){ + connection = new SocketOpenOfficeConnection(convertHost,8100); + }else{ + connection = new SocketOpenOfficeConnection(8100); + } + connection.connect(); + DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection); + converter.convert(docFile, convertPdfFile); + + //addWaterToPdf(pdfFile,userId); + + // close the connection + connection.disconnect(); + // 开始设置PDF文档相关属性保证其安全性 + logger.info("开始设置转换后的PDF文件安全性相关设置"); + PDFEditUtils.setPDFFileSecurity(pdfFile.getAbsolutePath(),convertPdfFile.getAbsolutePath()); + logger.info("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****"); + } catch (java.net.ConnectException e) { + logger.error(e.getMessage(),e); + logger.info("****pdf转换器异常,openoffice服务未启动!****"); + throw e; + } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { + logger.error(e.getMessage(),e); + logger.info("****pdf转换器异常,读取转换文件失败****"); + throw e; + } catch (Exception e) { + logger.error(e.getMessage(),e); + throw e; + } + } else { + logger.info("****已经转换为pdf,不需要再进行转化****"); + } + } + + /** + * 转换成 swf + */ + @SuppressWarnings("unused") + /*private void pdf2swf(String fileType) throws Exception { + Runtime r = Runtime.getRuntime(); + if (!swfFile.exists()) { + if (pdfFile.exists()) { + if (ENVIRONMENT == 1) {// windows环境处理 + try { + //String paperSwfFile = swfFile.getPath().replace(".swf","%.swf"); + Process p = r.exec("G:/swftTools/pdf2swf.exe "+ waterPdfPath + " -o "+ swfFile.getPath() + " -f -T 9 -t -s languagedir=G:/xpdf/xpdf-chinese-simplified"); + logger.info("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); + if(".doc".equals(fileType) || ".docx".equals(fileType)){ + *//*if (pdfFile.exists()) { + pdfFile.delete(); + }*//* + } + } catch (IOException e) { + logger.error(e.getMessage(),e); + throw e; + } + } else if (ENVIRONMENT == 2) {// linux环境处理 + try { + Process p = r.exec("pdf2swf " + pdfFile.getPath() + + " -o " + swfFile.getPath() + " -f -T 9 -t -s storeallcharacters"); + logger.info("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); + if (pdfFile.exists()) { + if(!pdfFile.delete()){ + logger.error("文件删除失败!!!"); + } + } + } catch (Exception e) { + logger.error(e.getMessage(),e); + throw e; + } + } + } else { + logger.info("****pdf不存在,无法转换****"); + } + } else { + logger.info("****swf已经存在不需要转换****"); + } + }*/ + + static String loadStream(InputStream in) throws IOException { + + int ptr = 0; + in = new BufferedInputStream(in); + StringBuilder buffer = new StringBuilder(); + while ((ptr = in.read()) != -1) { + buffer.append((char) ptr); + } + + return buffer.toString(); + } + /** + * 转换主方法 + */ + @SuppressWarnings("unused") + public File conver(String fileType) { + if (ENVIRONMENT == 1) { + logger.info("****swf转换器开始工作,当前设置运行环境windows****"); + } else { + logger.info("****swf转换器开始工作,当前设置运行环境linux****"); + } + try { + if(StringUtils.isNotBlank(fileType)){ + fileType = fileType.toUpperCase(); + if(".DOC".equals(fileType) || ".DOCX".equals(fileType) || ".XLSX".equals(fileType) || ".XLS".equals(fileType) || ".PPT".equals(fileType) || ".PPTX".equals(fileType)){ + doc2pdf(); + /*pdf2swf(fileType);*/ + } else if (".PDF".equals(fileType)){ + /*pdf2swf(fileType);*/ + } + }else{ + logger.error("文件类型后缀获取失败,请检查文件名称格式是否正确"); + } + } catch (Exception e) { + logger.error(e.getMessage(),e); + return new File(""); + } + + return pdfFile; + + } + + /** + * 返回文件路径 + * + * @param + */ + public String getswfPath() { + if (swfFile.exists()) { + String tempString = swfFile.getPath(); + tempString = tempString.replaceAll("\\\\", "/"); + return tempString; + } else { + return ""; + } + + } + + public String getpdfPath() { + if (pdfFile!= null && !pdfFile.getPath().isEmpty()) { + String tempString = pdfFile.getPath(); + tempString = tempString.replaceAll("\\\\", "/"); + return tempString; + } else { + return ""; + } + + } + + /** + * 设置输出路径 + */ + public void setOutputPath(String outputPath) { + this.outputPath = outputPath; + if (!outputPath.equals("")) { + String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf(".")); + //TODO 此处的if判断执行的代码逻辑完全一样, 因此将其判断注释掉,直接执行 +/* if (outputPath.charAt(outputPath.length()) == '/') { + swfFile = new File(outputPath + realName + ".swf"); + } else { + swfFile = new File(outputPath + realName + ".swf"); + }*/ + swfFile = new File(outputPath + realName + ".swf"); + } + } + + public String getConvertHost() { + return convertHost; + } + + public void setConvertHost(String convertHost) { + this.convertHost = convertHost; + } +} diff --git a/adc-da-convert/src/main/java/com/adc/da/convert/common/DocConverterPdfOld.java b/adc-da-convert/src/main/java/com/adc/da/convert/common/DocConverterPdfOld.java new file mode 100644 index 00000000..d9887b21 --- /dev/null +++ b/adc-da-convert/src/main/java/com/adc/da/convert/common/DocConverterPdfOld.java @@ -0,0 +1,225 @@ +package com.adc.da.convert.common; + +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 org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; + +public class DocConverterPdfOld { + + private static final Logger logger = LoggerFactory.getLogger(DocConverterPdf.class); + + private static final int ENVIRONMENT = 1;// 环境 1:windows 2:linux + private String fileString;// (只涉及pdf2swf路径问题) + private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置 + private String fileName; + private File pdfFile; + private File convertPdfFile; + private File swfFile; + private File docFile; + String waterPdfPath; + + public DocConverterPdfOld(String fileString, String fileType) { + ini(fileString,fileType); + } + + /** + * 重新设置file + * + * @param fileString + */ + public void setFile(String fileString,String fileType) { + ini(fileString,fileType); + } + + /** + * 初始化 + * + * @param fileString + */ + private void ini(String fileString,String fileType) { + this.fileString = fileString; + fileName = fileString.substring(0, fileString.lastIndexOf(".")); + if(".doc".equals(fileType) || ".docx".equals(fileType) || ".xlsx".equals(fileType) || ".xls".equals(fileType) || ".ppt".equals(fileType) || ".pptx".equals(fileType)){ + docFile = new File(fileString); + convertPdfFile=new File(fileName+"_C"+ ".pdf"); + pdfFile = new File(fileName + ".pdf"); + swfFile = new File(fileName + ".swf"); + } else if (".pdf".equals(fileType)){ + pdfFile = new File(fileName + ".pdf"); + swfFile = new File(fileName + ".swf"); + } + } + + /** + * 转为PDF + * + * @param + */ + private void doc2pdf() throws Exception { + if (!pdfFile.exists()) { + OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); + try { + connection.connect(); + DocumentConverter converter = new OpenOfficeDocumentConverter(connection); + converter.convert(docFile, convertPdfFile); + + //addWaterToPdf(pdfFile,userId); + + // close the connection + connection.disconnect(); + // 开始设置PDF文档相关属性保证其安全性 + logger.info("开始设置转换后的PDF文件安全性相关设置"); + logger.info("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****"); + } catch (java.net.ConnectException e) { + logger.error(e.getMessage(),e); + logger.info("****pdf转换器异常,openoffice服务未启动!****"); + throw e; + } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { + logger.error(e.getMessage(),e); + logger.info("****pdf转换器异常,读取转换文件失败****"); + throw e; + } catch (Exception e) { + logger.error(e.getMessage(),e); + throw e; + } + } else { + logger.info("****已经转换为pdf,不需要再进行转化****"); + } + } + + /** + * 转换成 swf + */ + @SuppressWarnings("unused") + /*private void pdf2swf(String fileType) throws Exception { + Runtime r = Runtime.getRuntime(); + if (!swfFile.exists()) { + if (pdfFile.exists()) { + if (ENVIRONMENT == 1) {// windows环境处理 + try { + //String paperSwfFile = swfFile.getPath().replace(".swf","%.swf"); + Process p = r.exec("G:/swftTools/pdf2swf.exe "+ waterPdfPath + " -o "+ swfFile.getPath() + " -f -T 9 -t -s languagedir=G:/xpdf/xpdf-chinese-simplified"); + logger.info("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); + if(".doc".equals(fileType) || ".docx".equals(fileType)){ + *//*if (pdfFile.exists()) { + pdfFile.delete(); + }*//* + } + } catch (IOException e) { + logger.error(e.getMessage(),e); + throw e; + } + } else if (ENVIRONMENT == 2) {// linux环境处理 + try { + Process p = r.exec("pdf2swf " + pdfFile.getPath() + + " -o " + swfFile.getPath() + " -f -T 9 -t -s storeallcharacters"); + logger.info("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); + if (pdfFile.exists()) { + if(!pdfFile.delete()){ + logger.error("文件删除失败!!!"); + } + } + } catch (Exception e) { + logger.error(e.getMessage(),e); + throw e; + } + } + } else { + logger.info("****pdf不存在,无法转换****"); + } + } else { + logger.info("****swf已经存在不需要转换****"); + } + }*/ + + static String loadStream(InputStream in) throws IOException { + + int ptr = 0; + in = new BufferedInputStream(in); + StringBuilder buffer = new StringBuilder(); + while ((ptr = in.read()) != -1) { + buffer.append((char) ptr); + } + + return buffer.toString(); + } + /** + * 转换主方法 + */ + @SuppressWarnings("unused") + public File conver(String fileType) { + if (ENVIRONMENT == 1) { + logger.info("****swf转换器开始工作,当前设置运行环境windows****"); + } else { + logger.info("****swf转换器开始工作,当前设置运行环境linux****"); + } + try { + if(".doc".equals(fileType) || ".docx".equals(fileType) || ".xlsx".equals(fileType) || ".xls".equals(fileType) || ".ppt".equals(fileType) || ".pptx".equals(fileType)){ + doc2pdf(); + /*pdf2swf(fileType);*/ + } else if (".pdf".equals(fileType)){ + /*pdf2swf(fileType);*/ + } + + } catch (Exception e) { + logger.error(e.getMessage(),e); + return new File(""); + } + + return pdfFile; + + } + + /** + * 返回文件路径 + * + * @param + */ + public String getswfPath() { + if (swfFile.exists()) { + String tempString = swfFile.getPath(); + tempString = tempString.replaceAll("\\\\", "/"); + return tempString; + } else { + return ""; + } + + } + + public String getpdfPath() { + if (!pdfFile.getPath().isEmpty()) { + String tempString = pdfFile.getPath(); + tempString = tempString.replaceAll("\\\\", "/"); + return tempString; + } else { + return ""; + } + + } + + /** + * 设置输出路径 + */ + public void setOutputPath(String outputPath) { + this.outputPath = outputPath; + if (!outputPath.equals("")) { + String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf(".")); + //TODO 此处的if判断执行的代码逻辑完全一样, 因此将其判断注释掉,直接执行 +/* if (outputPath.charAt(outputPath.length()) == '/') { + swfFile = new File(outputPath + realName + ".swf"); + } else { + swfFile = new File(outputPath + realName + ".swf"); + }*/ + swfFile = new File(outputPath + realName + ".swf"); + } + } + +} diff --git a/adc-da-convert/src/main/java/com/adc/da/convert/common/PDFEditUtils.java b/adc-da-convert/src/main/java/com/adc/da/convert/common/PDFEditUtils.java new file mode 100644 index 00000000..515b6106 --- /dev/null +++ b/adc-da-convert/src/main/java/com/adc/da/convert/common/PDFEditUtils.java @@ -0,0 +1,34 @@ +package com.adc.da.convert.common; + + +import com.itextpdf.kernel.pdf.*; + +import java.io.File; +import java.io.IOException; + +/*** + * + * @MethodName:PDF编辑工具类 + * @author: zhangyanduan + * @param: + * @return: + * date: 2019/3/6 18:36 + */ +public class PDFEditUtils { + + //设置安全性 + public static void setPDFFileSecurity(String descFilePath,String srcFilePath) throws IOException { + checkOutFile(descFilePath); + PdfReader reader = new PdfReader(srcFilePath, new ReaderProperties()); + PdfWriter writer = new PdfWriter(descFilePath, new WriterProperties() + .setStandardEncryption(null, "1234qwer".getBytes(), EncryptionConstants.ALLOW_SCREENREADERS, + EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA)); + PdfDocument pdfDoc = new PdfDocument(reader, writer); + pdfDoc.close(); + } + + private static void checkOutFile(String descFilePath){ + File desc=new File(descFilePath); + desc.getParentFile().mkdirs(); + } +} diff --git a/adc-da-convert/src/main/java/com/adc/da/convert/common/SplitPdf.java b/adc-da-convert/src/main/java/com/adc/da/convert/common/SplitPdf.java new file mode 100644 index 00000000..7160d5c5 --- /dev/null +++ b/adc-da-convert/src/main/java/com/adc/da/convert/common/SplitPdf.java @@ -0,0 +1,169 @@ +package com.adc.da.convert.common; + +import com.adc.da.util.utils.UUID; +import com.itextpdf.text.*; +import com.itextpdf.text.log.Logger; +import com.itextpdf.text.log.LoggerFactory; +import com.itextpdf.text.pdf.*; +import org.apache.commons.lang.StringUtils; +import sun.misc.BASE64Encoder; + +import java.io.*; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +public class SplitPdf { + + private static final Logger logger = LoggerFactory.getLogger(SplitPdf.class); + + public static String splitPDFFile(String inputPath, + String outputPath, int fromPage, int toPage) { + Document document = new Document(); + InputStream inputStream = null; + OutputStream outputStream = null; + String allPassword = ""; + try { + logger.info("*********开始拆分pdf**********"); + inputStream = new FileInputStream(inputPath); + outputStream = new FileOutputStream(outputPath); + PdfReader inputPDF = new PdfReader(inputStream); + int totalPages = inputPDF.getNumberOfPages(); + //make fromPage equals to toPage if it is greater + if(fromPage > toPage ) { + fromPage = toPage; + } + if(toPage > totalPages) { + toPage = totalPages; + } + + // Create a writer for the outputstream + PdfWriter writer = PdfWriter.getInstance(document, outputStream); + Map pwdMap = setFilePassword(); + allPassword = pwdMap.get("allPwd"); + String filepassword = pwdMap.get("filePwd"); + writer.setEncryption(filepassword.getBytes(), "1234qwer".getBytes(), + PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); + document.open(); + //构造文件,防止初始化文件为空 + //document.add(new Chunk("")); + PdfContentByte cb = writer.getDirectContent(); // Holds the PDF data + PdfImportedPage page; + + while(fromPage <= toPage) { + Rectangle pageSize = inputPDF.getPageSize(fromPage); + document.setPageSize(new Rectangle(pageSize.getWidth(),pageSize.getHeight())); + document.newPage(); + page = writer.getImportedPage(inputPDF, fromPage); + // 此处将文档的旋转参数写入新的文档中 + /* PdfDictionary pageN = inputPDF.getPageN(fromPage); + PdfNumber asNumber = pageN.getAsNumber(PdfName.ROTATE); + PdfDictionary additional = new PdfDictionary(); + additional.put(PdfName.ROTATE,asNumber); + page.setAdditional(additional);*/ + cb.addTemplate(page, 0, 0); + fromPage++; + } + outputStream.flush(); + document.close(); + inputStream.close(); + outputStream.close(); + //加密码 + /*String path = outputPath.replace("测试文件拆分\\","测试文件拆分\\加密码\\"); + PDFEditUtils.setPDFFileSecurity(outputPath,outputPath);*/ + }catch (IllegalArgumentException ie){ + logger.error("拆分过程中,读取文件失败"); + logger.error(ie.getMessage(),ie); + logger.error(ie.getMessage()); + } catch (IOException ioe) { + logger.error("拆分过程中,读取文件IO失败"); + logger.error(ioe.getMessage(),ioe); + logger.error(ioe.getMessage()); + }catch (Exception e) { + logger.error(e.getMessage(),e); + logger.error(e.getMessage()); + } finally { + if(document.isOpen()){ + document.close(); + } + try { + if (inputStream != null){ + inputStream.close(); + } + if (outputStream != null){ + outputStream.close(); + } + } catch (IOException ioe) { + ioe.printStackTrace(); + } + } + return allPassword; + } + + /** + * 利用libreOffice将office文档转换成pdf + * @param inputFile 目标文件地址 + * @param pdfFile 输出文件夹 + * @return + */ + public static boolean convertOffice2PDF(String inputFile, String pdfFile){ + long start = System.currentTimeMillis(); + String command; + boolean flag; + String osName = System.getProperty("os.name"); + if (osName.contains("Windows")) { + command = "cmd /c soffice --headless --invisible --convert-to pdf:writer_pdf_Export " + inputFile + " --outdir " + pdfFile; + }else { + command = "libreoffice --headless --invisible --convert-to pdf:writer_pdf_Export " + inputFile + " --outdir " + pdfFile; + } + flag = executeLibreOfficeCommand(command); + long end = System.currentTimeMillis(); + logger.debug("用时:{} ms"+(end - start)); + return flag; + } + + + /** + * 执行command指令 + * @param command + * @return + */ + public static boolean executeLibreOfficeCommand(String command) { + logger.info("开始进行转化......."); + Process process;// Process可以控制该子进程的执行或获取该子进程的信息 + try { + logger.info("convertOffice2PDF cmd : {}"+ command); + process = Runtime.getRuntime().exec(command);// exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例。 + // 下面两个可以获取输入输出流 +// InputStream errorStream = process.getErrorStream(); +// InputStream inputStream = process.getInputStream(); + } catch (IOException e) { + logger.error(" convertOffice2PDF {} error"+command, e); + return false; + } + int exitStatus = 0; + try { + exitStatus = process.waitFor();// 等待子进程完成再往下执行,返回值是子线程执行完毕的返回值,返回0表示正常结束 + // 第二种接受返回值的方法 + int i = process.exitValue(); // 接收执行完毕的返回值 + logger.debug("返回值:"+i); + } catch (InterruptedException e) { + logger.info("InterruptedException convertOffice2PDF {}"+ command); + return false; + } + logger.info("convertOffice2PDF cmd exitStatus {}"+ exitStatus); + process.destroy(); // 销毁子进程 + logger.info("转化结束......."); + return true; + } + //设置文件密码 + public static Map setFilePassword(){ + String allPwd = UUID.randomUUID(20); + String filePwd = allPwd.substring(0,5) + allPwd.substring(15,20); + Map map = new HashMap<>(); + map.put("allPwd",allPwd); + map.put("filePwd",filePwd); + return map; + } + +} diff --git a/adc-da-convert/src/main/lib/jodconverter-2.2.2.jar b/adc-da-convert/src/main/lib/jodconverter-2.2.2.jar new file mode 100644 index 00000000..d033256e Binary files /dev/null and b/adc-da-convert/src/main/lib/jodconverter-2.2.2.jar differ diff --git a/pom.xml b/pom.xml index ca9ad0c4..4cec972f 100644 --- a/pom.xml +++ b/pom.xml @@ -39,7 +39,8 @@ adc-da-search adc-da-slrs adc-da-activiti - + adc-da-convert +