From 1021f56fb1ebc7564b30d8648e7678f45f4588a2 Mon Sep 17 00:00:00 2001 From: Crunchy <3114200645@qq.com> Date: 星期二, 23 七月 2024 10:23:33 +0800 Subject: [PATCH] 修复无法保存文件后缀bug --- inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsReportServiceImpl.java | 166 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 132 insertions(+), 34 deletions(-) diff --git a/inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsReportServiceImpl.java b/inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsReportServiceImpl.java index ea79da8..f238926 100644 --- a/inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsReportServiceImpl.java +++ b/inspect-server/src/main/java/com/yuanchu/mom/service/impl/InsReportServiceImpl.java @@ -1,13 +1,17 @@ package com.yuanchu.mom.service.impl; -import com.aspose.words.License; -import com.aspose.words.SaveFormat; +import com.aspose.words.*; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.data.Pictures; +import com.itextpdf.text.BadElementException; +import com.itextpdf.text.DocumentException; +import com.itextpdf.text.pdf.PdfContentByte; +import com.itextpdf.text.pdf.PdfReader; +import com.itextpdf.text.pdf.PdfStamper; import com.yuanchu.mom.common.GetLook; import com.yuanchu.mom.common.PrintChina; import com.yuanchu.mom.dto.ReportPageDto; @@ -25,10 +29,10 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.time.LocalDateTime; @@ -37,13 +41,13 @@ import java.util.concurrent.CompletableFuture; /** -* @author Administrator -* @description 閽堝琛ㄣ�恑ns_report(妫�楠屾姤鍛�)銆戠殑鏁版嵁搴撴搷浣淪ervice瀹炵幇 -* @createDate 2024-03-17 22:10:02 -*/ + * @author Administrator + * @description 閽堝琛ㄣ�恑ns_report(妫�楠屾姤鍛�)銆戠殑鏁版嵁搴撴搷浣淪ervice瀹炵幇 + * @createDate 2024-03-17 22:10:02 + */ @Service public class InsReportServiceImpl extends ServiceImpl<InsReportMapper, InsReport> - implements InsReportService{ + implements InsReportService { @Resource private GetLook getLook; @@ -98,16 +102,16 @@ String signatureUrl; try { signatureUrl = userMapper.selectById(insReport.getWriteUserId()).getSignatureUrl(); - }catch (Exception e){ + } catch (Exception e) { throw new ErrorException("鎵句笉鍒扮紪鍒朵汉鐨勭鍚�"); } //绯荤粺鐢熸垚鎶ュ憡鍦板潃 String url = insReport.getUrl(); //鎵嬪姩涓婁紶鎶ュ憡鍦板潃 String urlS = insReport.getUrlS(); - wordInsertUrl(new HashMap<String, Object>(){{ - put("writeUrl", Pictures.ofLocal(imgUrl+"/"+signatureUrl).create()); - }}, (urlS==null?url:urlS).replace("/word", wordUrl)); + wordInsertUrl(new HashMap<String, Object>() {{ + put("writeUrl", Pictures.ofLocal(imgUrl + "/" + signatureUrl).create()); + }}, (urlS == null ? url : urlS).replace("/word", wordUrl)); return insReportMapper.updateById(insReport); } @@ -121,7 +125,7 @@ } insReport.setExamineUserId(getLook.selectPowerByMethodAndUserId(null).get("userId"));//瀹℃牳浜� insReport.setExamineTime(LocalDateTime.now());//瀹℃牳鏃堕棿 - if (isExamine==0){ + if (isExamine == 0) { //濡傛灉瀹℃牳涓嶉�氳繃 insReport.setState(0);//鎻愪氦鐘舵�佹敼涓哄緟鎻愪氦 return insReportMapper.updateById(insReport); @@ -130,16 +134,16 @@ String signatureUrl; try { signatureUrl = userMapper.selectById(insReport.getExamineUserId()).getSignatureUrl(); - }catch (Exception e){ + } catch (Exception e) { throw new ErrorException("鎵句笉鍒板鏍镐汉鐨勭鍚�"); } //绯荤粺鐢熸垚鎶ュ憡鍦板潃 String url = insReport.getUrl(); //鎵嬪姩涓婁紶鎶ュ憡鍦板潃 String urlS = insReport.getUrlS(); - wordInsertUrl(new HashMap<String, Object>(){{ - put("examineUrl", Pictures.ofLocal(imgUrl+"/"+signatureUrl).create()); - }}, (urlS==null?url:urlS).replace("/word", wordUrl)); + wordInsertUrl(new HashMap<String, Object>() {{ + put("examineUrl", Pictures.ofLocal(imgUrl + "/" + signatureUrl).create()); + }}, (urlS == null ? url : urlS).replace("/word", wordUrl)); return insReportMapper.updateById(insReport); } @@ -154,7 +158,7 @@ } insReport.setRatifyUserId(getLook.selectPowerByMethodAndUserId(null).get("userId"));//鎵瑰噯浜� insReport.setRatifyTime(LocalDateTime.now());//鎵瑰噯鏃堕棿 - if (isRatify==0){ + if (isRatify == 0) { //濡傛灉鎵瑰噯涓嶉�氳繃 insReport.setState(0);//鎻愪氦鐘舵�佹敼涓哄緟鎻愪氦 return insReportMapper.updateById(insReport); @@ -163,27 +167,44 @@ String signatureUrl; try { signatureUrl = userMapper.selectById(insReport.getRatifyUserId()).getSignatureUrl(); - }catch (Exception e){ + } catch (Exception e) { throw new ErrorException("鎵句笉鍒版壒鍑嗕汉鐨勭鍚�"); } + //鑾峰彇鍦烘墍鐨勬姤鍛婁笓鐢ㄧ珷 String sealUrl; try { String laboratory = insOrderMapper.selectById(insReport.getInsOrderId()).getLaboratory(); sealUrl = insReportMapper.getLaboratoryByName(laboratory); - }catch (Exception e){ + } catch (Exception e) { throw new ErrorException("鎵句笉鍒版姤鍛婁笓鐢ㄧ珷"); } - if(sealUrl==null) throw new ErrorException("鎵句笉鍒版姤鍛婁笓鐢ㄧ珷"); + if (sealUrl == null) throw new ErrorException("鎵句笉鍒版姤鍛婁笓鐢ㄧ珷"); //绯荤粺鐢熸垚鎶ュ憡鍦板潃 String url = insReport.getUrl(); //鎵嬪姩涓婁紶鎶ュ憡鍦板潃 String urlS = insReport.getUrlS(); - wordInsertUrl(new HashMap<String, Object>(){{ - put("ratifyUrl", Pictures.ofLocal(imgUrl+"/"+signatureUrl).create()); - put("seal1", Pictures.ofLocal(imgUrl+"/"+sealUrl).create()); - put("seal2", Pictures.ofLocal(imgUrl+"/"+sealUrl).create()); - }}, (urlS==null?url:urlS).replace("/word", wordUrl)); - wordToPdf((urlS == null ? url : urlS).replace("/word", wordUrl)); + String finalUrl = (urlS == null ? url : urlS).replace("/word", wordUrl); + wordInsertUrl(new HashMap<String, Object>() {{ + put("ratifyUrl", Pictures.ofLocal(imgUrl + "/" + signatureUrl).create()); + put("seal1", Pictures.ofLocal(imgUrl + "/" + sealUrl).create()); + put("seal2", Pictures.ofLocal(imgUrl + "/" + sealUrl).create()); + }}, finalUrl); + wordToPdf(finalUrl,sealUrl); + + /* String replace = finalUrl.replace(".docx", ".pdf"); + CompletableFuture.supplyAsync(() -> { + try { + stamperCheckMarkPDF(replace,replace,sealUrl); + return null; + } catch (Exception e) { + throw new ErrorException("楠戠紳绔犳彃鍏ュけ璐�"); + } + }).thenAccept(res -> { + }).exceptionally(e -> { + e.printStackTrace(); + return null; + });*/ + InsOrder insOrder = new InsOrder(); insOrder.setId(insReportMapper.selectById(id).getInsOrderId()); insOrder.setState(4); @@ -203,10 +224,10 @@ } @Override - public void wordToPdf(String path) { + public void wordToPdf(String path,String sealUrl) { CompletableFuture.supplyAsync(() -> { try { - wordToPdf(path, path.replace(".docx", ".pdf")); + wordToPdf(path, path.replace(".docx", ".pdf"),sealUrl); return null; } catch (Exception e) { throw new ErrorException("杞崲澶辫触"); @@ -218,11 +239,23 @@ }); } - public String wordToPdf(String wordPath,String pdfPath) { + public String wordToPdf(String wordPath, String pdfPath,String sealUrl) { FileOutputStream os = null; try { //鍑瘉 涓嶇劧鍒囨崲鍚庢湁姘村嵃 // InputStream inputStream = this.getClass().getResourceAsStream("/lib/license.xml"); + /*String url; + try { + InputStream inputStream = this.getClass().getResourceAsStream("/lib/license.xml"); + File file = File.createTempFile("temp", ".tmp"); + OutputStream outputStream = new FileOutputStream(file); + IOUtils.copy(inputStream, outputStream); + url = file.getAbsolutePath(); + } catch (FileNotFoundException e) { + throw new ErrorException("鎵句笉鍒版ā鏉挎枃浠�"); + } catch (IOException e) { + throw new RuntimeException(e); + }*/ InputStream is = new ClassPathResource("/lib/license.xml").getInputStream(); License license = new License(); license.setLicense(is); @@ -231,11 +264,16 @@ return null; } //鐢熸垚涓�涓┖鐨凱DF鏂囦欢 - File file = new File(pdfPath); + File file = new File(pdfPath.replace(".pdf","-1.pdf")); os = new FileOutputStream(file); //瑕佽浆鎹㈢殑word鏂囦欢 com.aspose.words.Document doc = new com.aspose.words.Document(wordPath); doc.save(os, SaveFormat.PDF); + + //娣诲姞楠戠紳绔� + stamperCheckMarkPDF(pdfPath.replace(".pdf","-1.pdf"),pdfPath,imgUrl + "/" +sealUrl); + + } catch (Exception e) { e.printStackTrace(); } finally { @@ -249,8 +287,68 @@ } return null; } + + /** + * 鍒囧壊鍥剧墖 + * @param Path 鍥剧墖璺緞 + * @param n 鍒囧壊浠芥暟 + */ + public static com.itextpdf.text.Image[] slicingImages(String Path, int n) throws IOException, BadElementException { + com.itextpdf.text.Image[] nImage = new com.itextpdf.text.Image[n]; + BufferedImage img = ImageIO.read(new File(Path)); + + int h = img.getHeight(); + int w = img.getWidth(); + + int sw = w/n; + for(int i=0;i<n;i++){ + BufferedImage subImg; + if(i==n-1){//鏈�鍚庡墿浣欓儴鍒� + subImg = img.getSubimage(i * sw, 0, w-i*sw, h); + }else {//鍓峮-1鍧楀潎鍖�鍒� + subImg = img.getSubimage(i * sw, 0, sw, h); + } + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ImageIO.write(subImg,Path.substring(Path.lastIndexOf('.')+1),out); + nImage[i] = com.itextpdf.text.Image.getInstance(out.toByteArray()); + + } + return nImage; + } + + /** + * 鐩栭獞缂濈珷 + * + * @param infilePath 鍘烶DF璺緞 + * @param outFilePath 杈撳嚭PDF璺緞 + */ + public static void stamperCheckMarkPDF(String infilePath,String outFilePath,String picPath) throws IOException, DocumentException { + PdfReader reader = new PdfReader(infilePath);//閫夋嫨闇�瑕佸嵃绔犵殑pdf + PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(outFilePath));//鍔犲畬鍗扮珷鍚庣殑pdf + + + com.itextpdf.text.Rectangle pageSize = reader.getPageSize(1);//鑾峰緱绗竴椤� + float height = pageSize.getHeight(); + float width = pageSize.getWidth(); + + int nums = reader.getNumberOfPages(); + com.itextpdf.text.Image[] nImage = slicingImages(picPath,nums);//鐢熸垚楠戠紳绔犲垏鍓插浘鐗� + + for(int n=1;n<=nums;n++){ + PdfContentByte over = stamp.getOverContent(n);//璁剧疆鍦ㄧ鍑犻〉鎵撳嵃鍗扮珷 + com.itextpdf.text.Image img = nImage[n-1];//閫夋嫨鍥剧墖 + float newHeight = 100f; + float newWidth = img.getWidth()/(img.getHeight()/100); + img.scaleAbsolute(newWidth,newHeight);//鎺у埗鍥剧墖澶у皬 + img.setAbsolutePosition(width-newWidth,height/2-newHeight/2);//鎺у埗鍥剧墖浣嶇疆 + over.addImage(img); + } + stamp.close(); + } } + + -- Gitblit v1.9.3