生活随笔
收集整理的這篇文章主要介紹了
excel转实现pdf、图片、base64互转
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一:jar包
aspose-cells依賴
excel轉成pdf以及去除水印所需依賴包
鏈接:https://pan.baidu.com/s/1rWeG1wgnIP03YmqpD1Vp5g
提取碼:4gw1
<dependency><groupId>org
.apache
.pdfbox
</groupId
><artifactId>pdfbox
</artifactId
><version>2.0.12</version
></dependency
>
package com.emodor.attendance.utils.itext;import com.aspose.cells.PdfSaveOptions;
import com.aspose.cells.Workbook;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.openxml4j.opc.internal.FileHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Encoder;import java.io.*;
import java.util.Base64;
public class ExcelToPDFUtil {public static boolean isWordLicense() {return true;
}public static String getImageStr(String imgFile
) {InputStream inputStream
= null;byte[] data
= null;try {inputStream
= new FileInputStream(imgFile
);data
= new byte[inputStream
.available()];inputStream
.read(data
);inputStream
.close();} catch (IOException e
) {e
.printStackTrace();}BASE64Encoder encoder
= new BASE64Encoder();return encoder
.encode(data
);}public static String excel2pdf(String Address) {if (!isWordLicense()) { return null;}String imageStr
=null;try {File pdfFile
= new File("D:/data/2.pdf"); FileInputStream excelstream
= new FileInputStream(Address);Workbook wb
= new Workbook(excelstream
);FileOutputStream fileOS
= new FileOutputStream(pdfFile
);PdfSaveOptions pdfSaveOptions
= new PdfSaveOptions();pdfSaveOptions
.setOnePagePerSheet(true);wb
.save(fileOS
, pdfSaveOptions
);
imageStr
= getImageStr("D:/data/4.png");fileOS
.close();} catch (Exception e
) {e
.printStackTrace();}return imageStr
;}public static void main(String[] args
) {String s
= excel2pdf("D:/data/1.xlsx");System.out
.println(s
);}public static void pdfToImageLast(String PdfFilePath, File files
, float dpi
) {File file
= new File(PdfFilePath);@SuppressWarnings("resource")PDDocument pdDocument
= null;try {if (files
!= null) {pdDocument
= PDDocument.load(file
);PDFRenderer renderer
= new PDFRenderer(pdDocument
);PdfReader reader
= new PdfReader(PdfFilePath);int pages
= reader
.getNumberOfPages();logger
.info("PDF文檔轉PNG圖片: pdf總共多少頁-----" + pages
);BufferedImage image
= renderer
.renderImageWithDPI(0, dpi
);ImageIO.write(image
, "png", files
);} else {System.out
.println("PDF文檔轉PNG圖片失敗:" + "創建" + "失敗");}} catch (IOException e
) {logger
.error("PDF文檔轉PNG圖片失敗:", e
);} finally {try {pdDocument
.close();} catch (IOException e
) {logger
.error("PDF文件轉圖片 - 關閉PDF文件失敗!!! ", e
);}}}}
總結
以上是生活随笔為你收集整理的excel转实现pdf、图片、base64互转的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。