我有这个代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@page import="com.itextpdf.text.Document"%>
<%@page import="com.itextpdf.text.Paragraph"%>
<%@page import="com.itextpdf.text.PageSize,java.sql.Blob"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*,com.itextpdf.text.DocumentException" %>
<%@page import="java.io.*,java.text.SimpleDateFormat"%>
<%@page import="java.sql.*,java.nio.charset.Charset,com.itextpdf.text.pdf.BaseFont"%>
<%@ page import="com.itextpdf.tool.xml.ElementHandler,java.util.List,java.util.Arrays,java.util.Collections,java.util.*,com.itextpdf.text.pdf.*,com.itextpdf.tool.xml.ElementList,com.itextpdf.text.Rectangle,com.itextpdf.text.Element,com.itextpdf.text.*,com.itextpdf.text.Font,java.awt.Color,com.itextpdf.text.Font.FontFamily,java.util.Date,java.text.*,com.itextpdf.tool.xml.XMLWorkerHelper" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Class.forName("com.mysql.jdbc.Driver");
request.setCharacterEncoding("UTF-8");
String date=request.getParameter("user");
OutputStream output=response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=details.pdf");
try{
Document document = new Document(PageSize.A4, 20, 20, 130, 20);
PdfWriter writer=PdfWriter.getInstance(document, output);
document.open();
BaseFont bf = BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
writer.setPageEvent(new HeaderAndFooter(date));
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
List arrlist = new ArrayList();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gla", "root", "root");
Statement st=con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs=st.executeQuery("SELECT * FROM user_start2 where date='"+date+"' ");
while(rs.next()){
arrlist.add(rs.getString("data"));
}
InputStream is=null;
for(int i=0;i<interval+1;i++){
String str =(String) arrlist.get(i);
StringBuffer sb=new StringBuffer();
sb.append(str+"<br />");
String ss=sb.toString();
is = new ByteArrayInputStream(ss.getBytes());
worker.parseXHtml(writer,document,is);
}
is.close();
writer.flush();
writer.close();
document.close();
}catch(IOException e){e.printStackTrace();}
output.close();
%>
</body>
</html>
上面的代码给了我这个结果
正如您在图像中看到的,它完美地显示英文,但对于印地文字体,它显示????? 我也尝试用这一行添加字符编码
is = new ByteArrayInputStream(ss.getBytes("UTF-8"));
但它给了我这个输出
以前我只使用了没有 xmlworker 的 itext,它在 pdf 上给了我正确的印地语字体,但是自从使用 xmlworker 我得到了????或垃圾值。
如果我需要提供更多数据,请告诉我。