flying-saucer pdf

maven下载开源jar包
<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf</artifactId>
<version>9.0.1</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-pdf-itext5</artifactId>
<version>9.0.1</version>
<type>jar</type>
</dependency>
</dependencies>

导出代码:
Configuration cfg = new Configuration();
//initial the template configuration
cfg.setClassForTemplateLoading(ExportPdfUtils.class, " pdf path");
cfg.setObjectWrapper(new DefaultObjectWrapper());
//get template data
Template template = cfg.getTemplate(templateName);
StringWriter tmpWriter = new StringWriter();

template.process(data, tmpWriter);

ITextRenderer renderer = new ITextRenderer(); 
ITextFontResolver fontResolver = renderer.getFontResolver();
   
    //set font
fontResolver.addFont(FONT_PATH,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); 
renderer.setDocumentFromString(tmpWriter.toString());
   
renderer.layout(); 
renderer.createPDF(out);

你可能感兴趣的:(pdf)