java图片转换pdf_利用java实现一个图片转PDF文件工具

9cf2228dc5375f537b41d3f65f956491.png

出于某些需求需要将一张简单的图片转换为PDF的文件格式,因此自己动手写了一个图片转换PDF的系统,现在将该系统分享在这里,供大家参考。

(学习视频推荐:java课程)

具体代码:

引入依赖:

org.springframework.boot

spring-boot-starter-parent

2.0.4.RELEASE

org.springframework.boot

spring-boot-starter-web

com.itextpdf

itextpdf

5.4.2

前端页面:

图片转换Pdf

.submitButton {

margin-top: 20px;

margin-left: 150px;

background-color: #e37e10;

border-radius: 10px;

border: 1px solid #ff8300;

}

图片转换pdf工具

function allowFileType() {

let file = document.getElementById("file").files[0];

let fileName = file.name;

console.log(fileName)

let fileSize = file.size;

console.log(fileSize)

let suffix = fileName.substring(fileName.lastIndexOf("."),fileName.length);

if('.jpg' != suffix && '.png' != suffix) {

alert("目前只允许传入.jpg或者.png格式的图片!");

return false;

}

if(fileSize > 2*1024*1024) {

alert("上传图片不允许超过2MB!");

return false;

}

return true;

}

控制层接口package com.hrp.controller;

import com.hrp.util.PdfUtils;

import org.springframework.stereotype.Controller;

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.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**

* @description: 用于处理Pdf相关的请求

*/

@Controller

@RequestMapping("pdf")

public class PdfController {

@PostMapping("image/to")

public void imageToPdf(@RequestParam("file") MultipartFile file,HttpServletResponse response) throws Exception{

PdfUtils.imageToPdf(file,response);

}

}

PDF工具类package com.hrp.util;

import com.itextpdf.text.Document;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.Image;

import com.itextpdf.text.PageSize;

import com.itextpdf.text.pdf.PdfWriter;

import org.springframework.stereotype.Component;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import java.io.*;

import java.net.URLEncoder;

/**

* @description: pdf相关的工具类

*/

@Component

public class PdfUtils {

/**

* 图片转换PDF的公共接口

*

* @param file SpringMVC获取的图片文件

* @param response HttpServletResponse

* @throws IOException IO异常

* @throws DocumentException PDF文档异常

*/

public static void imageToPdf(MultipartFile file, HttpServletResponse response) throws IOException, DocumentException {

File pdfFile = generatePdfFile(file);

downloadPdfFile(pdfFile, response);

}

/**

* 将图片转换为PDF文件

*

* @param file SpringMVC获取的图片文件

* @return PDF文件

* @throws IOException IO异常

* @throws DocumentException PDF文档异常

*/

private static File generatePdfFile(MultipartFile file) throws IOException, DocumentException {

String fileName = file.getOriginalFilename();

String pdfFileName = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf";

Document doc = new Document(PageSize.A4, 20, 20, 20, 20);

PdfWriter.getInstance(doc, new FileOutputStream(pdfFileName));

doc.open();

doc.newPage();

Image image = Image.getInstance(file.getBytes());

float height = image.getHeight();

float width = image.getWidth();

int percent = getPercent(height, width);

image.setAlignment(Image.MIDDLE);

image.scalePercent(percent);

doc.add(image);

doc.close();

File pdfFile = new File(pdfFileName);

return pdfFile;

}

/**

*

* 用于下载PDF文件

*

* @param pdfFile PDF文件

* @param response HttpServletResponse

* @throws IOException IO异常

*/

private static void downloadPdfFile(File pdfFile, HttpServletResponse response) throws IOException {

FileInputStream fis = new FileInputStream(pdfFile);

byte[] bytes = new byte[fis.available()];

fis.read(bytes);

fis.close();

response.reset();

response.setHeader("Content-Type", "application/pdf");

response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(pdfFile.getName(), "UTF-8"));

OutputStream out = response.getOutputStream();

out.write(bytes);

out.flush();

out.close();

}

/**

* 等比压缩,获取压缩百分比

*

* @param height 图片的高度

* @param weight 图片的宽度

* @return 压缩百分比

*/

private static int getPercent(float height, float weight) {

float percent = 0.0F;

if (height > weight) {

percent = PageSize.A4.getHeight() / height * 100;

} else {

percent = PageSize.A4.getWidth() / weight * 100;

}

return Math.round(percent);

}

}

实现效果:

6b446d80679a92359e091aed6788ec07.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值