<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</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.11</version>
</dependency>
@RequestMapping("pdf")
public void pdf(HttpServletRequest request, HttpServletResponse response, String html) {
Document document = new Document(PageSize.LETTER);
try {
response.setContentType("application/pdf");
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
document.open();
InputStream htmlStream = new ByteArrayInputStream(html.getBytes());
InputStream cssStream = request.getServletContext().getResourceAsStream("/resources/css/pdf.css");
XMLWorkerHelper.getInstance().parseXHtml(writer, document, htmlStream , cssStream , new XMLWorkerFontProvider() {
@Override
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) {
e.printStackTrace();
}
Font font = new Font(bf, size, style, color);
font.setColor(color);
return font;
}
});
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
document.close();
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者