将html转换成image图片png格式
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream; import javax.imageio.ImageIO;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicEditorPaneUI; /**
* 将html转换成Image图片
*
*
*
*/
public class HtmlToImageUtils { /**
* 画页面的方法
*
* @param g 画笔
* @param panel 画板
* @return
*/
public static void paintPage(Graphics g, JTextPane panel) {
Graphics2D g2 = (Graphics2D) g;
g2.translate(0f, 0f);
panel.paint(g2);
} /**
* html转换为png文件
*
* @param bgColor 图片的背景色
* @param html html的文本信息
* @param width 显示图片的text容器的宽度
* @param height 显示图片的text容器的高度
* @param eb 設置容器的边框
* @return
* @throws Exception
*/
public static byte[] html2png(Color bgColor, String html, EmptyBorder eb) throws Exception {
JTextPane tp = new JTextPane();
if (eb == null) {
eb = new EmptyBorder(0, 50, 0, 50);
}
if (bgColor != null) {
tp.setBackground(bgColor);
}
tp.setBorder(eb);
tp.setContentType("text/html");
tp.setText(html);
byte[] bytes = null;
Dimension d = ((BasicEditorPaneUI) tp.getUI()).getPreferredSize(tp);
int height = d.height + 20;
int width = d.width + 15;
tp.setSize(width, height);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setClip(0, 0, width, height);
paintPage(g, tp);
g.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
baos.flush();
bytes = baos.toByteArray();
baos.close();
return bytes;
}
调用方法:
byte[] bytes = HtmlToImageUtils.html2png(Color.white, html, new EmptyBorder(0, 0, 0, 0));
html样例
String htmlstr =
""+"<body style=\"background:#f4f4f4;margin:0;padding:0;font-size:14px;color:#333;line-height: 1;box-sizing:border-box;\">" +
"<div style=\"box-sizing:border-box;width:980px;padding: 32px 40px;overflow: hidden;margin:0 auto;\">" +
"<div style=\"box-sizing:border-box;text-align: right;width:100%;height:16px;font-size: 16px;line-height: 16px;\">住院</div>" +
"<div style=\"box-sizing:border-box;width:100%;height:60px;line-height: 1;overflow:hidden;text-align: center;\">" +
"<div style=\"box-sizing:border-box;display: inline-block;margin:0 auto;\">" +
"<div style=\"box-sizing:border-box;display:block;font-size: 24px;\">首都医科大学附属北京儿童医院</div>" +
"<div style=\"box-sizing:border-box;display:block;font-size: 20px;margin-top:16px;\">影像检查诊断报告单</div>" +
"</div>" +
"</div>" +
"<div style=\"box-sizing:border-box;width:100%;border:solid 2px #000;border-right:none;border-left:none; line-height:28px;height:62px;margin-top:10px;padding:2px 8px;\">" +
"<table border=\"0\" style=\"width:100%;border-spacing:0px ; \">" +
"<tr style=\"width:100%;height:16px;\" border=\"0\">" +
"<td style=\"width:25%;\">姓名:张三丰</td>" +
"<td style=\"width:25%;\">性别:男</td>" +
"<td style=\"width:25%;\">年龄:4岁</td>" +
"<td style=\"width:25%;\">ID号: 0009090990</td>" +
"</tr>" +
"<tr style=\"width:100%;height:16px;\" border=\"0\">" +
"<td style=\"\">科室:内分泌</td>" +
"<td style=\"width:25%;\">HIS号:987922</td>" +
"<td style=\"width:25%;\">检查类别:MR</td>" +
"<td style=\"width:25%;\">检验号:201209090909</td>" +
"</tr>" +
"</table>" +
"</div>" +
"<div style=\"min-height:30px;clear:both;overflow:hidden;margin-top:8px;\">" +
"<table border=\"0\" style=\"width:100%;border-spacing:0px ; \">" +
"<tr style=\"width:100%;height:16px;\" border=\"0\">" +
"<td style=\"width:90px;font-size: 16px;line-height:24px;color:#000;font-weight:700;\">检查部位:</td>" +
"<td style=\"width:810px;padding-left:8px;line-height:20px;padding-top:2px;\">小腿哦UI电话小腿小腿哦U话小腿哦UI电话</td>" +
"</tr>" +
"</table>" +
"</div>" +
"<div style=\"min-height:100px;clear:both;overflow:hidden;margin-top:20px;\">" +
"<table border=\"0\" style=\"width:100%;border-spacing:0px ; \">" +
"<tr style=\"width:100%;\" border=\"0\" valign=\"top\">" +
"<td style=\"width:90px;font-size: 16px;line-height:24px;color:#000;font-weight:700;\">影像表现:</td>" +
"<td style=\"width:810px;padding-left:8px;line-height:20px;padding-top:2px;\">小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UIUI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UIUI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话</td>" +
"</tr>" +
"</table>" +
"</div>" +
"<div style=\"min-height:100px;clear:both;overflow:hidden;margin-top:30px;\">" +
"<table border=\"0\" style=\"width:100%;border-spacing:0px ; \">" +
"<tr style=\"width:100%;\" border=\"0\" valign=\"top\">" +
"<td style=\"width:90px;font-size: 16px;line-height:24px;color:#000;font-weight:700;\">印象:</td>" +
"<td style=\"width:810px;padding-left:8px;line-height:20px;padding-top:2px;\">小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UIUI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UIUI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话小腿哦UI电话</td>" +
"</tr>" +
"</table>" +
"</div>" +
"<div style=\"margin-top:20px;height:24px;line-height:24px;\">" +
"<table border=\"0\" style=\"width:100%;border-spacing:0px ; \">" +
"<tr style=\"width:100%;height:24x;\" border=\"0\">" +
"<td style=\"width:36%;\">报告日期:2017-11-22 11:12:09</td>" +
"<td style=\"width:34%;\">报告医生:唐小小</td>" +
"<td style=\"\">审核医生:李若曦</td>" +
"</tr>" +
"</table>" +
"</div>" +
"<div style=\"border-top:solid 2px #000;margin-top:6px;font-size:10px;font-weight:600;line-height:26px;padding-left:10px;\"> " +
"注:本报告仅供临床医师参考,影像科医师签字 后生效" +
"</div>" +
"" +
"</div>" +
"</body>"
;
try {
// FileUtil.readAscFile(
byte[] bytes = HtmlToImageUtils.html2jpeg(Color.white, htmlstr,
1300, 1200, new EmptyBorder(0, 0,
0, 0));
//写入阿里云
InputStream instreams = new ByteArrayInputStream(bytes);
String resUrl= ossService.uploadObject(instreams, "mrUpload/",
UUID.randomUUID().toString(), com.hecross.common.util.FileUtils.getFileExt(".jpg"));
System.out.println(resUrl);
res.put("resUrl",resUrl);
} catch (Exception e) {
e.printStackTrace();
}
将html转换成image图片png格式的更多相关文章
- Excel将秒转换成标准的时间格式HH:MM:SS
Excel将秒转换成标准的时间格式HH:MM:SS 比如120秒,转换成00:02:00 Excel公式为: =TEXT(A1/86400,"[hh]:mm:ss") A1为秒数据 ...
- qrcode.js插件将你的内容转换成二维码格式
---qrcode.js插件将你的内容转换成二维码格式--- 我之前一直想知道二维码是怎么生成,所以就了解了一下, 最后还是不知道它的原理, 但是,我知道怎么生成. 现在就让我带你制作一个你喜爱的二维 ...
- Ghostscript 将PDF文件转换成PNG图片 问题一二
由于项目需求,需要将原来的PDF文档转换成图片文件,在网上找了一些PDF转图片的方法:测试了几个后,都有这样或那样的问题 1.PDFLibNet.dll,这个类型最初还是挺好用的,能转图片和HTML, ...
- 将psd格式批量转换成jpg或png格式(C#自制软件)
此项目基于.net framework 4.0 将psd格式批量转换成jpg或png格式. 链接:https://pan.baidu.com/s/16IEjX0sHaY9H3Ah7mv6IPQ 提取码 ...
- Openssl生成RSA公私钥以及将公钥转换成C#支持的格式
Openssl生成RSA公私钥以及将公钥转换成C#支持的格式 1.RSA算法介绍 RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密.RSA ...
- 【Spring Boot】Spring Boot之利用Logstash将日志转换成以JSON的格式存储和输出
一.Logstash的作用 Logstash是一个完全开源的工具,它可以对日志进行收集.过滤,能非常方便地将日志转换成以JSON的格式存储和输出,并将其存储供以后使用. 二.整合Logstash的步骤 ...
- JS框架_(Qrcode.js)将你的内容转换成二维码格式
百度云盘 传送门 密码:304e 输入网址点击按钮生成二维码,默认为我的博客首页 二维码格式演示 <!DOCTYPE html> <html lang="en"& ...
- C#使用FFmpeg 将视频格式转换成Gif图片示例
根据EFmpeg封装的视频转换gif工具:https://my.oschina.net/tianma3798/blog/825317 一.本次使用参数说明 /* * 参数说明: * -i 源文件位置 ...
- mpp文件转换成jpg图片,可以用pdf文件做中转站
用project软件做了一个表,发现不能转换成图片,先把mpp文件转换成pdf文件,然后用PS打开pdf文件,存储为jpg格式就行了
随机推荐
- ElasticSearch Document API
删除索引库 可以看到id为1的索引库不见了 这里要修改下配置文件 slave1,slave2也做同样的操作,在这里就不多赘述了. 这个时候记得要重启elasticseach才能生效,怎么重启这里就不多 ...
- Python之函数——内置函数
内置函数(Built-in Functions) 截止到3.6版本,python一共为我们提供了68个内置函数.它们就是python提供给的可以直接拿来使用的所有函数,接下来让我们一起认识一下这些函数 ...
- centos7.3下curl支持https协议
1 由于自己的curl是默认安装的,查看了下 不支持https协议 [root@izwz90bp6do7s3cr45cw6az ~]# curl --version curl (x86_64-redh ...
- centos73下php5.6安装GD库
yum --enablerepo=remi-php56 install php-gd php-mysql php-mbstring php-xml php-mcrypt YUM安装的 找到了源 分分 ...
- OpenCL 设备队列
▶ 按书上写的设备队列的代码,需要 OpenCL2.0 的平台和设备,先把代码堆上来 ● 程序主要功能:用主机上的数组 Ahost 和 Bhost 创建设备缓冲区 Adevice 和 Bdevice, ...
- input文本框 放上图片img 通过padding relative和absolute 的实现
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- python远程调试及celery调试
部分来自 from: https://www.xncoding.com/2016/05/26/python/pycharm-remote.html 你是否经常要在Windows 7或MAC OS X上 ...
- eclipse上一次没有正确关闭,导致启动的时候卡死错误解决方法
关于 eclipse启动卡死的问题(eclipse上一次没有正确关闭,导致启动的时候卡死错误解决方法),自己常用的解决方法: 方案一(推荐使用,如果没有这个文件,就使用方案二): 到<works ...
- storm集群相关资料
1. Storm集群组件 Storm集群中包含两类节点:主控节点(Master Node)和工作节点(Work Node).其分别对应的角色如下: 主控节点(Master Node)上运行一个被称为N ...
- OpenCv 人脸识别 基础
#include <opencv2\opencv.hpp> #include <iostream> using namespace std; int main() { // 摄 ...