Java 通过freemarker 转 html, 然后html转图片

记录笔记---------------------------------------------
Java html 转 图片
1、通过freemarker 转 html
2、html转图片

freemarker模板:test.flt

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>sdsd</title>
</head>
<body>
	<table width="300" border="1" cellspacing="0">
	  <tr>
		<td colspan="3">姓名:${username}</td>
	  </tr>
	  <tr> 
        <td>一班</td> 
        <td>2012-5-10</td> 
        <td>标题1</td> 
    </tr> 
	</table>
</body>
</html>

代码如下:

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import javafx.embed.swing.JFXPanel;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class FreeMakerUtils {

    public static void main(String[] args) throws IOException {

        freeMakerToHtml("C:\\Users\\lili\\Desktop\\IO","test.flt","C:\\Users\\lili\\Desktop\\IO\\ooo.html");
        String html = getHtmlContent("C:\\Users\\lili\\Desktop\\IO\\ooo.html");
        htmlToImage(html);
    }


    public static void freeMakerToHtml(String templatePath, String templateName, String outPath){

        //1创建freeMarker配置实例
        Configuration configuration = new Configuration();
        Writer out  = null;
        try {
            //获取模版路径
            configuration.setDirectoryForTemplateLoading(new File((templatePath)));
            //加载模版文件
            Template template =  configuration.getTemplate(templateName);
            //创建数据模型
            Map dataMap = new HashMap();
            dataMap.put("username","你哈");
            // 生成数据
            File docFile = new File(outPath);
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docFile)));
            //输出文件
            template.process(dataMap,out);
            System.out.println("freemaker生成html成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取html的内容
     * @param filePath
     * @return
     */
    public static String getHtmlContent(String filePath){
        File file = new File(filePath);
        String html = "";
        try {
            FileInputStream in = new FileInputStream(file);
            Reader reader = new InputStreamReader(in);
            BufferedReader htmlReader = new BufferedReader(reader);
            String line;
            while ((line = htmlReader.readLine())  != null) {
                html = html + line + "\n";
            }
            htmlReader.close();
            reader.close();
            in.close();
            return html;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return html;
    }

    /**
     * html转图片
     * @param html
     * @throws IOException
     */
    public static void htmlToImage(String html) throws IOException {
        JEditorPane ed = new JEditorPane();
        ed.setSize(700,800);
        ed.setBackground(Color.white);
        ed.setEditable(false);
        ed.setContentType("text/html");
        ed.setText(html);
        BufferedImage image = new BufferedImage(ed.getWidth(), ed.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        ed.paint(g);
        g.dispose();
        ImageIO.write(image,"jpg",new File("C:\\Users\\lili\\Desktop\\IO\\ooo.jpg"));
    }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

燕長亭

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值