html代码 转换成图片(偶尔会生成空白)
程序员文章站
2022-03-02 12:48:30
...
package com.by;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class CreatPicByHtml {
public static void main(String[] args) {
String html = getHtml();
OutputStream fos;
try{
String htmlPath = "D:\\tiantianbaiying\\by_timer\\src\\test\\com\\by\\ysd.html";
String pngPath = "D:\\tiantianbaiying\\by_timer\\src\\test\\com\\by\\ysd.png";
fos = new FileOutputStream(htmlPath);
fos.write(html.getBytes("UTF-8"));
fos.flush();
fos.close();
System.out.println("成功");
createPic(htmlPath,pngPath);
}catch (Exception e){
e.printStackTrace();
System.out.println("失败");
}finally {
}
// createPic();
}
public static String getHtml(){
//编写html
StringBuffer html = new StringBuffer();
html.append("<html>\n" +
"<head></head>" +
"\t<body style='width:500px;margin:0px 0px;font-size:16px;font-family:Microsoft YaHei,宋体'>\n" +
"\t\t<div id='title' style='background-color:#ffE400;height:100px;font-size:26px;text-align:center;padding-top:38px' >工程部验收单</div>\n" +
"\t\t<div id='information' style='padding-left:18px'>\n" +
"\t\t\t<br/>\n" +
"\t\t\t<span>订单编号:BN2018070309253872604</span><br/><br/>\n" +
"\t\t\t<span>店铺ID:2454</span><br/><br/>\n" +
"\t\t\t<span>店铺名称:宁波印象城Ningbo In City</span><br/><br/>\n" +
"\t\t\t<span>施工地址:上海市闵行区莘庄公路6088号龙之梦商务楼2楼</span><br/><br/>\n" +
"\t\t\t<span>报修内容:</span><br/><br/>\n" +
"\t\t\t<div id='content' style='background-color:#E7E7E7;padding:10px 16px 30px 16px;border-radius:1px;width:420px;font-size:14px;'>\n" +
"\t\t\t\t立体发光字整体不亮,报修数量10个,需紧急维修\n" +
"\t\t\t</div>\n" +
"\t\t\t<br/>\n" +
"\t\t</div>\n" +
"\t\t<div style='height:14px;background-color:#f7f7f7'></div>\n" +
"\t\t<div style='padding:16px 0px 16px 18px' id='worker'>\n" +
"\t\t\t上门师傅姓名:张梓晨\n" +
"\t\t</div>\n" +
"\t <div style='height:14px;background-color:#f7f7f7'></div>\n" +
"\t <div id='ys' style='height:180px;padding:16px 18px 16px 18px'> \n" +
"\t\t\t<div>店铺验收结果:</div>\n" +
"\t\t\t<div style='height:70px'></div>\n" +
"\t\t\t<div style='text-align:right'>3454 屈臣氏浦东店</div>\n" +
"\t\t</div>\n" +
"\t</body>\n" +
"</html>");
System.out.println(html);
return html.toString();
}
public static void createPic(String htmlPath,String pngPath){
//转换成图片
try {
// JEditorPane ed = new JEditorPane("file:///" + "C:\\Users\\Administrator\\Desktop\\ysd.html");
JEditorPane ed = new JEditorPane("file:///" + htmlPath);
ed.setSize(655,820);
BufferedImage image = new BufferedImage(ed.getWidth(), ed.getHeight(),
BufferedImage.TYPE_INT_ARGB);
//paint the editor onto the image
SwingUtilities.paintComponent(image.createGraphics(),
ed,
new JPanel(),
0, 0, image.getWidth(), image.getHeight());
//save the image to file
// ImageIO.write((RenderedImage)image, "png", new File("C:\\Users\\Administrator\\Desktop\\html1.png"));
ImageIO.write((RenderedImage)image, "png", new File(pngPath));
System.out.println("完成了啊?");
}catch (Exception e){
e.printStackTrace();
System.out.println("报错了?");
}
}
}
上一篇: 裁剪图片并上传