Java obtains web page content, generates files, and generates images through url (html2image method)

Reference article
[1] java converts html tags java converts html pages into images
https://blog.51cto.com/u_13360/6326010
[2] Java - obtains web page content and generates HTML files locally
https://blog. 51cto.com/u_12660945/5163153
[3] Java accesses the network url and obtains the html code of the web page
https://www.cnblogs.com/weilunhui/p/3854249.html
[4] Restriction premise
https://blog.csdn. net/qq_43376347/article/details/127784268

Prerequisite ( only applicable to ordinary pure HTML screenshots, css and js will be missed and lost )

When using html2image to convert html to images, the problem of css style loss will occur.
1. All css codes must be written in inline style on each tag.
2. Do not use too new css syntax
. 3. Do not use js to design the page. Rendering
If you have tried all the above and it still doesn’t work, set the program to wait for a period of time
Thread.sleep(6000);

required dependencies

                <dependency>
                    <groupId>gui.ava</groupId>
                    <artifactId>html2image</artifactId>
                    <version>2.0.1</version>
                </dependency>

1. Convert local files to images

//将本地的文本读取到String中
String html = FileUtil.readString("D:\\test\\1\\20211009103530.html", StandardCharsets.UTF_8);

//创建html2Image对象
Html2Image html2Image = Html2Image.fromHtml(html);
//生成image渲染器
ImageRenderer imageRenderer = html2Image.getImageRenderer()
        .setImageType("png")
        .setWidth(3000)
        .setHeight(2100);
//生成图片
imageRenderer.saveImage("D:\\test\\hello-png-5.png");

2. Convert remote URL files into images

/**
     * 直接将访问网址转成图片保存本地
     *
     * @param hzdtpwzPath 文件夹位置
     * @param ycurl       访问的地址
     * @throws Exception
     * @return 图片完整位置
     */
    public String wstest(String hzdtpwzPath, String ycurl) throws Exception {
    
    
        String html = "";
        URL url = new URL(ycurl);
        InputStream in = url.openStream();
        InputStreamReader isr = new InputStreamReader(in);
        BufferedReader bufr = new BufferedReader(isr);
        String str;
        while ((str = bufr.readLine()) != null) {
    
    
            html += str;
        }
        bufr.close();
        isr.close();
        in.close();

        System.out.println("===========");
        System.out.println(html);
        //创建html2Image对象
        Html2Image html2Image = Html2Image.fromHtml(html);
        //生成image渲染器
        ImageRenderer imageRenderer = html2Image.getImageRenderer()
                .setImageType("png")
                .setWidth(3000)
                .setHeight(2100);
        //图片的名称及后缀
        String tpwz = hzdtpwzPath +  ".png";
        //生成图片
        imageRenderer.saveImage(tpwz);
        return tpwz;
    }

If you need to bring cookies, you can do it in the following ways

/**
     * 直接将访问网址转成图片保存本地 (带cookie)
     *
     * @param hzdtpwzPath 文件夹位置
     * @param ycurl       访问的地址
     * @throws Exception
     * @return 图片完整位置
     */
    public String wstest(String hzdtpwzPath, String ycurl) throws Exception {
    
    
        URL url = new URL(ycurl);
        //加cooker
        URLConnection conn = url.openConnection();
        conn.setRequestProperty("cookie", "token=" + nhhzdtoken);
        conn.setDoOutput(true);
        InputStream inS = conn.getInputStream();
        InputStreamReader inR = new InputStreamReader(inS);
        BufferedReader br = new BufferedReader(inR);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
    
    
            sb.append(line);
        }
        br.close();
        inR.close();
        inS.close();

        //将本地的文本读取到String中
        //String html = FileUtil.readString("C:\\Users\\HONOR\\Desktop\\受理回执单.html", StandardCharsets.UTF_8);
        String html = sb.toString();
        //创建html2Image对象
        Html2Image html2Image = Html2Image.fromHtml(html);
        //Html2Image html2Image = Html2Image.fromURI(new URI(ycurl));
        //生成image渲染器
        ImageRenderer imageRenderer = html2Image.getImageRenderer()
                .setImageType("png")
                .setWidth(3000)
                .setHeight(2100);
        //图片的名称
        String tpwz = hzdtpwzPath + ".png";
        //生成图片
        imageRenderer.saveImage(tpwz);
        return tpwz;
    }

3. Download the remote URL file locally and convert it into an image

/**
     * 将访问的地址生成html文件且存本地
     *
     * @param hzdtpwzPath 文件夹位置
     * @param ycurl       访问的地址
     * @return 具体的html文件位置
     * @throws IOException
     */
    public String saveHtml(String hzdtpwzPath, String ycurl) throws IOException {
    
    
        String htmlFilepath = hzdtpwzPath +  ".html";
        System.out.println("html文件位置:" + htmlFilepath);
        File dest = new File(htmlFilepath);
        InputStream is;//接收字节输入流
        FileOutputStream fos = new FileOutputStream(dest);//字节输出流

        URL wangyi = new URL(ycurl);
        is = wangyi.openStream();

        BufferedInputStream bis = new BufferedInputStream(is);//为字节输入流加缓冲
        BufferedOutputStream bos = new BufferedOutputStream(fos);//为字节输出流加缓冲

        int length;

        byte[] bytes = new byte[1024 * 20];
        while ((length = bis.read(bytes, 0, bytes.length)) != -1) {
    
    
            fos.write(bytes, 0, length);
        }

        bos.close();
        fos.close();
        bis.close();
        is.close();
        return htmlFilepath;
    }

/**
     * 将访问网址转成本地html文件再转为图片保存本地
     *
     * @param hzdtpwzPath 文件夹位置
     * @param ycurl       远程地址
     * @throws Exception
     */
    public String wstest2(String hzdtpwzPath, String ycurl) throws Exception {
    
    
        String s = saveHtml(hzdtpwzPath, ycurl);
        //将本地的文本读取到String中
        String html = FileUtil.readString(s, StandardCharsets.UTF_8);
        //创建html2Image对象
        Html2Image html2Image = Html2Image.fromHtml(html);
        //生成image渲染器
        ImageRenderer imageRenderer = html2Image.getImageRenderer()
                .setImageType("png")
                .setWidth(3000)
                .setHeight(2100);
        //图片的名称及后缀
        String tpwz = hzdtpwzPath + ".png";
        //生成图片
        imageRenderer.saveImage(tpwz);
        return tpwz;
    }

Guess you like

Origin blog.csdn.net/qq_20236937/article/details/132043924