我有一个带有法国口音的简单字符串。我正在尝试使用 ITextRenderer 将其保存到 pdf 中。问题是所有重音都从生成的 pdf 中删除。
要保存的输入字符串来自我的速度模板。在那里,我在做 StringEscapeUtils.escape(StringEscapeUtils.unescape(stringWithAccents)),这个过程给了我输入字符串,比如补充:Visa&Pourboires"。
我的代码:
String documentHtml = "Supplément : àè"
DocumentBuilder builder;
try {
DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
fac.setFeature("http://xml.org/sax/features/namespaces", false);
fac.setFeature("http://xml.org/sax/features/validation", false);
fac.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
fac.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
builder = fac.newDocumentBuilder();
byte[] docByte = documentHtml.getBytes("UTF-8");
ByteArrayInputStream is = new ByteArrayInputStream(docByte);
Document doc = builder.parse(is);
is.close();
File file = new File(this.getFolder(), this.getFileName());
if (file.exists()) {
file.delete();
}
// save pdf
OutputStream os = new FileOutputStream(file);
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, file.getParentFile().getAbsolutePath());
renderer.layout();
renderer.createPDF(os, true);
os.close();
return this.getFolder().getAbsolutePath() + "/" + this.getFileName();
} catch (ParserConfigurationException e) {
LOGGER.error("Error while parsing the configuration " + e.getMessage(), e);
throw new BOServiceException("Error while parsing the configuration : " + e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
LOGGER.error("Encoding error : " + e.getMessage(), e);
throw new BOServiceException("Encoding error : " + e.getMessage(), e);
} catch (SAXException e) {
LOGGER.error("Error in the document because of SAX : " + e.getMessage(), e);
throw new BOServiceException("Error in the document because of SAX : " + e.getMessage(), e);
} catch (IOException e) {
LOGGER.error("Error due to io problem : " + e.getMessage(), e);
throw new BOServiceException("Error due to io problem :" + e.getMessage(), e);
}
所以你知道为什么我的编码不起作用?为什么在结果pdf中我看不到像àè这样的字符