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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/448362.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/448362.shtml
英文地址,請注明出處:http://en.pswp.cn/news/448362.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

const的用法,特別是用在函數后面

原文出處&#xff1a;http://blog.csdn.net/zcf1002797280/article/details/7816977

圖解 Linux 安裝 JDK1.8 、配置環境變量

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 到官網下載 JDK 1.8 https://www.oracle.com/technetwork/java/javase/downloads/index.html 2. 用 rz 命令把 jdk-8u191-linux-x6…

剎車

定義 剎車就是可以減慢車速的機械制動裝置&#xff0c;又名減速器。簡單來說&#xff0c;汽車剎車踏板在方向盤下面&#xff0c;踩住剎車踏板&#xff0c;則使剎車杠桿聯動受壓并傳至到剎車鼓上的剎車片卡住剎車輪盤&#xff0c;使汽車減速或停止運行。作用 目的是減速&a…

【原創】Performanced C++ 經驗規則 第五條:再談重載、覆蓋和隱藏

第五條&#xff1a;再談重載、覆蓋和隱藏 在C中&#xff0c;無論在類作用域內還是外&#xff0c;兩個&#xff08;或多個&#xff09;同名的函數&#xff0c;可能且僅可能是以下三種關系&#xff1a;重載&#xff08;Overload&#xff09;、覆蓋&#xff08;Override&#xff0…

C++之純虛函數和抽象類

純虛函數和抽象類 1.基本概念 2.案例 #include <iostream> using namespace std;////面向抽象類編程(面向一套預先定義好的接口編程)//解耦合 ....模塊的劃分class Figure //抽象類 { public://閱讀一個統一的界面(接口),讓子類使用,讓子類必須去實現virtual void get…

解決: -bash: $‘\302\240docker‘: command not found

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 我只是運行 一條很簡單的啟動容器的命令&#xff0c;多次執行都報錯&#xff0c;報錯如題&#xff1a; -bash: $\302\240docker: comma…

換擋/掛檔

定義 換擋/掛檔是指變速器&#xff0c;用于轉變發動機曲軸的轉矩及轉速&#xff0c;以適應汽車在起步、加速、行駛以及克服各種道路阻礙等不同行駛條件下對驅動車輪牽引力及車速不同要求的需要。作用 使汽車能以非常低的穩定車速行駛&#xff0c;而這種低的轉速只靠內然…

sql:無法解決 equal to 操作中 Chinese_PRC_CI_AS 和 Chinese_Taiwan_Stroke_CI_AS 之間的排序規則沖突。...

--無法解決 equal to 操作中 "Chinese_PRC_CI_AS" 和 "Chinese_Taiwan_Stroke_CI_AS" 之間的排序規則沖突。 CREATE VIEW View_VipBranchStaffBranchList AS select VipBranchStaff.*,geovindu_branch.B_Name,VipExamCountry.ExamCountryName from VipBran…

【汽車取證篇】GA-T 1998-2022《汽車車載電子數據提取技術規范》(附下載)

【汽車取證篇】GA-T 1998-2022《汽車車載電子數據提取技術規范》&#xff08;附下載&#xff09; GA-T 1998-2022《汽車車載電子數據提取技術規范》標準—【蘇小沐】 總結 公眾號回復關鍵詞【汽車取證】自動獲取資源合集&#xff0c;如鏈接失效請留言&#xff0c;便于…

解決: Client does not support authentication protocol requested by server; consider upgrading MySQL

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 在服務器上把 mysql 裝好后&#xff0c;運行起來。 2. navicat 死活連接不上&#xff0c;在網上查說是要改數據庫賬號、密碼什么的&…

C++之STL理論基礎

1.基本概念 STL&#xff08;Standard Template Library&#xff0c;標準模板庫)是惠普實驗室開發的一系列軟件的統稱。雖然主要出現在C中&#xff0c;但在被引入C之前該技術就已經存在了很長的一段時間。 STL的從廣義上講分為三部分&#xff1a;algorithm&#xff08;算法&am…

方向盤

定義 方向盤是汽車、輪船、飛機等的操縱行駛方向的輪狀裝置。 構成 一般由骨架和發泡組合起來就是最簡單的方向盤了&#xff0c;而方向盤上都會有和主駕駛氣囊對應的安裝卡扣或螺釘孔&#xff0c;其下方一般會有多功能開關模塊。作用 方向盤不僅可以控制車輛的方向…

數據庫范式俗話

1NF&#xff1a;一個table中的列是不可再分的&#xff08;即列的原子性&#xff09; 2NF&#xff1a;一個table中的行是可以唯一標示的&#xff0c;&#xff08;即table中的行是不可以 重復的&#xff09; 3NF&#xff1a;一個table中的列不依賴于另一個table中的非主鍵列 4NF&…

STL之string類型

1.String概念 string是STL的字符串類型&#xff0c;通常用來表示字符串。而在使用string之前&#xff0c;字符串通常是用char*表示的。 string和char*的區別&#xff1a; string是一個類, char*是一個指向字符的指針。 string封裝了char*&#xff0c;管理這個字符串&#x…

解決maven打包報錯:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 一、報錯經歷&#xff1a; 今天使用eclipse通過maven install打war包的時候&#xff0c;出現了下圖所示的錯誤 二、問題分析&#xff1a…

離合器

離合器的定義 汽車離合器位于發動機和變速箱之間的飛輪殼內&#xff0c;用螺釘將離合器總成固定在飛輪的后平面上&#xff0c;離合器的輸出軸就是變速箱的輸入軸。在汽車行駛過程中&#xff0c;駕駛員可根據需要踩下或松開離合器踏板&#xff0c;使發動機與變速箱暫時分離和…

Python 刪除滿足條件的某些行

數據&#xff1a; data 字段&#xff1a;col 要刪除的內容是 col False 的行 # 方案一 data1 data[~data[col] False] # ~ 取反# 方案二 保留 data[已采] ! False ind data[col] ! False data2 data.loc[ind,]# 方案三 去掉 data[已采] True ind2 data[col] False…

STL之Vector

1.簡介 vector是將元素置于一個動態數組中加以管理的容器。可以隨機存取元素&#xff08;支持索引值直接存取&#xff0c;用[]操作符或at()方法&#xff0c;還支持迭代器方式存取&#xff09;。   vector尾部添加或移除元素非常快速。但是在中部或頭部插入元素或移除元素比…

解決 : Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile

前些天發現了一個巨牛的人工智能學習網站&#xff0c;通俗易懂&#xff0c;風趣幽默&#xff0c;忍不住分享一下給大家。點擊跳轉到教程。 1. 執行 maven install 命令報錯如題&#xff1a; Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:comp…

制動踏板

什么是制動踏板 制動踏板就是限制動力的踏板&#xff0c;即腳剎(行車制動器)的踏板&#xff0c;是長時間摩擦導致剎車片過熱軟化的原因。制動踏板的作用 其主要作用是剎車減速或停車。 制動踏板的工作原理 在機器的高速軸上固定一個輪或盤&#xff0c;在機座上安裝…