网站导出pdf

pom.xml

        <!-- https://mvnrepository.com/artifact/net.sourceforge.htmlunit/htmlunit -->
        <dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
            <version>2.42.0</version>
        </dependency>
        <dependency>
            <groupId>e-iceblue</groupId>
             <artifactId>spire.pdf.free</artifactId>
            <version>5.1.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.15.3</version>
        </dependency>

demo 代码测试

package com.hj.cognition.screening;

import java.io.IOException;
import java.net.MalformedURLException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.htmlconverter.qt.HtmlConverter;
import com.spire.pdf.htmlconverter.qt.Size;

import cn.hutool.core.io.file.FileWriter;
import cn.hutool.http.HtmlUtil;

public class Test8 {
    public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException {
            String url="http://127.0.0.1:9000/print.html?sessionId=[1b60cf41-1ca2-495b-8aff-039703ce1672]&orderid=[1590604433818165249]&type=pdf";
           //String url="http://127.0.0.1:9000/print.html?"+java.net.URLEncoder.encode("type=pdf&sessionId=[1b60cf41-1ca2-495b-8aff-039703ce1672]&orderid=[1590604433818165249]");

           //String url="https://rzcs.yj-health.com/pre/web/print.html?"+java.net.URLEncoder.encode("sessionId=[2d98cdae-4d7d-4a50-9539-ac86a106c554]&orderid=[1599690211275218945]","UTF-8");
             url="http://192.168.10.108:9001/web/print.html?sessionId=[2d98cdae-4d7d-4a50-9539-ac86a106c554]&orderid=[1599690211275218945]&type=pdf"; 

             WebClient webClient= new WebClient(BrowserVersion.CHROME);//设置浏览器
             webClient.getOptions().setCssEnabled(true);//设置css是否生效
             webClient.getOptions().setJavaScriptEnabled(true);//设置js是否生效
             webClient.getOptions().setTimeout(180000);
             webClient.waitForBackgroundJavaScript(180000);
             webClient.setJavaScriptTimeout(180000);
             webClient.getCache().clear();
             webClient.getCookieManager().clearCookies();
             HtmlPage htmlPage=webClient.getPage(url);//访问路径设置
             Thread.sleep(10000);
             String pageXml=htmlPage.asXml();
             System.out.println(htmlPage.asXml());
             String result = HtmlUtil.removeHtmlTag(pageXml, "script");
             result=result.replaceAll("href=\"css", "href=\"http://192.168.10.108:9001/web/css").replaceAll("href=\"./css", "href=\"http://192.168.10.108:9001/web/css");
             result=result.replaceAll("src=\"./img", "src=\"http://192.168.10.108:9001/web/img");
             Document doc = Jsoup.parse(result);
             //result= result.replaceAll("last_tck", "last_tck1").replaceAll("ceshi_css_1", "ceshi_css_111");
             doc.getElementsByClass("init").remove();
             doc.getElementsByClass("last_tck").remove();
             doc.getElementsByClass("ceshi_css_1").remove();
             doc.getElementsByClass("help_me_mb").remove();
             doc.getElementsByClass("help_me").remove();
             doc.getElementsByClass("help_me_txt").remove();
             doc.getElementsByClass("ajax_txt").remove();
             doc.getElementsByClass("wrapper-dropdown-3").remove();
             
             FileWriter writer = new FileWriter("D:\\123456.html");
             writer.write(doc.html());
              webClient.close();
             String fileName = "D:\\1234.pdf";
             String pluginPath = "E:\\plugins-windows-x64\\plugins";
             HtmlConverter.setPluginPath(pluginPath);
             HtmlConverter.convert("D:\\123456.html", fileName, true, 10000, new Size(700f, 800f), new PdfMargins(0));
    }

}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值