java图片转换pdf_Java实现图片转换PDF文件的示例代码
最近因为一些事情,需要将一张简单的图片转换为PDF的文件格式,在网上找了一些工具,但是这些工具不是需要注册账号,就是需要下载软件。
而对于只是转换一张图片的情况下,这些操作显然是非常繁琐的,所以作者就直接使用java写了一个图片转换PDF的系统,现在将该系统分享在这里。
引入依赖
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) http://www.cppcns.comthrows 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(hwww.cppcns.comeight, 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 图片的宽度
ZnYLZdZ* @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);
}
}
页面效果
这就是系统启动之后的页面效果,虽然页面比较http://www.cppcns.com简陋,但是功能却没有任何折扣,有兴趣或者有需要的同学可以自己搭建一下,试一试图片转换PDF文件的效果。
注意:作者自己测试了一下,普通图片基本是没有问题的,但是遇到一些特殊的图片可能会出现异常,毕竟只是一个比较简单的图片转换PDF系统,难以兼容所有图片。
到此这篇关于Java实现图片转换PDF文件的示例http://www.cppcns.com代码的文章就介绍到这了,更多相关Java 图片转换PDF内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
本文标题: Java实现图片转换PDF文件的示例代码
本文地址: http://www.cppcns.com/ruanjian/java/341084.html
java图片转换pdf_Java实现图片转换PDF文件的示例代码相关推荐
- java txt 写入pdf_java学习笔记 读取pdf文件并写成txt文件输出
1.用到的jar有:pdfbox-1.8.4.jar ,fontbox-1.8.4.jar ,pdfbox-app-1.8.4.jar , preflight-1.8.4.jar,preflight- ...
- CAD图纸转换成高质量的PDF文件,两种方法一步搞定哦
其实提到CAD,可能大家都大致的了解,CAD现在已经是建筑行业运用的非常广泛的一个设计软件了,其实CAD就是一种计算机辅助软件,可以高效的帮助我们进行图纸的绘制.我们一般在设计完图纸,可能定会转换格式 ...
- CAD图纸文件如何转换成常见的彩色PDF文件?
CAD图纸文件如何转换成常见的彩色PDF文件?将CAD图纸文件进行格式间的转换成为PDF格式也是格式转换中经常见的,编辑完成的CAD图纸文件为了将其进行方便查看会需要将其进行格式间的转换成为PDF格式 ...
- java实现多张条码转成pdf文件
目录 需求 管理后台通过查询并生成多张条码并一起打印出来 思路 思路一 思路二 代码编写 maven pom文件 方法一 步骤一:创建barcode 条码文件 步骤二:将创建的barcode 条码文件 ...
- Java实现多张图片合并成一份PDF文件
Java实现多张图片合并成一份PDF文件,一行两列展示 引入依赖包 <dependency><groupId>com.lowagie</groupId><ar ...
- java 使用itextpdf工具实现HTML转PDF文件
java 使用itextpdf工具实现HTML转PDF文件 本文转载https://my.oschina.net/960823/blog/1588166内容 demo maven依赖包 <dep ...
- Java 使用iText7生成带页码的PDF文件(同时生成目录,但是不会合并两个PDF)
一.效果图 1.带页码效果 2.目录效果 前言:Java 使用iText7生成带页码的PDF文件,同时生成目录PDF,但限于水平,暂时还在摸索合并两个PDF.不过看了一下,iText好像有生成目录的代 ...
- java实现通过url实现浏览器下载pdf文件
java实现通过url实现浏览器下载pdf文件 // 下载pdf文件工具类 public static void toDownload(HttpServletRequest request, Http ...
- java 如何导出json文件_java导出json格式文件的示例代码
本文介绍了java导出json格式文件的示例代码,分享给大家,具体如下: import java.io.File; import java.io.FileWriter; import java.io. ...
最新文章
- 【c语言】判断一个数是否为素数
- MySQL的binlog数据如何查看
- cocos2dx游戏解决方案
- python网络爬虫实战 吕文翔_实战Python网络爬虫
- 实现根据id查询房源数据的GraphQL服务
- 《Python编程初学者指南》——1.2 Python简介
- webstorm 代码提示
- 交际过程的两个基本环节_男女相处,若不介意这些“小动作”,基本可以确定“关系暧昧”...
- es6 Proxy 实例的方法
- (收藏)Android VoIP
- 【转】No JVM could be found on your system解决方法
- [雪峰磁针石博客]python 3.7极速入门教程1安装:Linux(Ubuntu 18.04)及Windows上安装Anaconda...
- 单片机实验板 c语言 打包下载,《AVR单片机开发板 实验板 C语言 视频教程 》
- js 获取mac地址和IP地址
- windows 8 下五笔98版和新世纪版的解决方案
- 《机器人动力学与控制》第九章——动力学 9.4 欧拉-拉格朗日方程法应用举例(下)
- 中控 人脸指纹 iface702 二次开发 (X86 X64 SDK,VB,C#Demo)
- ADO连接MySql数据库之通过连接字符串
- Nginx配置虚拟主机
- HDU 3236 Gift Hunting (程序猿的哄女朋友方式)