将印地语显示在使用JSP创建的pdf上

时间:2014-01-07 07:27:41

标签: java jsp pdf-generation

这是我的代码

<%@ 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"%>
   <%@ 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"%>
   <%@ 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>
<% 
String date=request.getParameter("user");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat myFormat = new SimpleDateFormat("EEE,dd MMM yyyy");
final java.util.Date dtt = df.parse(date);
final String ds = myFormat.format(df.parse(date));

class HeaderAndFooter extends PdfPageEventHelper {
   private String name = "";
   protected Phrase footer;
   protected Phrase header;
   Font headerFont = new Font(FontFamily.COURIER, 13, Font.UNDERLINE);
   Font footerFont = new Font(FontFamily.HELVETICA, 11, Font.BOLD);
   String date1;
   public HeaderAndFooter(String name) {
    super();
    this.name = name;
    header = new Phrase("***** Header *****");
    footer = new Phrase("**** Footer ****");
  }

  @Override
   public void onEndPage(PdfWriter writer, Document document) {
     PdfContentByte cb = writer.getDirectContent();
     //header content
     String headerContent =name;
     //header content
     String footerContent = headerContent;
  //date selection
     ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("Date:"+ds,headerFont), document.leftMargin() - 1, document.top() + 18, 0);
     }
  }
OutputStream output=response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "inline; filename=details.pdf");
try{
    Document document = new Document(PageSize.A4);
PdfWriter writer=PdfWriter.getInstance(document, output);
document.open();
writer.setPageEvent(new HeaderAndFooter(date));
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yre", "root", "root");
Statement st=con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
List arrlist = new ArrayList();
ResultSet rsta=st.executeQuery("Select * from yu where date='"+date+"' ");
int interval=0;
while(rsta.next()){
interval=rsta.getInt("inter");
}  
System.out.println(interval);
ResultSet rs=st.executeQuery("SELECT * FROM ur_start2 where date='"+date+"' ");
 while(rs.next()){
 arrlist.add(rs.getString("data"));
 }  
for(int i=0;i<interval+1;i++){
  String str =(String) arrlist.get(i);
  worker.parseXHtml(writer, document, new StringReader("<html><head></head><body>"+str+"<br></br><br></br></body></html>"));
}
document.close();
writer.flush();
writer.close();
output.close();
}catch(IOException e){e.printStackTrace();} 
%>
</body>
</html>

这只显示了pdf中的英文字符。因此印地文字体是不可见的。

我在印第安语中输入的内容也会在印地语中找回textareas。但是在pdf上我无法在印地文中找回它。

我应该怎么做才能用印地语取回它?

1 个答案:

答案 0 :(得分:1)

即使是最新的iText版本也不支持梵文脚本,请参考here

但是,有一些替代方法,比如使用awt Graphics2D类,设置支持unicode字符的字体并将其添加到PDF中(尽管不是文本)。

示例:here

使用上述参考输出:

enter image description here

'印地语中的新PDF'呈现为此处的文字