2015-10-05 59 views
0

我正在使用pentaho prpt从使用Java的xml生成pdf。 代码必须采用prpt和预定义数据源(xml),并在运行此代码时生成pdf。我试着用这个工作,但我得到这些例外。使用Java和Pentaho报告设计器将XML转换为PDF

Exception in thread "main" java.lang.NullPointerException: Key data must not be null. 
at org.pentaho.reporting.libraries.resourceloader.DefaultResourceManagerBackend.createKey(DefaultResourceManagerBackend.java:53) 
at org.pentaho.reporting.libraries.resourceloader.ResourceManager.createKey(ResourceManager.java:151) 
at org.pentaho.reporting.libraries.resourceloader.ResourceManager.createKey(ResourceManager.java:137) 
at org.pentaho.reporting.libraries.resourceloader.ResourceManager.createDirectly(ResourceManager.java:213) 
at report.Sample1.getReportDefinition(Sample1.java:67) 
at report.AbstractReportGenerator.generateReport(AbstractReportGenerator.java:19 0) 
at  report.AbstractReportGenerator.generateReport(AbstractReportGenerator.java:15 7) 
    at report.Sample1.main(Sample1.java:141) 

下面是代码

public class Sample1 extends AbstractReportGenerator 
{ 
/** 
* Default constructor for this sample report generator 
*/ 
public Sample1() 
{ 
} 
/** 
* Returns the report definition which will be used to generate the report. In 
this case, the report will be 
* loaded and parsed from a file contained in this package. 
* 
* @return the loaded and parsed report definition to be used in report 
generation. 
*/ 
public MasterReport getReportDefinition() 
{ 
try 
{ 
// Using the classloader, get the URL to the reportDefinition file 
final ClassLoader classloader = this.getClass().getClassLoader(); 
final URL reportDefinitionURL = classloader.getResource("/report1.prpt"); 
// Parse the report file 
final ResourceManager resourceManager = new ResourceManager(); 
resourceManager.registerDefaults(); 
final Resource directly=resourceManager.createDirectly(reportDefinitionURL, MasterReport.class); 
return (MasterReport) directly.getResource(); 
} 
catch (ResourceException e) 
{ 
e.printStackTrace(); 
} 
return null; 
} 
/** 
* Returns the data factory which will be used to generate the data used during 
report generation. In this example, 
* we will return null since the data factory has been defined in the report 
definition. 
* 
* @return the data factory used with the report generator 
*/ 
public DataFactory getDataFactory() 
{ 
return null; 
} 
/** 
* Returns the set of runtime report parameters. This sample report uses the 
following three parameters: 
* <ul> 
* <li><b>Report Title</b> - The title text on the top of the report</li> 
* <li><b>Customer Names</b> - an array of customer names to show in the 
report</li> 
* <li><b>Col Headers BG Color</b> - the background color for the column 
headers</li> 
* </ul> 
* 
* @return <code>null</code> indicating the report generator does not use any 
report parameters 
*/ 

public Map<String, Object> getReportParameters() 
{ 
final Map<String, Object> parameters = new HashMap<String, Object>(); 
parameters.put("Report Title", "Simple Embedded Report Example with Parameters>"); 
parameters.put("Col Headers BG Color", "yellow"); 
parameters.put("Customer Names", 
new String [] { 
"American Souvenirs Inc", 
"Toys4GrownUps.com", 
"giftsbymail.co.uk", 
"BG&E Collectables", 
"Classic Gift Ideas, Inc", 
}); 
return parameters; 
} 
/** 
* Simple command line application that will generate a PDF version of the 
report. In this report, 
* the report definition has already been created with the Pentaho Report 
Designer application and 
* it located in the same package as this class. The data query is located in 
that report definition 
* as well, and there are a few report-modifying parameters that will be passed 
to the engine at runtime. 
* <p/> 
* The output of this report will be a PDF file located in the current directory 
and will be named 
* <code>SimpleReportGeneratorExample.pdf</code>. 
* 
* @param args none 
* @throws IOException indicates an error writing to the filesystem 
* @throws ReportProcessingException indicates an error generating the report 
*/ 
    public static void main(String[] args) throws IOException,ReportProcessingException 
{ 
// Create an output filename 
final File outputFilename = new File(Sample1.class.getSimpleName() + ".pdf"); 
// Generate the report 
new Sample1().generateReport(AbstractReportGenerator.OutputType.PDF,outputFilename); 
// Output the location of the file 
System.err.println("Generated the report [" + outputFilename.getAbsolutePath()+ "]"); 
} 
} 

请帮助。如果我点击Eclipse中的错误。它说'151 ResourceManager的无效行号。

我的继承人FOR AbstractReportGenerator

public abstract class AbstractReportGenerator { 
public static enum OutputType 
{ 
PDF, EXCEL, HTML 
} 
public PdfMakerAbstract() 
{ 
// Initialize the reporting engine 
ClassicEngineBoot.getInstance().start(); 
} 
public abstract MasterReport getReportDefinition(); 
public abstract DataFactory getDataFactory(); 
public abstract Map<String, Object> getReportParameters(); 
public void generateReport(final OutputType outputtype, File outputFile) 
throws IllegalArgumentException, IOException, ReportProcessingException 
{ 
if (outputFile == null) 
{ 
throw new IllegalArgumentException("The output file was not specified"); 
} 
public void generateReport(final OutputType outputtype, OutputStream outputStream) 
throws IllegalArgumentException, ReportProcessingException 
{ 
if (outputStream == null) 
{ 
throw new IllegalArgumentException("The output stream was not specified"); 
} 
    // Get the report and data factory 
final MasterReport report = getReportDefinition(); 
final DataFactory dataFactory = getDataFactory(); 
// Set the data factory for the report 
if (dataFactory != null) 
{ 
report.setDataFactory(dataFactory); 
} 
// Add any parameters to the report 

final Map<String, Object> reportParameters = getReportParameters(); 
if (null != reportParameters) 
{ 
for (String key : reportParameters.keySet()) 
{ 
report.getParameterValues().put(key, reportParameters.get(key)); 
} 
} 

// Prepare to generate the report 
AbstractReportProcessor reportProcessor = null; 
try 
{ 
// Greate the report processor for the specified output type 
switch (outputtype) 
{ 
case PDF: 
{ 
final PdfOutputProcessor outputProcessor =new PdfOutputProcessor(report.getConfiguration(), outputStream,report.getResourceManager()); 
reportProcessor = new PageableReportProcessor(report, outputProcessor); 
break; 
} 
case EXCEL: 
{ 
final FlowExcelOutputProcessor target =new FlowExcelOutputProcessor(report.getConfiguration(),outputStream, report.getResourceManager()); 
reportProcessor = new FlowReportProcessor(report, target); 
break; 
} 
case HTML: 
{ 
final StreamRepository targetRepository = new StreamRepository(outputStream); 
final ContentLocation targetRoot = targetRepository.getRoot(); 
final HtmlOutputProcessor outputProcessor = new StreamHtmlOutputProcessor(report.getConfiguration()); 
final HtmlPrinter printer = new AllItemsHtmlPrinter(report.getResourceManager()); 
printer.setContentWriter(targetRoot, new 
DefaultNameGenerator(targetRoot, "index", "html")); 
printer.setDataWriter(null, null); 
printer.setUrlRewriter(new FileSystemURLRewriter()); 
outputProcessor.setPrinter(printer); 
reportProcessor = new StreamReportProcessor(report, outputProcessor); 
break; 
} 
} 
// Generate the report 
reportProcessor.processReport(); 
} 
finally 
{ 
if (reportProcessor != null) 
{ 
reportProcessor.close(); 
} 
} 
} 
} 
OutputStream outputStream = null; 
try 
{ 
// Open the output stream 
outputStream = new BufferedOutputStream(new FileOutputStream(outputFile)); 
// Generate the report to this output stream 
generateReport(outputtype, outputStream); 
} 
finally 
{ 
if (outputStream != null) 
{ 
outputStream.close(); 
} 
} 
} 

回答

2

CODE最近项目的代码风格改变了,这就是为什么你不能找到就行了。检查您正在使用的PRD版本并使用适当的分支。

至于你的问题,它显然是因为reportDefinitionURLnull。检查您的报告的位置。还要确保发动机已初始化(ClassicEngineBoot.getInstance().start())。

而最后,我建议你看一看这些类:

  • PdfReportProcessTask
  • PdfOutputProcessor

我不知道你确实需要扩展AbstractReportGenerator

+0

请您详细说明如何使用适当的分支? – user3270763

+0

和reportdefinitionURL,我在存储Sample1.java的文件夹中有一个report1.prpt文件。我的指定路径是否正确?我在AbstractReportGenerator中引擎初始化 – user3270763

+0

我正在使用Pentaho Reporting 5.4.0 – user3270763