html到pdf使用itext xmlworker [英] html to pdf using itext xmlworker

查看:143
本文介绍了html到pdf使用itext xmlworker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

   Document document = new Document(PageSize.A4, ml, mr, mt, mb);
   PdfWriter writer=PdfWriter.getInstance(document, response.getOutputStream());
   String sc_na=request.getParameter("school_name");
   String name=sc_na;
   writer.setPageEvent(new HeaderAndFooter(name));
   document.open();
   XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
   //HTMLWorker htmlWorker = new HTMLWorker(document);
   Class.forName("com.mysql.jdbc.Driver");
   Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/elect", "root", "root");
   Statement st=con.createStatement();
   ResultSet rs=st.executeQuery("Select * from test3");
   List arrlist = new ArrayList();
   while(rs.next()){
   String xa =rs.getString("display");
   if(xa.equals("1")){
    arrlist.add(rs.getString("question_text"));
   }
 }
 Collections.shuffle(arrlist); 
for(int i=0;i<nb;i++){
    String str = (String) arrlist.get(i);
     worker.parseXHtml(writer, document, new StringReader("<br>"));
     worker.parseXHtml(writer, document, new StringReader(str));

}
document.close();

这是我的代码片段。使用我在一个列中获取所有数据。应该怎么做我这样做,我可以进入多个列。我猜是使用PDFptable,但我如何在这里应用它。请帮助。

This is a snippet of my code.Using I get all my data in one single column.What should I do so that I can get into multiple columns.One way I guess is using PDFptable but how do I apply it here.Please Help.

推荐答案

对于Html到PDF,请使用Flying Saucer: http://code.google.com/p/flying-saucer/

For Html to PDF, use Flying Saucer: http://code.google.com/p/flying-saucer/

它使用iText 2.0.8并且它尊重CSS。代码如下:

It uses iText 2.0.8 and it respects a CSS. It is as much code as:

 org.w3c.dom.Document doc = . . .
 ITextRenderer renderer = new ITextRenderer();
 renderer.setDocument(doc, null);

 renderer.layout();
 renderer.createPDF(out);

确保您的XHTML链接到可访问的CSS,例如

Make sure your XHTML links to a reachable CSS, e.g.

 <link rel="stylesheet" type="text/css" href="file://. . ./pdf.css" media="print"/>

这篇关于html到pdf使用itext xmlworker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆