itext5 html转pdf

1.maven jar包依赖

<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
 </dependency>
<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
 </dependency>
<dependency>
            <groupId>com.itextpdf.tool</groupId>
            <artifactId>xmlworker</artifactId>
            <version>5.5.13</version>
 </dependency>

  2.核心代码

public static ByteArrayOutputStream htmlToPdf(String htmlStr){
        Document document = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            document = new Document(PageSize.A3);
            PdfWriter pdfWriter = PdfWriter.getInstance(document,
                    baos);
            document.open(); 
            XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, new ByteArrayInputStream(htmlStr.getBytes()), Charset.forName("UTF-8"),new AsianFontProvider());
        } catch (DocumentException e) {log.error("HtmlToPDF 获取实例pdfWriter失败....",e);
            throw  new BusException();
        }catch (IOException e) {
            log.error("HtmlToPDF 转换pdf失败....",e);
            throw  new BusException();
        } catch (RuntimeWorkerException e) {
            log.error("html标签未闭合...", e);
            throw new BusException();
        } catch (Exception e) {
            log.error("HtmlToPDF 转换pdf失败....", e);
            throw new BusException();
        } finally {
            if (document != null) {
                try {
                    document.close();
                } catch (Exception e) {
                    log.error("document关闭异常...",e);
                    throw new BusException();
                }
            }
        }
        return baos;
    }

/**
 * 
 * @version 1.0
 * @date 2020/3/22 15:43
 * @description 解决pdf文件中文不显示的问题
 **/
@Slf4j
public class AsianFontProvider extends XMLWorkerFontProvider {
 
    public Font getFont(final String fontname, final String encoding,
                        final boolean embedded, final float size, final int style,
                        final BaseColor color) {
        BaseFont bf = null;
        try {
            bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        } catch (Exception e) {
           log.error("生成pdf文件,添加字体失败",e);
        }
        Font font = new Font(bf, size, style, color);
        font.setColor(color);
        return font;
    }
}

 

1、解决中文问题 2、附字体 3、动态html拼接pdf public static void htmlCodeComeString(String linkcss,String htmlCode, String outputFile,String title) throws Exception { OutputStream os = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); renderer.setDocumentFromString(getConversionHtmlCode(linkcss,htmlCode,title)); ITextFontResolver fontResolver = renderer.getFontResolver(); URL fontPath = ItextUtil.class.getResource("simsun.ttc"); fontResolver.addFont(fontPath.toString(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // 解决图片的相对路径问题 // renderer.getSharedContext().setBaseURL("file:/F:/teste/html/"); renderer.layout(); renderer.createPDF(os); System.out.println("======换成功!"); os.close(); os.flush(); } public static void main(String[] args) { ItextUtil itextUtil = new ItextUtil(); String html = ""; html += ""; html += "企业信息"; html += " "; html += " "; html += " 登记日期"; html += " 2006-04-28"; html += " "; html += " "; html += " 纳税人编号"; html += " HSJIHKS002"; html += " "; html += " "; html += " 有效标志"; html += " Y"; html += " "; html += " "; html += " 社会信用代码"; html += " 916101317H"; html += " "; html += " "; html += " 评估机关代码"; html += " 盛世"; html += " "; html += " "; html += " 工商注销日期"; html += " 2006-04-28"; html += " "; html += " "; html += ""; String outputFile = "D:\\pdf\\aa.pdf"; try { itextUtil.htmlCodeComeString("",html,outputFile,""); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("生成结束!!!"); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值