2016-08-23 4 views
1

デモコードをされて使用して、表のセルにパディングを追加します。私は、アドホックレポートを作成し、そこからHTMLを生成するために、碧玉のレポートで働いていますHTMLソースからPDFドキュメントを生成するには、次のiTextPdf

public class SimpleAdhocReport 
{ 
public SimpleAdhocReport() 
{ 
    build(); 
} 

private void build() 
{ 
    AdhocConfiguration configuration = new AdhocConfiguration(); 
    AdhocReport report = new AdhocReport(); 
    configuration.setReport(report); 

    AdhocColumn column = new AdhocColumn(); 
    column.setName("item"); 
    report.addColumn(column); 

    column = new AdhocColumn(); 
    column.setName("orderdate"); 
    report.addColumn(column); 

    column = new AdhocColumn(); 
    column.setName("quantity"); 
    report.addColumn(column); 

    column = new AdhocColumn(); 
    column.setName("unitprice"); 
    report.addColumn(column); 

    try 
    { 
     AdhocManager.saveConfiguration(configuration, new FileOutputStream("d:/configuration.xml")); 
     @SuppressWarnings("unused") 
     AdhocConfiguration loadedConfiguration = AdhocManager.loadConfiguration(new FileInputStream("d:/configuration.xml")); 

     JasperReportBuilder reportBuilder = AdhocManager.createReport(configuration.getReport()); 
     reportBuilder.setDataSource(createDataSource()); 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     reportBuilder.toHtml(baos); 
     String html = new String(baos.toByteArray(), "UTF-8"); 
     baos.close(); 

     Whitelist wl = Whitelist.simpleText(); 
     wl.addTags("table", "tr", "td"); 
     String clean = Jsoup.clean(html, wl); 

     clean = clean.replace("<td></td>", ""); 
     clean = clean.replace("<td> </td>", ""); 
     clean = clean.replace("<td> ", "<td>"); 

     Document doc = Jsoup.parse(clean); 
     for (Element element : doc.select("*")) 
     { 
      if (!element.hasText() && element.isBlock()) 
      { 
       element.remove(); 
      } 
     } 

     clean = doc.body().html(); 

     int startIndex = clean.indexOf("<table>", 6); 
     int endIndex = clean.indexOf("</table>"); 
     clean = clean.substring(startIndex, endIndex + 8); 

     BufferedWriter writer = new BufferedWriter(new FileWriter(("d:/test.html"))); 
     writer.write(clean); 

     writer.close(); 

     try 
     { 
      createPdf(clean); 
     } 
     catch (DocumentException e) 
     { 
      e.printStackTrace(); 
     } 

    } 
    catch (DRException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (FileNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

private JRDataSource createDataSource() 
{ 
    DRDataSource dataSource = new DRDataSource("item", "orderdate", "quantity", "unitprice"); 
    for (int i = 0; i < 20; i++) 
    { 
     dataSource.add("Book", new Date(), (int) (Math.random() * 10) + 1, 
       new BigDecimal(Math.random() * 100 + 1).setScale(4, BigDecimal.ROUND_HALF_UP)); 
    } 
    return dataSource; 
} 

public static void main(String[] args) 
{ 
    new SimpleAdhocReport(); 
} 

public void createPdf(String html) throws IOException, DocumentException 
{ 
    com.itextpdf.text.Document document = new com.itextpdf.text.Document(PageSize.LETTER); 
    document.setMargins(30, 30, 80, 30); 
    PdfWriter.getInstance(document, new FileOutputStream("D:\\HTMLtoPDF.pdf")); 
    document.open(); 

    PdfPTable table = null; 
    ElementList list = com.itextpdf.tool.xml.XMLWorkerHelper.parseToElementList(html, null); 
    for (com.itextpdf.text.Element element : list) 
    { 
     table = new PdfPTable((PdfPTable) element); 
    } 
    table.setWidthPercentage(100); 
    ArrayList<PdfPRow> rows = table.getRows(); 

    for (PdfPRow rw : rows) 
    { 
     PdfPCell[] cells = rw.getCells(); 
     for (PdfPCell cl : cells) 
     { 
      cl.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE); 
      cl.setBorder(PdfPCell.NO_BORDER); 
      cl.setNoWrap(true); 
      cl.setPadding(10f); 
      cl.setCellEvent(new MyCell()); 
     } 
    } 

    document.add(table); 

    document.close(); 
} 
} 

class MyCell implements PdfPCellEvent 
{ 
    public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) 
    { 
     float x1 = position.getLeft() - 2; 
     float x2 = position.getRight() + 2; 
     float y1 = position.getTop() + 2; 
     float y2 = position.getBottom() - 2; 
     PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; 
     canvas.rectangle(x1, y1, x2 - x1, y2 - y1); 
     canvas.stroke(); 
    } 
} 

。このHTMLからPDFを生成する必要があります。私が直面しています問題の

カップルは、任意のヘルプは大歓迎です。

  1. 私は

    table.setWidthPercentage(100)を設定しています。

テーブルがあるページでは機能しません。

  1. 私は列間のスペースを増やす必要があります。ブルーノが提案したものを試したhere。動いていない。私はまた、hereの解決策を使ってみました。 Ref。下の画像。

  2. また、デフォルトのiセルイベントが機能していない場合。

table.getDefaultCell().setCellEvent() 

更新:

マイ出力Generated PDF

+0

エラーが発生していますか?あなたはコードをデバッグしようとしましたか? – lsiva

+0

エラーはありません。 PDFが生成されています。生成されたPDFのスクリーンショットを追加できるかどうか確認してみましょう。 –

+0

iReportデザイナーを使ってページをデザインして使用することはできませんか? – lsiva

答えて

0

私は私が望むような方法以下のHTMLを解析することにより、パディングを得ることができた:

質問2で述べた問題を修正
public PdfPTable getTable(String cleanHTML) throws IOException 
{ 
    String CSS = "tr { text-align: center; } td { padding: 5px; }"; 

    CSSResolver cssResolver = new StyleAttrCSSResolver(); 
    CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(CSS.getBytes())); 
    cssResolver.addCss(cssFile); 

    // HTML 
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); 
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); 

    // Pipelines 
    ElementList elements = new ElementList(); 
    ElementHandlerPipeline pdf = new ElementHandlerPipeline(elements, null); 
    HtmlPipeline html = new HtmlPipeline(htmlContext, pdf); 
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); 

    // XML Worker 
    XMLWorker worker = new XMLWorker(css, true); 
    XMLParser p = new XMLParser(worker); 
    p.parse(new ByteArrayInputStream(cleanHTML.getBytes())); 

    return (PdfPTable) elements.get(0); 
} 

。 Q3は必要ありません。

関連する問題