com.itextpdf.html2pdf -2.1.5 html轉換pdf


html2pdf 是itext提供的網頁轉PDF包

老版本的

itextpdf 對html標簽轉換支持太差,現在升級到最新版本

1,使用版本maven

          <dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>html2pdf</artifactId>
			<version>2.1.5</version>
		</dependency>

2, 示例

 String html="<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><title>First parse</title></head>" +
                "<body  style=\"font-family: SimSun\"><div class=\"t1 pl2\" style=\"  margin: 0;  display: flex;flex-wrap: wrap;align-items: center;\">\n" +
                "        <div class=\"ml15\" style=\"display: inline-block;\"><input id=\"ck1\" checked=\"checked\" type=\"checkbox\"><label for=\"ck1\">嘟嘟嘟</label></div>\n" +
                "        <div class=\"ml15\" style=\"display: inline-block;\"><input id=\"ck2\" type=\"checkbox\"><label for=\"ck2\">嘿嘿嘿</label></div>\n" +
                "        <div class=\"ml15\" style=\"display: inline-block;\"><input id=\"ck3\" type=\"checkbox\"><label for=\"ck3\">哈哈哈</label></div>\n" +
                "        <div class=\"ml15\" style=\"display: inline-block;\"><input id=\"ck4\" type=\"checkbox\"><label for=\"ck4\">啦啦啦</label></div>\n" +
                "    </div><div><ul>\n" +
                "<li>Coffee</li>\n" +
                "<li>Milk</li>\n" +
                "<li>可以選擇</li>\n" +
                "</ul></div></body></html>";

        //pdf轉換配置類
        ConverterProperties converterProperties=new ConverterProperties();

        //中文字體
        String a= "D:\\work\\test\\src\\main\\webapp\\static\\dsPdf\\SONGTI.TTF";

        FontProvider fontProvider=new FontProvider();
        fontProvider.addStandardPdfFonts();
        fontProvider.addFont(a);
        converterProperties.setFontProvider(fontProvider);
        converterProperties.setCharset("UTF-8");
        //輸出地址
        PdfWriter pdfWriter =new PdfWriter(new FileOutputStream("D:\\work\\test\\src\\main\\webapp\\upload\\2019-10-28"+"/a.pdf"));
        //開始轉換
        HtmlConverter.convertToPdf(html,pdfWriter,converterProperties);

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM