简体   繁体   中英

XML to PDF using Java and Pentaho Report Designer

I am using pentaho prpt to generate pdf from xml using Java. The code has to take the prpt, and predefined data source (xml) and generate pdf whenever I run this code. I tried working with this but I'm getting these exceptions.

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)

Here is the code

 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()+ "]");
}
}

please help. If i click on the errors in Eclipse. it says '151 invalid line number for ResourceManager.

HERES MY CODE 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();
}
}
}

Recently the project's code style was changed, that is why you cannot find the line. Check the version of PRD you are utilizing and use appropriate branch.

As for your problem, it apparently happens because reportDefinitionURL is null . Check your report's location. Also make sure the engine is initialized ( ClassicEngineBoot.getInstance().start() ).

And the last, I would recommend you to take a look at these classes:

  • PdfReportProcessTask
  • PdfOutputProcessor

I am not sure you indeed need to extend AbstractReportGenerator .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM