首页>代码>java+vuejs导出pdf和word文件>/ces/src/main/java/jsopu.java
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.nio.charset.Charset;

/***
 * html
 * 转pdf文档
 */

public class jsopu {
    private static final String HTML_PATH = "D:\\sdf.html";
    private static final String PDF_PATH = "d:\\2222222.pdf";
    public static void main(String[] args) throws IOException, DocumentException {


        File f = new File(HTML_PATH);
        //转换html以jsoup格式
        org.jsoup.nodes.Document jsdocument = Jsoup.parse(f, "UTF-8");
        String html = jsdocument.html();
        //个人业务代码开始----------------------------str
        //本段落的作用是获取html里面所有标签为table的内容。
        //循环当thead 内容为空时 移除该table标签
        Elements elements =  jsdocument.getElementsByTag("table");
        for (Element d:elements){
            Element d1 =  d.getElementById("romveiss");
            if(d!=d1){
                if (d.getElementsByTag("thead").text().isEmpty()){
                    d.remove();
                }
            }

        }
        //个人业务代码end------------------------------
        System.out.println(jsdocument.html());
        File file1 = new File(HTML_PATH);
        FileOutputStream fos = new FileOutputStream(file1);
        BufferedWriter bw = null;
        bw = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
        bw.write(jsdocument.html());
        bw.close();
        fos.close();

        Document document = new Document(PageSize.A4, 20, 20, 20, 20);//左右上下的margin
        document.setMargins(20, 20, 20, 20);
        PdfWriter pdfwriter;
        pdfwriter = PdfWriter.getInstance(document, new FileOutputStream(PDF_PATH));
        document.open();
        XMLWorkerHelper wh = XMLWorkerHelper.getInstance();
        wh.parseXHtml(pdfwriter, document, new FileInputStream(HTML_PATH), Charset.forName("UTF-8"), new DefineFontProvider());
        document.close();
        System.out.println("succeed");
    }


}
最近下载更多
Jacko01  LV8 3月20日
467277  LV13 2024年3月26日
papatap  LV1 2023年4月20日
倒转流年  LV1 2022年7月7日
lhr6767  LV1 2022年4月29日
代码醉了哈哈  LV4 2022年3月8日
我不是大象  LV10 2021年11月11日
蒋彦云  LV1 2021年9月26日
...  LV1 2021年9月5日
毛哲  LV1 2021年9月3日
最近浏览更多
Jacko01  LV8 3月20日
liuyinwan 1月14日
暂无贡献等级
源蝈蝈 2024年12月28日
暂无贡献等级
3334004690  LV10 2024年6月24日
19950118 2024年4月19日
暂无贡献等级
zhos0212  LV19 2024年3月29日
467277  LV13 2024年3月26日
studengaaa 2024年3月17日
暂无贡献等级
a1325922140  LV1 2024年2月19日
wbbhappy  LV13 2024年1月13日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友