java 图片base64 转 pdf_高大王竟然被注册的博客-程序员秘密

技术标签: java  base64  itext  

思路:base64 -> multipartFile - > pdf

base64 -> multipartFile

/**
 *   将 图片base64 - > MultipartFile
 * @param base64
 * @return
 */
public static MultipartFile base64MultipartFile(String base64) {
    
    try {
    
        String[] baseStr = base64.split(",");
        BASE64Decoder base64Decoder = new BASE64Decoder();
        byte[] b;
        b = base64Decoder.decodeBuffer(baseStr[1]);
        for (int i = 0; i < b.length; ++i) {
    
            if (b[i] < 0) {
    
                b[i] += 256;
            }
        }
        return new BASE64DecodedMultipartFile(b, baseStr[0]);
    } catch (Exception e) {
    
        e.printStackTrace();
        return null;
    }
}

multipartFile - > pdf

/**
 * 将图片转换为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;
    if (fileName != null) {
    
        pdfFileName = fileName.substring(0, fileName.lastIndexOf(".")) + ".pdf";
    } else {
    
        return null;
    }
    Document doc = new Document(PageSize.A4, 100, 100, 20, 30);
    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();
    return new File(pdfFileName);
}



	/**
	 * 等比压缩,获取压缩百分比
	 * @param height 图片的高度
	 * @param weight 图片的宽度
	 * @return 压缩百分比
	 */
	private static int getPercent(float height, float weight) {
    
		float percent;
		if (height > weight) {
    
			percent = PageSize.A4.getHeight() / height * 100;
		} else {
    
			percent = PageSize.A4.getWidth() / weight * 100;
		}
		// -5 多缩放一点 不影响内容
		percent -= 5;
		return Math.round(percent);
	}

如果还需要转成multipartFile方便调用api上传的话

pdf file - > multipartFile

FileInputStream input = new FileInputStream(file);

MultipartFile multipartFilePDF = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(input));

pom 依赖 itextpdf

   <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.5.10</version>
    </dependency>
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42404521/article/details/109600684

智能推荐

基于Cocos2dx + box2d 实现的愤慨的小鸟Demo_weixin_33708432的博客-程序员秘密

1. Demo初始界面2. 游戏界面3. 精确碰撞检測4. 下载 压缩文件文件夹AngryBird source    愤慨的小鸟Demo源码,基于Cocos2dx C++,以及box2d技术。run 可运行程序文件夹点击打开链接 ...

Linux下安装ffmpeg详解版:_ffmpeg有linux版本的吗_梆子大鼓的博客-程序员秘密

1:切换到你想要安装的目录:cd developer2:得到一个安装包,选择自己想要的版本:wget https://ffmpeg.org/releases/ffmpeg-4.1.tar.bz23:解压 tar -xjvf ffmpeg-4.1.tar.bz24:切换到解压好的目录中 cd ffmpeg-4.1/5:先安装 yasm :yum install yasm6:安装完毕以后...

【IOS】mac终端执行.sh文件总是提示permission denied_chqj_163的博客-程序员秘密

假设我文件夹jni有一个list.sh文件我直接 nxgametekiMacBook-Air:jni luonan$  ./list.sh ../../Classes提示 permission denied然后chmod +x  *.sh然后运行就OK了nxgametekiMacBook-Air:jni luonan$ chmod +x *.sh ./list.sh ../...

Python标准库 --- 内置模块汇总(字母排序)_python有字母库吗_TechnologyGuy的博客-程序员秘密

Standard Module List点击Module名字,可导航至python官方的Python Document相关模块说明。若是英文阅读吃力,可在参考本文件夹下相关模块内容后,再行阅读(可能也没必要阅读了)。以下列表,内置模块总计205个。嗯,所以当你用到的时候再来查阅就好了,单独全面学习的话,我也没说不好。。。___future__Future statement definitions__main__The environment where the

转载:LoadRunner11-遇到问题及解决办法_weixin_30872789的博客-程序员秘密

转自:http://4951507.blog.51cto.com/4941507/11087331、LoadRunner超时错误:在录制Web服务器端,如果超过120秒服务器协议脚本回放时超时情况经常出现,产生错误的原因也有很多,解决的方法也不同。错误现象1:Action.c(16): Error -27728: Step download timeout (120 seconds) has ...

基于STM32的角度跟随云台_stm32云台相机_不用加油也没有关系哦的博客-程序员秘密

硬件:STM32F103SG90MPU6050OLED具体程序和云台效果:https://www.bilibili.com/video/BV1S7411m7eu/

随便推点

java拍照搜题软件下载_微信拍照搜答案的小程序 可以拍照搜java题的软件_布瓦吉吉的博客-程序员秘密

有什么适合大学生搜题的一些APP,比如解决高数,线代,数学建模,大学物理这样的学科问题的APP?您可以使用quark或百度直接搜索对于高级数字,您可以使用“Microsoft math”或photomath,但这需要一点高扫描角度;您也可以尝试wellframe Alpha家庭作业帮助,小猿猴搜索的题目可以在一些题目里找到微信小程序也可以试试课本上的题目可以试试“帮班宝”、“学小容易”APP有一些...

Git:代码冲突常见解决方法_yjl2055的博客-程序员秘密

如果系统中有一些配置文件在服务器上做了配置修改,然后后续开发又新添加一些配置项的时候,在发布这个配置文件的时候,会发生代码冲突:error: Your local changes to the following files would be overwritten by merge:protected/config/main.phpPlease, commit your changes...

asp.net2.0连接mysql数据库_回首笑浮生的博客-程序员秘密

1,确认安装mysql(本人用的5.1版本)2,下载mysql-connector-net-5.2.3并安装,地址:http://dev.mysql.com/downloads/connector/net/5.2.html  里面集成了MySQL Visual Studio Plugin,而mysql-connector-net-5.0不集成.3,到这步为止,已经可以用vs菜单里"工具

audio读取文件流播放音频_audio文件流_菜鸟进军大神陆的博客-程序员秘密

jsp阶段:&amp;lt;audio controls=&quot;controls&quot; controlsList=&quot;nodownload&quot;&amp;gt;&amp;lt;source src=&quot;${ctx }/phone/getAudio.ce?type=3&amp;amp;PATHID='+mp+'&quot; type=&quot;audio/mpeg&quot;&amp;gt;&amp;lt;/audio&amp;gt;效果图:java部分:@Request

支付漏洞总结 / 在线支付流程安全分析_我是如何1元再购特斯拉的是什么漏洞_Omni-Space的博客-程序员秘密

前言大家对支付漏洞的理解通常都是篡改价格,已有的对支付漏洞的总结也是对现有的一些案例的经验式归类,没有上升到对在线支付流程深入分析的一个层面。这里尝试从分析在线支付流程,在线支付厂商的接入方式开始,深入业务分析整个在线交易流程中容易出现的安全问题。支付宝/在线支付流程支付宝即时到账接口开发流程在线支付从功能上来说是通过支付宝的支付渠道,付款者直接汇款给另一个拥有支付宝账号的收款者。

推荐文章

热门文章

相关标签