尽管我将导出器的CHARACTER_ENCODING属性设置为UTF-8,但我在使用 iText PDF 库导出 PDF 时遇到了字符编码问题,如下所示:
Class clazz = Class.forName(User.class);
Document document = new Document();
PdfWriter.getInstance(document, baos);
document.open();
addMetaData(document);
addDate(document);
addTitlePage(document, className);
addEmptyLineToDocument(document, 2);
addContent(document, className, clazz);
addEmptyLineToDocument(document, 2);
addSignature(document);
return document;
这是addContent方法示例:
private static FontFamily fontFamily = FontFamily.TIMES_ROMAN;
Font fontNormal10 = new Font(fontFamily, 10, Font.NORMAL);
Paragraph paragraph = new Paragraph();
Chunk chunk = new Chunk("Here is the list of " + className, fontNormal10);
paragraph.add(chunk);
// continues
然后我用 ServletOutputStream 编写文档:
// **document** => which I created and returned above
document.close();
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setHeader("Content-Disposition", "attachment; filename=" + outputFileName);
response.setContentType("application/pdf; charset=UTF-8");
response.setContentLength(baos.size());
ServletOutputStream outputStream = response.getOutputStream();
baos.writeTo(outputStream);
outputStream.flush();
outputStream.close();
- 我还添加了 JVM 参数“ file.encoding=UTF-8 ”。
- 使用“ charset=UTF-8; ”属性设置响应内容类型。
有任何想法吗?提前致谢..