Merge branch 'dev-zzs' into test
This commit is contained in:
commit
4c8ff75bf1
@ -122,8 +122,12 @@ public class SalesOrderAutoSignOp extends AbstractOperationServicePlugIn {
|
||||
|
||||
// 书签位置替换为图片(书签不会删除)
|
||||
file = ContactSignUtils.replaceBookmarkWithImage(in, stream, filename, BOOKMARK);
|
||||
// File pdf = ContactSignUtils.convertToPDF(file);
|
||||
AttachmentUtil.uploadAttachment(appId, formId, String.valueOf(pkId), UPLOAD_ATTACHMENT_KEY, file.getName(), file.getPath());
|
||||
List<Map<String, Object>> uploaded = AttachmentServiceHelper.getAttachments(formId, pkId, UPLOAD_ATTACHMENT_KEY);
|
||||
File pdf = ContactSignUtils.convertToPDF(uploaded);
|
||||
if (pdf != null) {
|
||||
AttachmentUtil.uploadAttachment(appId, formId, String.valueOf(pkId), UPLOAD_ATTACHMENT_KEY, pdf.getName(), pdf.getPath());
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
|
@ -1,19 +1,22 @@
|
||||
package com.yem.wm.utils;
|
||||
|
||||
import com.yem.wm.es.encasement.util.FileUtil;
|
||||
import kd.bos.context.RequestContext;
|
||||
import kd.bos.exception.KDBizException;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
import org.apache.pdfbox.pdmodel.PDPage;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
||||
import org.apache.poi.util.Units;
|
||||
import org.apache.poi.xwpf.usermodel.*;
|
||||
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @Description: 合同盖章
|
||||
@ -23,6 +26,8 @@ import java.util.List;
|
||||
|
||||
public class ContactSignUtils {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ContactSignUtils.class);
|
||||
|
||||
public static List<CTBookmark> getFileBookMarks(InputStream in) {
|
||||
List<CTBookmark> list = new ArrayList<>();
|
||||
try (XWPFDocument doc = new XWPFDocument(in)) {
|
||||
@ -40,10 +45,10 @@ public class ContactSignUtils {
|
||||
*
|
||||
* @param wordFile 原始 Word 文档
|
||||
* @param imageFile 图片文件
|
||||
* @param outfileName 修改后的 Word 文档保存路径
|
||||
* @param outfileName 修改后的 Word 文档保存路径
|
||||
* @param bookmarkName 书签名称
|
||||
* @return 修改后的 Word 文件
|
||||
* @throws IOException IO 异常
|
||||
* @throws IOException IO 异常
|
||||
* @throws InvalidFormatException 格式异常
|
||||
*/
|
||||
public static File replaceBookmarkWithImage(InputStream wordFile, InputStream imageFile, String outfileName, String bookmarkName) throws IOException {
|
||||
@ -89,62 +94,129 @@ public class ContactSignUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static File convertToPDF(File file) {
|
||||
if (file == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
XWPFDocument docx = new XWPFDocument(fis);
|
||||
|
||||
// 创建 PDF 文档
|
||||
PDDocument pdf = new PDDocument();
|
||||
PDPage page = new PDPage();
|
||||
pdf.addPage(page);
|
||||
|
||||
// 获取 PDF 页面的内容流
|
||||
PDPageContentStream contentStream = new PDPageContentStream(pdf, page);
|
||||
|
||||
// 设置字体和字号
|
||||
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
|
||||
|
||||
// 遍历 docx 文档的段落和表格
|
||||
List<XWPFParagraph> paragraphs = docx.getParagraphs();
|
||||
List<XWPFTable> tables = docx.getTables();
|
||||
|
||||
for (XWPFParagraph paragraph : paragraphs) {
|
||||
String text = paragraph.getText();
|
||||
contentStream.beginText();
|
||||
contentStream.newLineAtOffset(25, 700);
|
||||
contentStream.showText(text);
|
||||
contentStream.endText();
|
||||
}
|
||||
|
||||
for (XWPFTable table : tables) {
|
||||
List<XWPFTableRow> rows = table.getRows();
|
||||
for (XWPFTableRow row : rows) {
|
||||
List<XWPFTableCell> cells = row.getTableCells();
|
||||
for (XWPFTableCell cell : cells) {
|
||||
String text = cell.getText();
|
||||
contentStream.beginText();
|
||||
contentStream.newLineAtOffset(25, 700);
|
||||
contentStream.showText(text);
|
||||
contentStream.endText();
|
||||
}
|
||||
}
|
||||
public static File convertToPDF(List<Map<String, Object>> files) {
|
||||
for (Map<String, Object> file : files) {
|
||||
InputStream in = word2PdfByYunpan(file);
|
||||
if (in == null) {
|
||||
logger.error("word2PdfByYunpan response null");
|
||||
return null;
|
||||
}
|
||||
FileUtil fileUtil = new FileUtil("sales-order-sign-file-pdf");
|
||||
File outFile = fileUtil.createNewFile(changeFileExtension(file.getName(), "pdf"));
|
||||
contentStream.close();
|
||||
pdf.save(outFile);
|
||||
pdf.close();
|
||||
fis.close();
|
||||
|
||||
return outFile;
|
||||
} catch (IOException e) {
|
||||
throw new KDBizException("Word转换PDF失败:" + e.getMessage());
|
||||
try {
|
||||
File outFile = fileUtil.createNewFile(changeFileExtension((String) file.get("name"), "pdf"));
|
||||
writeToFile(in, outFile);
|
||||
return outFile;
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void writeToFile(InputStream in, File file) {
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(file);
|
||||
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
while ((bytesRead = in.read(buffer)) != -1) {
|
||||
fos.write(buffer, 0, bytesRead);
|
||||
}
|
||||
logger.info("PDF文件写入成功");
|
||||
} catch (IOException e) {
|
||||
logger.error("PDF文件写入本地失败");
|
||||
logger.error(e.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (in != null) {
|
||||
in.close();
|
||||
}
|
||||
if (fos != null) {
|
||||
fos.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过预览服务器生成pdf文件
|
||||
*
|
||||
* @param urlWord 附件面板数据
|
||||
* @return 文件流
|
||||
*/
|
||||
public static InputStream word2PdfByYunpan(Map<String, Object> urlWord) {
|
||||
//需要添加到附件的文件
|
||||
String yunPanUrl = System.getProperty("yunpan.previewUrl");
|
||||
String getPdfUrl = yunPanUrl + "/api/officeToPdf";
|
||||
// 请求参数
|
||||
Map<String, Object> param = new HashMap<>(8);
|
||||
//文件id
|
||||
param.put("id", urlWord.get("uid"));
|
||||
//文件名
|
||||
String wordName = urlWord.get("name").toString();
|
||||
String[] wordNameStr = wordName.split("\\.");
|
||||
//获取文件类型 doc/docx
|
||||
param.put("ext", wordNameStr[wordNameStr.length - 1]);
|
||||
//构造原始word文件获取的url,需要携带token
|
||||
String url = urlWord.get("url").toString();
|
||||
logger.info("download_url {}", url);
|
||||
param.put("download_url", url
|
||||
+ "&access_token=" + RequestContext.get().getGlobalSessionId());
|
||||
String paramUrl = getGetParamUrl(param);
|
||||
String officeToPdfUrl = getPdfUrl + "?" + paramUrl;
|
||||
logger.info("officeToPdfUrl {}", officeToPdfUrl);
|
||||
//word 转pdf之后的文件流
|
||||
return getInputStreamByUrl(officeToPdfUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过浏览器下载获取文件流
|
||||
* 传入一个url 地址,获取文件二进制流
|
||||
*
|
||||
* @param pdfUrl 地址
|
||||
*/
|
||||
public static InputStream getInputStreamByUrl(String pdfUrl) {
|
||||
//输入流
|
||||
try {
|
||||
// 当作一个URL来装载文件
|
||||
URL url = new URL(pdfUrl);
|
||||
URLConnection con = url.openConnection();
|
||||
con.setConnectTimeout(3 * 1000);
|
||||
return con.getInputStream();
|
||||
} catch (Exception e) {
|
||||
logger.error("word转pdf失败", e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把key-value参数序列化成 url get参数
|
||||
*
|
||||
* @param body key-value
|
||||
* @return a=1&b=2
|
||||
*/
|
||||
public static String getGetParamUrl(Map<String, Object> body) {
|
||||
String client;
|
||||
String strBody = "";
|
||||
if (body != null && !body.isEmpty()) {
|
||||
Iterator<String> iterator = body.keySet().iterator();
|
||||
List<BasicNameValuePair> params = new ArrayList<>();
|
||||
while (iterator.hasNext()) {
|
||||
client = iterator.next();
|
||||
String value = body.get(client).toString();
|
||||
params.add(new BasicNameValuePair(client, value));
|
||||
}
|
||||
try {
|
||||
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
|
||||
strBody = EntityUtils.toString(entity);
|
||||
} catch (IOException var28) {
|
||||
logger.error("Http get error", var28);
|
||||
}
|
||||
}
|
||||
return strBody;
|
||||
}
|
||||
|
||||
public static String changeFileExtension(String fileName, String newExtension) {
|
||||
|
Loading…
Reference in New Issue
Block a user