基于SpringBoot制作一個(gè)PDF切圖小工具
老婆需要一個(gè)PDF工具,咱能說(shuō)做不出來(lái)?
最近老婆需要將PDF文件分頁(yè)切割并轉(zhuǎn)成圖片,但使用在線工具時(shí)經(jīng)常遇到繁瑣的廣告。她問(wèn)我能否做一個(gè)工具來(lái)簡(jiǎn)化這個(gè)過(guò)程。作為程序員,咱程序員能說(shuō)不會(huì)做嗎?當(dāng)然不能,于是便有了我摸魚寫的一個(gè)小工具,【PDF切圖】,將源碼分享給大家。
【PDF切圖】源碼分享 跟著我一步一步來(lái)
1、首先我們需要一個(gè)SpringBoot項(xiàng)目
可以參考我的文章
【SpringBoot】從0~1 手把手教你搭建 分布式 項(xiàng)目
【SpringBoot】 一個(gè)優(yōu)秀Maven項(xiàng)目的各Model間最佳繼承設(shè)計(jì)
2、添加Maven依賴
在父Pom.xml文件中添加
<pdfbox.version>2.0.9</pdfbox.version>
?
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>${pdfbox.version}</version>
</dependency>
子pom.xml中添加
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
</dependency>
3、封裝我們的工具類(非常好用,直接分享給你們)
文件工具類FileUtils
package com.le.common.utils;
?
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
?
public class FileUtils {
?
?
/**
* 獲取不帶擴(kuò)展名的文件名
* @param fileName
* @return
*/
public static String getFileNameRemoveSuffix(String fileName) {
int pos = fileName.lastIndexOf(".");
if (pos > 0) {
return fileName.substring(0, pos);
}
return fileName;
}
?
/**
* 遞歸刪除指定目錄及其內(nèi)容。
*
* @param directory 要?jiǎng)h除的目錄
*/
public static void deleteDirectory(File directory) {
File[] allContents = directory.listFiles();
if (allContents != null) {
for (File file : allContents) {
deleteDirectory(file);
}
}
directory.delete();
}
?
/**
* 將指定目錄中的圖像壓縮為zip文件,并將其寫入輸出流中。
*
* @param directory 包含圖像文件的目錄
* @param outputStream 要寫入zip文件的輸出流
* @throws IOException 如果發(fā)生I/O錯(cuò)誤
*/
public static void zipImages(File directory, OutputStream outputStream) throws IOException {
try (ZipOutputStream zipOut = new ZipOutputStream(outputStream)) {
File[] imageFiles = directory.listFiles();
if (imageFiles != null) {
for (File imageFile : imageFiles) {
ZipEntry zipEntry = new ZipEntry(imageFile.getName());
zipOut.putNextEntry(zipEntry);
try (FileInputStream fis = new FileInputStream(imageFile)) {
byte[] bytes = new byte[1024];
int length;
while ((length = fis.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
}
}
}
}
}
?
/**
* 創(chuàng)建臨時(shí)文件
* @return
* @throws IOException
*/
public static File createTempDirectory() throws IOException {
File tempDir = File.createTempFile("temp", Long.toString(System.nanoTime()));
if (!tempDir.delete() || !tempDir.mkdir()) {
throw new IOException("Could not create temp directory: " + tempDir);
}
return tempDir;
}
}PDF工具類PdfUtils
package com.le.common.utils;
?
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
?
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
?
import org.apache.pdfbox.multipdf.Splitter;
?
public class PdfUtils {
?
/**
* 將PDF文件拆分為單獨(dú)的圖像,并將它們保存在指定的目錄中。
* PDF的每一頁(yè)被轉(zhuǎn)換為一個(gè)單獨(dú)的圖像文件。
*
* @param inputPdfFile 要拆分的輸入PDF文件
* @param outputImageDirectory 圖像文件將要保存的目錄
* @throws IOException 如果發(fā)生I/O錯(cuò)誤
*/
public static void splitPdfToImages(File inputPdfFile, File outputImageDirectory) throws IOException {
try (PDDocument document = PDDocument.load(inputPdfFile)) {
Splitter splitter = new Splitter();
List<PDDocument> pages = splitter.split(document);
int pageIndex = 1;
for (PDDocument page : pages) {
PDFRenderer renderer = new PDFRenderer(page);
BufferedImage image = renderer.renderImageWithDPI(0, 300, ImageType.RGB);
// 寫入圖像文件
File outputImage = new File(outputImageDirectory, "page" + pageIndex + ".png");
ImageIO.write(image, "png", outputImage);
pageIndex++;
page.close();
}
}
}
?
}4、接口:PDF文件切割成圖片輸出
package com.le.admin.after.api.mytools;
?
import com.le.common.utils.FileUtils;
import com.le.common.utils.PdfUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
?
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
?
@RestController
@RequestMapping("/tools/pdf")
public class PdfToolController {
?
/**
* pdf文件切割成圖片輸出
* @param pdfFile
* @return
*/
@PostMapping("/pdfCutToImage")
public ResponseEntity<byte[]> processPdf(@RequestParam("pdfFile") MultipartFile pdfFile) {
try {
File tempDir = FileUtils.createTempDirectory();
File pdfInputFile = new File(tempDir, pdfFile.getOriginalFilename());
pdfFile.transferTo(pdfInputFile);
?
// 將PDF文件拆分為單獨(dú)的圖像
PdfUtils.splitPdfToImages(pdfInputFile, tempDir);
?
// 將圖像壓縮為zip文件
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileUtils.zipImages(tempDir, baos);
?
// 刪除臨時(shí)目錄及其內(nèi)容
FileUtils.deleteDirectory(tempDir);
?
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=images.zip");
?
return ResponseEntity.ok()
.headers(headers)
.contentType(MediaType.parseMediaType("application/zip"))
.body(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
}以上就是基于SpringBoot制作一個(gè)PDF切圖小工具的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot PDF切圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Mybatis超級(jí)強(qiáng)大的動(dòng)態(tài)SQL語(yǔ)句大全
MyBatis的動(dòng)態(tài)SQL是基于OGNL表達(dá)式的,它可以幫助我們方便的在SQL語(yǔ)句中實(shí)現(xiàn)某些邏輯,下面這篇文章主要給大家介紹了關(guān)于Mybatis超級(jí)強(qiáng)大的動(dòng)態(tài)SQL語(yǔ)句的相關(guān)資料,需要的朋友可以參考下2022-05-05
SpringBoot統(tǒng)一返回處理出現(xiàn)cannot?be?cast?to?java.lang.String異常解決
這篇文章主要給大家介紹了關(guān)于SpringBoot統(tǒng)一返回處理出現(xiàn)cannot?be?cast?to?java.lang.String異常解決的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
Java簡(jiǎn)單計(jì)算兩個(gè)日期月數(shù)差的方法
這篇文章主要介紹了Java簡(jiǎn)單計(jì)算兩個(gè)日期月數(shù)差的方法,結(jié)合實(shí)例形式分析了java使用Calendar類進(jìn)行日期時(shí)間操作相關(guān)技巧,需要的朋友可以參考下2017-06-06
springboot整合nacos的入門Demo及Nacos安裝部署
Nacos?提供了一組簡(jiǎn)單易用的特性集,幫助您快速實(shí)現(xiàn)動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、服務(wù)配置、服務(wù)元數(shù)據(jù)及流量管理,Nacos?致力于幫助您發(fā)現(xiàn)、配置和管理微服務(wù),這篇文章主要介紹了springboot整合nacos的入門Demo,需要的朋友可以參考下2024-01-01
k8s部署java項(xiàng)目的實(shí)現(xiàn)
本文主要介紹了k8s部署java項(xiàng)目的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
Mybatis日期格式自動(dòng)轉(zhuǎn)換需要用到的兩個(gè)注解說(shuō)明
這篇文章主要介紹了Mybatis日期格式自動(dòng)轉(zhuǎn)換需要用到的兩個(gè)注解說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

