cliffrubinmusic has asked for the wisdom of the Perl Monks concerning the following question:

Hi all; I've got a java module that's supposed to create pdf files from data sources and templates I feed it. I've checked the .class file inline creates and it says the java is compiling just fine "process complete!" Except..there's no pdf file. This is running on a linux server

here's the first part of my code:

#!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser); BEGIN { $ENV{CLASSPATH}='.:/usr/share/java/*:/the paths to the jars used/; } use Inline Java => <<'END',,CLASSPATH=> $ENV{CLASSPATH}; ; import java.datasource.dom4j.Dom4jDataSource; import java.xmlreport.ProcessPdf; import java.xmlreport.ProcessReport; import java.io.InputStream; import java.io.OutputStream; import org.dom4j.Element; public class GeneratePdfReport { InputStream template = new FileInputStream("/path/template.docx"); OuputStream report = new FileOutputStream("/path/report.pdf"); InputStream xmlData = new FileInputStream("/path/datasource.xml"); public static void generatePdfReport(InputStream template, OutputStre +am report, InputStream xmlData) throws Exception { ProcessReport.init(); // create the report. ProcessPdf proc = new ProcessPdf(template, report); proc.processSetup(); proc.processData(new Dom4jDataSource(xmlData), ""); proc.processComplete(); } } END

my question is: what is the perl I need to write to execute the java and create the pdf?

thank you for your wisdom

Replies are listed 'Best First'.
Re: inline::java error
by tobyink (Canon) on Jul 14, 2012 at 14:44 UTC

    You want something like:

    my $gen = GeneratePdfReport->new; $gen->generatePdfReport( ... something ...);

    Though your generatePdfReport looks a bit confused. Are template, report and xmlData supposed to be parameters passed to the method, or are they properties of the GeneratePdfReport object?

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      Hi;

      Thanks for your response

      template, report and xmlData are supposed to be parameters passed to the method. did I not do that correctly? Is that the problem..because when I add the code you suggest I either get an error:

      "wrong number of arguments"

      or when I try to add arguments I get the error:

      "can't convert arg to object"

      any suggestions?

        "template, report and xmlData are supposed to be parameters passed to the method. did I not do that correctly?"

        You did, but then you also defined them as part of the class definition, outside the method, which makes them object properties.

        "or when I try to add arguments I get the error:

        "'can't convert arg to object'"

        That's because the generatePdfReport method expects to be passed three Java objects as parameters. This is totally possible with Inline::Java, but generally speaking I find it much easier to pass plain old strings back and forth over the language barrier.

        In your case, the FileInputStream and FileOutputStream objects can easily be constructed from a filename (i.e. a string), so it certainly makes a lot of sense to pass them into the method as plain old strings instead of instantiating InputStream/OutputStream objects at the Perl end and passing them in.

        Anyway, this is how I'd do it...

        #!/usr/bin/perl use strict; use warnings; BEGIN { $ENV{CLASSPATH}='.:/usr/share/java/*:/the paths to the jars used/' +; } use Inline Java => <<'END', CLASSPATH => $ENV{CLASSPATH}; import java.datasource.dom4j.Dom4jDataSource; import java.xmlreport.ProcessPdf; import java.xmlreport.ProcessReport; import java.io.InputStream; import java.io.OutputStream; import org.dom4j.Element; public class PdfReportGenerator { public static void generatePdfReport( String template_filename, String report_filename, String xmlData_filename ) throws Exception { // This function gets passed its parameters as strings // (makes it easier to call from Perl), so turn them into // InputStream and OutputStream objects. InputStream template = new FileInputStream(template_filename); OuputStream report = new FileOutputStream(report_filename); InputStream xmlData = new FileInputStream(xmlData_filename); ProcessReport.init(); // create the report. ProcessPdf proc = new ProcessPdf(template, report); proc.processSetup(); proc.processData(new Dom4jDataSource(xmlData), ""); proc.processComplete(); } } END my $generator = PdfReportGenerator->new; $generator->generatePdfReport( "/path/template.docx", "/path/report.pdf", "/path/datasource.xml", );

        This is obviously untested, as I don't have the required Java libraries on this system. But, based on my fairly limited experience of Inline::Java, I think it ought to work (or at least be close to working).

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
        Also, what is the something in the:

        $gen->generatePdfReport(..something..)

        is it ("template","report","xmlData")?