feat: 新增 convert 转换pdf子项目
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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<String,String> 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<String,String> setFilePassword(){
|
||||
String allPwd = UUID.randomUUID(20);
|
||||
String filePwd = allPwd.substring(0,5) + allPwd.substring(15,20);
|
||||
Map<String,String> map = new HashMap<>();
|
||||
map.put("allPwd",allPwd);
|
||||
map.put("filePwd",filePwd);
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user