--添加依賴
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
???<groupId>org.apache.pdfbox</groupId>
???<artifactId>pdfbox</artifactId>
???<version>2.0.12</version>
</dependency>
--最佳實踐
package com.dhht.wechat.util;
import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPageTree;
import org.apache.pdfbox.rendering.PDFRenderer;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
/**
?* @Author: sh
?* @Description: PDFUtil
?* @Date: 11:35 2019/7/1
?*/
public class PDFUtil {
????/**
?????* 將base64字符串轉換為PDF在顯示到頁面中
?????* @param base64String
?????* @param httpServletResponse
?????*/
????public static void base64StringToPDFToPage(String base64String, HttpServletResponse httpServletResponse){
????????BASE64Decoder decoder = new BASE64Decoder();
????????ByteArrayOutputStream baos = null;
????????ServletOutputStream sos = null;
????????try {
????????????byte[] bytes = decoder.decodeBuffer(base64String);
????????????baos = new ByteArrayOutputStream();
????????????baos.write(bytes); //把byte寫進輸出流里
????????????if (baos != null) {
????????????????httpServletResponse.setContentType("application/pdf");
????????????????httpServletResponse.setContentLength(baos.size());
????????????????httpServletResponse.setHeader("Expires", "0");
????????????????httpServletResponse.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
????????????????httpServletResponse.setHeader("Pragma", "public");
????????????????// 設置打印PDF的文件名
????????????????String fileName = "社保證明文件.pdf";
????????????????fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
????????????????httpServletResponse.setHeader("Content-Disposition", "filename=" + fileName);
????????????????sos = httpServletResponse.getOutputStream();
????????????????baos.writeTo(sos); //byte輸出流寫入servlet輸出流
????????????????sos.flush();
????????????}
????????} catch (IOException e) {
????????????e.printStackTrace();
????????} finally {
????????????try {
????????????????sos.close();
????????????????baos.close();
????????????} catch (IOException e) {
????????????????e.printStackTrace();
????????????}
????????}
????}
????/**
?????* ?將base64編碼轉換成PDF
?????* ?@param base64String
?????* ?1.使用BASE64Decoder對編碼的字符串解碼成字節數組
?????* ?2.使用底層輸入流ByteArrayInputStream對象從字節數組中獲取數據;
?????* ?3.建立從底層輸入流中讀取數據的BufferedInputStream緩沖輸出流對象;
?????* ?4.使用BufferedOutputStream和FileOutputSteam輸出數據到指定的文件中
?????*/
????public static void base64StringToPDF(String base64String, String pdfPath/*File file*/){
????????File file = new File(pdfPath);// 將原來參數修改為字符串
????????BASE64Decoder decoder = new BASE64Decoder();
????????BufferedInputStream bin = null;
????????FileOutputStream fout = null;
????????BufferedOutputStream bout = null;
????????try {
????????????//將base64編碼的字符串解碼成字節數組
????????????byte[] bytes = decoder.decodeBuffer(base64String);
????????????//創建一個將bytes作為其緩沖區的ByteArrayInputStream對象
????????????ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
????????????//創建從底層輸入流中讀取數據的緩沖輸入流對象
????????????bin = new BufferedInputStream(bais);
????????????//創建到指定文件的輸出流
????????????fout ?= new FileOutputStream(file);
????????????//為文件輸出流對接緩沖輸出流對象
????????????bout = new BufferedOutputStream(fout);
????????????byte[] buffers = new byte[1024];
????????????int len = bin.read(buffers);
????????????while(len != -1){
????????????????bout.write(buffers, 0, len);
????????????????len = bin.read(buffers);
????????????}
????????????//刷新此輸出流并強制寫出所有緩沖的輸出字節,必須這行代碼,否則有可能有問題
????????????bout.flush();
????????} catch (IOException e) {
????????????e.printStackTrace();
????????} finally {
????????????try {
????????????????bout.close();
????????????????fout.close();
????????????????bin.close();
????????????} catch (IOException e) {
????????????????e.printStackTrace();
????????????}
????????}
????}
????/**
?????* PDF轉換為Base64編碼
?????* @param file
?????* @return
?????*/
????public static String pdfToBase64(File file) {
????????BASE64Encoder encoder = new BASE64Encoder();
????????FileInputStream fin =null;
????????BufferedInputStream bin =null;
????????ByteArrayOutputStream baos = null;
????????BufferedOutputStream bout =null;
????????try {
????????????fin = new FileInputStream(file);
????????????bin = new BufferedInputStream(fin);
????????????baos = new ByteArrayOutputStream();
????????????bout = new BufferedOutputStream(baos);
????????????byte[] buffer = new byte[1024];
????????????int len = bin.read(buffer);
????????????while(len != -1){
????????????????bout.write(buffer, 0, len);
????????????????len = bin.read(buffer);
????????????}
????????????//刷新此輸出流并強制寫出所有緩沖的輸出字節
????????????bout.flush();
????????????byte[] bytes = baos.toByteArray();
????????????return encoder.encodeBuffer(bytes).trim();
????????} catch (FileNotFoundException e) {
????????????e.printStackTrace();
????????} catch (IOException e) {
????????????e.printStackTrace();
????????} finally {
????????????try {
????????????????fin.close();
????????????????bin.close();
????????????????baos.close();
????????????????bout.close();
????????????} catch (IOException e) {
????????????????e.printStackTrace();
????????????}
????????}
????????return null;
????}
????/**
?????* pdf轉jpg
?????* @param pdfPath
?????* @param jpgPath
?????*/
????public static void pdfToJpg(String pdfPath,String jpgPath){
????????long start = System.currentTimeMillis();
????????//pdf路徑
????????InputStream stream = null;
????????try {
????????????stream = new FileInputStream(new File(pdfPath));//URLUtil.getStream(url);
????????????// 加載解析PDF文件
????????????PDDocument doc = PDDocument.load(stream);
????????????PDFRenderer pdfRenderer = new PDFRenderer(doc);
????????????PDPageTree pages = doc.getPages();
????????????int pageCount = pages.getCount();
????????????for (int i = 0; i < pageCount; i++) {
????????????????BufferedImage bim = pdfRenderer.renderImageWithDPI(i, 200);
????????????????ByteArrayOutputStream os = new ByteArrayOutputStream();
????????????????ImageIO.write(bim, "jpg", os);
????????????????byte[] datas = os.toByteArray();
????????????????FileUtils.writeByteArrayToFile(new File(jpgPath),datas);
????????????}
????????????long end = System.currentTimeMillis();
????????????long time = (end - start) / 1000;
????????????System.out.println("pdf轉jpg耗時: {}s"+time);
????????}catch (Exception e){
????????}
????}
????/**
?????* base64轉jpg
?????* @param val
?????* @param pdfFile
?????* @param jpgFile
?????*/
????public static void base64ToJPG(String val,String pdfFile,String jpgFile){
????????base64StringToPDF(val,pdfFile);
????????pdfToJpg(pdfFile,jpgFile);
????}
}