<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();
}