DynamicReports AP:导出一个空的 pdf

DynamicReports AP: Exports an empty pdf

我正在尝试将报告导出为 PDF 格式,但生成的是空白 PDF。

我正在通过 JRDatasource 方法填写报告。 .show() 方法显示所有列都经过格式化和填充的填充报告,但是当我使用 .toPdf() 方法将其导出为 PDF 格式时,它会生成一个空白报告。

这里是整个过程的代码:

1) 生成报告列

InvoiceData data = new InvoiceData();
        AggregationSubtotalBuilder<BigDecimal> totalSum;

        StyleBuilder boldStyle         = stl.style().bold();
        StyleBuilder boldCenteredStyle = stl.style(boldStyle)
                                                    .setHorizontalAlignment(HorizontalAlignment.CENTER);
        StyleBuilder titleStyle         = stl.style(boldStyle).setFontSize(15);
        StyleBuilder CenteredStyle = stl.style().setHorizontalAlignment(HorizontalAlignment.CENTER);
        StyleBuilder columnTitleStyle  = stl.style(boldCenteredStyle)       
                                                    .setBorder(stl.pen1Point()).setBackgroundColor(Color.LIGHT_GRAY);
        TextColumnBuilder<Integer> rowNumberColumn = col.reportRowNumberColumn("No.").setFixedColumns(2).setStyle(CenteredStyle);
        TextColumnBuilder<String>    columnItem = col.column("Item Name", "itemname", type.stringType()).setFixedColumns(10).setStyle(CenteredStyle);
        TextColumnBuilder<String>    columnpkg = col.column("Pkg Date", "pkgdate", type.stringType()).setFixedColumns(7).setStyle(CenteredStyle);
        TextColumnBuilder<String>    columnmanf = col.column("Manft Date", "manftdate", type.stringType()).setFixedColumns(7).setStyle(CenteredStyle);
        TextColumnBuilder<String>    columnexp = col.column("Exp Date", "expdate", type.stringType()).setFixedColumns(7).setStyle(CenteredStyle);
        TextColumnBuilder<String>    columnbatch = col.column("Batch No", "batch", type.stringType()).setFixedColumns(5).setStyle(CenteredStyle);
        TextColumnBuilder<Double>    columnunit = col.column("Unit Price", "unit", type.doubleType()).setFixedColumns(5).setStyle(CenteredStyle);
        TextColumnBuilder<Integer>    columnqty = col.column("Qty", "qty", type.integerType()).setFixedColumns(2).setStyle(CenteredStyle);
        TextColumnBuilder<BigDecimal>    columnsub = columnqty.multiply(columnunit).setTitle("Subtotal").setFixedColumns(5).setStyle(CenteredStyle);
        totalSum = sbt.sum(columnsub)
                         .setLabel("Total:")
                         .setLabelStyle(Templates.boldStyle);

2) 生成报告供查看

        try {
            DateFormat dateformat = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
            java.util.Date date = new java.util.Date();
            JasperReportBuilder report = DynamicReports.report();

            report.setColumnTitleStyle(columnTitleStyle)
            .highlightDetailEvenRows()
            .title(cmp.text("MEDICAL STORE").setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.LEFT),
                   cmp.text("Invoice No:").setStyle(titleStyle).setHorizontalAlignment(HorizontalAlignment.RIGHT),
                   cmp.text("Address of the shop").setHorizontalAlignment(HorizontalAlignment.JUSTIFIED),
                    Components.horizontalList().setStyle(stl.style(10)).setGap(50).add(Components.hListCell(createCustomerComponent("Bill To",data.getInvoice().getBillTo(),person,contact)).heightFixedOnTop(),
                             cmp.hListCell(createDoctorComponent("Prescribed By", data.getInvoice().getShipTo(),doctorregno,doctor)).heightFixedOnTop()),
                                         cmp.verticalGap(10))
                  .columns(rowNumberColumn,
                          columnItem,
                          columnpkg,
                          columnmanf,
                          columnexp,
                          columnbatch,
                          columnunit,
                          columnqty,
                          columnsub)
                  .setDataSource(populatereport(prod,pkgdate,manufact,exp,batch,unit,qty))
                  .subtotalsAtSummary(totalSum)
                  .pageFooter(Templates.footerComponent)
                  .summary(Components.horizontalList(
                          Components.verticalGap(100),                    
                          Components.text("Thank you for your business").setStyle(Templates.bold12CenteredStyle)
                          ))
                          .pageFooter(cmp.text("Address for Medical Store here"))
                    .show();

3) 将报告导出为 pdf

                 report.toPdf(new FileOutputStream("/home/kunal/Medicam reports/Customer Bill/"+String.valueOf(dateformat.format(date))+".pdf"));
                 //Here is some problem when exporting to pdf

            } catch(DRException e) {
            e.printStackTrace();
            log.error("Exception in Report");
        }

填写报告

private JRDataSource populatereport(String[] prod, String[] pkgdate,
            String[] manufact, String[] exp, String[] batch, String[] unit,
            String[] qty) {
        DRDataSource dataSource = new DRDataSource("itemname", "pkgdate", "manftdate", "expdate", "batch", "unit", "qty");
        for (int i = 0; i < prod.length; i++) {

            dataSource.add(prod[i], pkgdate[i], manufact[i], exp[i], batch[i], Double.parseDouble(unit[i]), Integer.parseInt(qty[i]));
        }
        return dataSource;
    }

回答我自己的问题

缺少 jar 文件和导入

所以添加了 jar

com.lowagie.text-2.1.7.jar

并导入了 class

import com.lowagie.text.DocumentException;

现在一切正常了!!!!