zoukankan      html  css  js  c++  java
  • 将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;980px;padding: 32px 40px;overflow: hidden;margin:0 auto;">" +
                            "<div style="box-sizing:border-box;text-align: right;100%;height:16px;font-size: 16px;line-height: 16px;">住院</div>" +
                            "<div style="box-sizing:border-box;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;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="100%;border-spacing:0px ; ">" +
                            "<tr style="100%;height:16px;" border="0">" +
                            "<td style="25%;">姓名:张三丰</td>" +
                            "<td style="25%;">性别:男</td>" +
                            "<td style="25%;">年龄:4岁</td>" +
                            "<td style="25%;">ID号:  0009090990</td>" +
                            "</tr>" +
                            "<tr style="100%;height:16px;" border="0">" +
                            "<td style="">科室:内分泌</td>" +
                            "<td style="25%;">HIS号:987922</td>" +
                            "<td style="25%;">检查类别:MR</td>" +
                            "<td style="25%;">检验号:201209090909</td>" +
                            "</tr>" +
                            "</table>" +
                            "</div>" +
                            "<div style="min-height:30px;clear:both;overflow:hidden;margin-top:8px;">" +
                            "<table border="0" style="100%;border-spacing:0px ; ">" +
                            "<tr style="100%;height:16px;" border="0">" +
                            "<td style="90px;font-size: 16px;line-height:24px;color:#000;font-weight:700;">检查部位:</td>" +
                            "<td style="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="100%;border-spacing:0px ; ">" +
                            "<tr style="100%;" border="0" valign="top">" +
                            "<td style="90px;font-size: 16px;line-height:24px;color:#000;font-weight:700;">影像表现:</td>" +
                            "<td style="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="100%;border-spacing:0px ; ">" +
                            "<tr style="100%;" border="0" valign="top">" +
                            "<td style="90px;font-size: 16px;line-height:24px;color:#000;font-weight:700;">印象:</td>" +
                            "<td style="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="100%;border-spacing:0px ; ">" +
                            "<tr style="100%;height:24x;" border="0">" +
                            "<td style="36%;">报告日期:2017-11-22 11:12:09</td>" +
                            "<td style="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();
            }
    

      

      

      

  • 相关阅读:
    python异常处理与上下文管理器
    SpringBoot-MongoDB 索引冲突分析及解决
    补习系列(13)-springboot redis 与发布订阅
    带着canvas去流浪系列之八 碰撞
    带着canvas去流浪系列之九 粒子动画
    通过哪吒动漫豆瓣影评,带你分析python爬虫与BeautifulSoup快速入门
    Python小数据保存,有多少中分类?不妨看看他们的类比与推荐方案...
    免费试用 | 多模 NoSQL 服务GeminiDB for Cassandra 全球首发
    Vue+ElementUI项目使用webpack输出MPA
    shell的shift用法
  • 原文地址:https://www.cnblogs.com/lambertwe/p/10538488.html
Copyright © 2011-2022 走看看