我有一个带法语口音的简单字符串。我想使用ITextRenderer将其保存到pdf。问题是所有重音都会从生成的pdf中删除。
要保存的输入字符串来自我的力度模板。在那里,我是doinf StringEscapeUtils.escape(StringEscapeUtils.unescape(stringWithAccents)),这个过程给我输入字符串,如Supplément: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中我看不到像à& egrave
这样的字符答案 0 :(得分:1)
尝试将编码从UTF-8更改为ISO-8859-1。