Java转换html为图片,图片嵌入pdf/word

一、html转图片

引入依赖

gradle 复制代码
implementation 'org.xhtmlrenderer:flying-saucer-core:9.1.22'

html页面转图片对于html标签格式有要求,自己调整,每个标签都要有结束标签,标签也要,标签也要, doctype需要大写

java 复制代码
try {
			String basePath = "D:\\";
			File source = new File(basePath, "report.html");
			File g2drDest = new File(basePath, "G2DR.png");
			BufferedImage g2drImage = Graphics2DRenderer.renderToImageAutoSize(source.toURI().toURL().toExternalForm(),
				1024, BufferedImage.TYPE_INT_ARGB);
			ImageIO.write(g2drImage, "png", g2drDest);
		}catch (Exception e){
			e.printStackTrace();
		}

二、图片嵌入pdf

gradle 复制代码
implementation 'com.itextpdf:itextpdf:5.5.5'
implementation 'com.itextpdf:itext-asian:5.2.0'
java 复制代码
try{
			BufferedImage originalImage = ImageIO.read(new File("D:\\G2DR.png"));
			float imgWidth = originalImage.getWidth();
			float imgHeight = originalImage.getHeight();
			float pageWidth = PageSize.A4.getWidth();
			float pageHeight = PageSize.A4.getHeight();
			float imgPerPage = pageHeight; // 每页只显示图片的一段
			Document document = new Document(PageSize.A4, 0, 0, 0, 0);
			PdfWriter.getInstance(document, new FileOutputStream("D:\\output.pdf"));
			document.open();
			int totalImages = (int) Math.ceil(imgHeight / imgPerPage);
			//图片长宽和A4大小不匹配可能会导致图片剪切嵌入观感不好看,可自己微调页面和图片大小
			for (int i = 0; i < totalImages; i++) {
				// 计算剪切起点和宽度
				int start = (int) (imgPerPage * i);
				int end = (int) Math.min(start + imgPerPage, imgHeight);
				BufferedImage subImage = originalImage.getSubimage(0, start, (int)imgWidth, end-start);
				// 将剪切后的图片转换为iText可用的Image对象
				Image image = Image.getInstance(subImage, null);
				image.scaleToFit(pageWidth, pageHeight); // 调整图片大小以适应页面
				// 插入图片到PDF文档
				document.add(new Paragraph());
				document.add(image);
				// 添加页码,如果是最后一段不需要换页
				if (i != totalImages - 1) {
					document.newPage();
				}
			}

			document.close();
		}catch (Exception e){
			e.printStackTrace();
		}

三、图片嵌入word

最简单的嵌入

复制代码
implementation 'org.apache.poi:poi:4.1.2'
implementation 'org.apache.poi:poi-ooxml:4.1.2'
java 复制代码
try{
			BufferedImage originalImage = ImageIO.read(new File("D:\\G2DR.png"));
			XWPFDocument document=new XWPFDocument();
			XWPFParagraph paragraph=document.createParagraph();
			XWPFRun run=paragraph.createRun();
			//自己设置页面参数
			run.addPicture(new FileInputStream("D:\\G2DR.png"),
				XWPFDocument.PICTURE_TYPE_PNG,
				"1.png",
				Units.toEMU(originalImage.getWidth()*(PageSize.A4.getHeight()/ originalImage.getHeight())),
				Units.toEMU(PageSize.A4.getHeight()));
			OutputStream outputStream=new FileOutputStream("D:\\output.docx");
			document.write(outputStream);
			outputStream.close();
		}catch (Exception e){
			e.printStackTrace();
		}
相关推荐
大菠萝学姐14 分钟前
基于Spring Boot和Vue的高校图书馆座位预约系统的设计与实现
java·vue.js·spring boot·后端·python·mysql·vue
信码由缰41 分钟前
Spring框架中的Component与Bean注解
java·spring
国家不保护废物1 小时前
多模态模型数据传输的秘密武器:html5对象Blob深度解析
前端·面试·html
学长学姐我该怎么办1 小时前
从零开始学前端html篇2
前端·html
程序员秘密基地1 小时前
基于html,css,vue,vscode,java,springboot,mysql数据库,在线考勤,管理系统
java·vue.js·spring·html·web app
hello 早上好2 小时前
多线程(1)
java
paopaokaka_luck2 小时前
基于Spring Boot+Vue的DIY手工社预约管理系统(Echarts图形化、腾讯地图API)
java·spring boot·后端
kk_stoper2 小时前
使用Ruby接入实时行情API教程
java·开发语言·javascript·数据结构·后端·python·ruby
我会冲击波3 小时前
告别flag与status:如何为你的布尔值(boolean)变量优雅命名?
java·后端
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ3 小时前
如何将一个本地的jar包安装到 Maven 仓库中
java·maven·jar