dynamicreports ap:导出一个空的pdf

nx7onnlm  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(378)

我试图将报告导出为pdf格式,但它生成了一个空白的pdf。
我正在填写报告 JRDatasource 方法。这个 .show() 方法显示已填充的报表,其中所有列都已格式化并填充,但当我将其导出为pdf格式时 .toPdf() 方法生成一个空白报告。
以下是整个过程的代码:
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;
    }
vngu2lb8

vngu2lb81#

回答我自己的问题
缺少jar文件和导入
所以他加了一个jar

com.lowagie.text-2.1.7.jar

并导入了类

import com.lowagie.text.DocumentException;

现在一切都好了!!!!

相关问题