使用Java和Pentaho报表设计器将XML转换为PDF
我正在使用pentaho prpt使用Java从xml生成pdf。 每当我运行此代码时,代码必须采用prpt和预定义数据源(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的行号无效
这是我的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();
}
}
}
# 1 楼答案
最近,项目的代码样式发生了更改,这就是为什么找不到行的原因。检查您正在使用的PRD版本,并使用适当的分支机构
至于你的问题,这显然是因为
reportDefinitionURL
是null
。检查报告的位置。还要确保引擎已初始化(ClassicEngineBoot.getInstance().start()
)最后,我建议大家看看这些课程:
PdfReportProcessTask
PdfOutputProcessor
我不确定您是否真的需要扩展
AbstractReportGenerator