我想在阿拉伯语中显示将通过mysql数据库生成的PDF报告。这是我的代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String relativeWebPath = "/font/A_Nefel_Adeti.ttf";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
File file = new File(absoluteDiskPath);
System.out.print(file);
ByteArrayOutputStream output=new ByteArrayOutputStream();
PDDocument document=new PDDocument();
PDFont font = PDTrueTypeFont.load(document, new File(absoluteDiskPath),new WinAnsiEncoding());
PDPage test=new PDPage();
document.addPage(test);
PDPageContentStream content=new PDPageContentStream(document, test);
final String EXAMPLE = "نديم";
System.out.print(EXAMPLE);
try{
con=DBUtility.getConnection();
stmt=con.prepareStatement("select * from login");
rs=stmt.executeQuery();
while(rs.next()){
userName=rs.getString("UserName");
password=rs.getString("Password");
content.beginText();
content.newLineAtOffset(50, 680);
content.setFont(font, 12);
content.showText(rs.getString("UserName"));
// System.out.print(rs.getString("Password"));
content.endText();
}
}catch(SQLException ex){
ex.printStackTrace();
}
content.close();
document.close();
response.setContentType("application/pdf;base64,BASE_64_ENCODED_PDF");
response.addHeader("Content-Disposition", "inline; filename=\"TestReport.pdf\"");
response.getOutputStream().write(output.toByteArray());
}
它在控制台上显示阿拉伯文字,但PDF给出了这个错误:
输入例外报告
消息U + 0646在此字体的编码中不可用:WinAnsiEncoding
说明服务器遇到内部错误,导致>无法完成此请求。
异常
java.lang.IllegalArgumentException: U+0646 is not available in this font's encoding: WinAnsiEncoding
org.apache.pdfbox.pdmodel.font.PDTrueTypeFont.encode(PDTrueTypeFont.java:368)
org.apache.pdfbox.pdmodel.font.PDFont.encode(PDFont.java:286)
org.apache.pdfbox.pdmodel.PDPageContentStream.showText(PDPageContentStream.java:411)
uae.org.TestReport.doGet(TestReport.java:93)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
答案 0 :(得分:0)
如果尚未完成,请更新至2.0。 javadoc有一个注释:
注意:简单字体仅支持256个字符。对于Unicode支持, 请改用PDType0Font.load(PDDocument,File)。
这样做:
PDFont font = PDType0Font.load(document, new File(absoluteDiskPath);
这不适用于1.8或更早版本。