DynamicReports AP:导出一个空的pdf

时间:2015-01-15 07:21:56

标签: java jasper-reports dynamic-reports

我正在尝试将报告导出为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;
    }

1 个答案:

答案 0 :(得分:0)

回答我自己的问题

缺少jar文件和导入

所以添加了jar

com.lowagie.text-2.1.7.jar

并导入了班级

import com.lowagie.text.DocumentException;

现在一切正常!!!!