html 转 jsoup document 转 w3c document 转 png

1 篇文章 0 订阅

用到的maven 包

这里面最大的问题就是如果是内部图片有可能不显示,还有就中文不能换行..

dependency>
            <groupId>net.sf.cssbox</groupId>
            <artifactId>cssbox</artifactId>
            <version>4.14</version>
        </dependency>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.8.3</version>
        </dependency>

程序代码

package cn.com.eduedu.exam.util;

import java.awt.Dimension;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.fit.cssbox.css.CSSNorm;
import org.fit.cssbox.css.DOMAnalyzer;
import org.fit.cssbox.layout.BrowserCanvas;
import org.jsoup.Jsoup;
import org.jsoup.helper.W3CDom;

public class HtmlToImage {
    public static void main(String[] args) throws IOException  {
        FileOutputStream out = new FileOutputStream(new File("D:\\html.png"));
        render.renderURL(url, out, ImageRenderer.Type.PNG);
        render.
        System.out.println("OK");*/
        String html="<html><body><p>这是第二个标记</p>"
                +"<img id=\"s_lg_img\" src=\"http://www.baidu.com/img/bd_logo1.png?where=super\" width=\"470\" height=\"129\" usemap=\"#mp\">"
                + "</body><html>";
        org.jsoup.nodes.Document document = Jsoup.parse(html);
        org.jsoup.helper.W3CDom w3cDom = new W3CDom();
        org.w3c.dom.Document w3cDoc = w3cDom.fromJsoup(document);
        DOMAnalyzer da = new DOMAnalyzer(w3cDoc,null);
        //da.setMediaSpec(media);
        //da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
        da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet
        //da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet
        //da.addStyleSheet(null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT); //render form fields using css
        //da.getStyleSheets(); //load the author style sheets

        BrowserCanvas contentCanvas = new BrowserCanvas(w3cDoc.getDocumentElement(), da, null);
        contentCanvas.setAutoMediaUpdate(false); //we have a correct media specification, do not update
        contentCanvas.getConfig().setClipViewport(false);
        contentCanvas.getConfig().setLoadImages(true);
        contentCanvas.getConfig().setLoadBackgroundImages(false);
        contentCanvas.createLayout(new Dimension(1200, 600));
        OutputStream out=new FileOutputStream("d:\\html111.png");
        ImageIO.write(contentCanvas.getImage(), "png", out);


    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值