日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Base64转PDF、PDF转IMG(使用pdfbox插件)

發布時間:2023/12/18 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Base64转PDF、PDF转IMG(使用pdfbox插件) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

--添加依賴

<!-- 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);
????}


}

轉載于:https://www.cnblogs.com/sung1024/p/11178360.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Base64转PDF、PDF转IMG(使用pdfbox插件)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。