Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
IRendertask pdf: images not found
gernot
I've a problem generating pdf, the images aren't found: I get an exception:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>org.eclipse.birt.report.engine.util.ResourceLocatorWrapper findResource
</pre>
<br />
I previously used runAndRender() and images are displayed correctly:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>task = engine.createRunAndRenderTask(report);
IPDFRenderOption options= new PDFRenderOption();
options.setOutputFormat(format.toString());
options.setSupportedImageFormats("JPG;PNG;BMP;SVG;GIF");
options.setOutputStream(output);
task.setRenderOption(options);
task.run();</pre>
but then I got problems with pagenumbers - they wasn't calculated correctly when a "soft page break" occured.<br />
I found a solution in the web: call run and render separately.<br />
So I changed my code:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>runTask = engine.createRunTask(report);
rptdocument = (File.createTempFile("birt", ".rptdocument")).getCanonicalPath();
runTask.run(rptdocument);
IReportDocument rptdoc = engine.openReportDocument(rptdocument);
renderTask = engine.createRenderTask(rptdoc);
IPDFRenderOption options= new PDFRenderOption();
options.setOutputFormat(format.toString());
options.setSupportedImageFormats("JPG;PNG;BMP;SVG;GIF");
options.setOutputStream(output);
renderTask.setRenderOption(options);
renderTask.render();</pre>
Now the pagenumbers are calculated correclty. By my image isnt't displayed. I get the above exception.<br />
I put an image in the masterpage in a rptlibrary. The image file is an "image file in shared resources" (a jpg in the same directory as the rptlibrary).<br />
<br />
I also tried an own IResourceLocator - this resourcelocator is called correctly when rptlibrary is searched, but there's no call for my image.<br />
<br />
Setting an imagehandler (options.setImageHandler(myownimagehandler)) has no effect - none of the methods are called.<br />
<br />
What shall I do?<br />
Any suggestions?
Find more posts tagged with
Comments
JasonW
Are you setting the resource folder? I just tried a test with a library with an image in the master page header. The image was in the resource folder with the library. I used the following code and it worked fine:
IReportEngine engine=null;
EngineConfig config = null;
IReportDocument document = null;
try{
//System.setProperty("java.io.tmpdir", "c:/temp/test/testsampledb");
config = new EngineConfig( );
config.setResourcePath("C:\\work\\workspaces\\2.6.1workspaces\\BIRT -DE RE API Samples\\APIs\\Reports");
config.setBIRTHome("C:\\birt\\birt-runtime-2_6_1\\birt-runtime-2_6_1\\ReportEngine");
config.setLogConfig(null, Level.FINEST);
Platform.startup( config );
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
engine = factory.createReportEngine( config );
IReportRunnable design = null;
//Open the report design
design = engine.openReportDesign("Reports/usemasterpagelib.rptdesign");
ReportDesignHandle rdh = (ReportDesignHandle)design.getDesignHandle();
IRunTask task = engine.createRunTask(design);
task.run("output/resample/usemaster.rptdocument");
task.close();
document = engine.openReportDocument("output/resample/usemaster.rptdocument");
IRenderOption options = new RenderOption();
options.setOutputFormat("pdf");
options.setOutputFileName("output/resample/usemaster.pdf");
PDFRenderOption pdfOptions = new PDFRenderOption( options );
pdfOptions.setOutputFileName("output/resample/usemaster.pdf");
pdfOptions.setOutputFormat("pdf");
IRenderTask rtask = engine.createRenderTask(document);
rtask.setRenderOption(options);
rtask.render();
rtask.close();
document.close();
engine.destroy();
System.out.println("Finished");
}catch( Exception ex){
ex.printStackTrace();
}
finally
{
Platform.shutdown( );
System.out.println("Finished");
}
Attached is the design and libaray with image.
Jason
gernot
<blockquote class='ipsBlockquote' data-author="'JasonW'" data-cid="75527" data-time="1302062403" data-date="05 April 2011 - 09:00 PM"><p>
Are you setting the resource folder? I just tried a test with a library with an image in the master page header. The image was in the resource folder with the library.<br /></p></blockquote>
<br />
Thank you for you answer - if I set the resource folder, the images are displayed correctly!<br />
BUT config.setResourcePath() is an engine setting not a report setting. My additional problem is:<br />
I've serveral webapps running on my servletengine. To reduce resource consumption BIRT is only started once on the servletengine and NOT in each webapp.<br />
Each webapp has it's own (customized) rptlibraries and therefore it's own resourcelibrary. So I've a problem setting resource folder at engine level.<br />
<br />
Is there a possibility setting resource folder at report level?<br />
<br />
I have an own IResourceLocator when opening rptdesign I pass an instance of this IResourceLocator<br />
IReportRunnable report = openReportDesign(rptdesign,resourceLocator);<br />
and runAndRender works perfectly (all images are shown correctly).<br />
<br />
I have not seen a possibilty to pass an IResourceLocator to run-task.<br />
How can I set IResourceLocator for run-task?
JasonW
One way to do this that I just tried and it works is I created a local class called MyResourceLocator. I used the default resource locator class and made a few changes to it. ResourceLocatorImplBase.java is what I started with. I added a local variable called myresource folder and added getter and setter methods for it. I then modified this method: private URL getResource( ModuleHandle moduleHandle, String fileName )
I commented out
//String resourcePath = moduleHandle.getModule( ).getSession( )
// .getResourceFolder( );
//if ( StringUtil.isBlank( resourcePath ) )
// resourcePath = moduleHandle.getResourceFolder( );
and added
String resourcePath = this.getMyresourcepath();
In my code I then did this:
IReportRunnable design = null;
//Open the report design
MyResourceLocator mrl = new MyResourceLocator();
mrl.setMyresourcepath("C:\\work\\Samples\\APIs\\resources");
design = engine.openReportDesign("Reports/usemasterpagelib.rptdesign", mrl);
IRunTask task = engine.createRunTask(design);
task.run("output/resample/usemaster.rptdocument");
Attached is the modified class.
Jason
gernot
Thanks Jason - but it doesn't work :-(<br />
<br />
I already have an own IResourceLocator, but I also tried your IResourceLocator - both doesn't work and have the same behaviour.<br />
<br />
Both are working correctly when using "runAndRender()"<br />
But when using separate "run()" and "render()", both don't work.<br />
<br />
Debugger shows following:<br />
Using "runAndRender()" following files are searched with "findResource()" in IResourceLocator:<br />
<ul class='bbc'><li>rptlibrary1</li><li>rptlibrary2</li><li>...</li><li>jpg1</li><li>jpg2</li><li>...</li></ul>
<br />
BUT when using "run()" and "render()" separately only rptlibrary1 to rptlibraryN are searched with "findResource()" in IResourceLocator - <br />
"findResource()" is NOT called for jpgs or other images!
JasonW
I can reproduce the problem now. I will look into what the issue is.
Jason
gernot
<blockquote class='ipsBlockquote' data-author="'JasonW'" data-cid="75709" data-time="1302517771" data-date="11 April 2011 - 03:29 AM"><p>
I can reproduce the problem now. I will look into what the issue is.<br />
<br />
Jason<br /></p></blockquote>
<br />
Have you found the issue? - Do you have a workaround?
JasonW
I believe I got this to work using module options when opening the report document:
Map moptions = new HashMap( );
moptions.put( IModuleOption.RESOURCE_FOLDER_KEY,
"C:/test93/APIs/resource1" );
moptions.put( IModuleOption.PARSER_SEMANTIC_CHECK_KEY, Boolean.FALSE );
document = engine.openReportDocument("Reports/usemasterpagelib.rptdesign","output/resample/usemaster1.rptdocument", moptions1);
I tested this with BIRT 3.7.2. You should be able to give a try with earlier version:
package REAPI;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import org.eclipse.birt.report.*;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.EngineConstants;
import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLActionHandler;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IPDFRenderOption;
import org.eclipse.birt.report.engine.api.IRenderOption;
import org.eclipse.birt.report.engine.api.IRenderTask;
import org.eclipse.birt.report.engine.api.IReportDocument;
import org.eclipse.birt.report.engine.api.IRunTask;
import org.eclipse.birt.report.engine.api.PDFRenderOption;
import org.eclipse.birt.report.engine.api.HTMLServerImageHandler;
import org.eclipse.birt.report.engine.api.HTMLCompleteImageHandler;
import org.eclipse.birt.report.engine.api.IGetParameterDefinitionTask;
import org.eclipse.birt.report.engine.api.IParameterSelectionChoice;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.engine.api.RenderOption;
import org.eclipse.birt.report.engine.api.IParameterDefnBase;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.IModuleOption;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.SlotHandle;
public class RunAndRenderTaskUseLib {
public void runReport() throws EngineException
{
IReportEngine engine=null;
EngineConfig config = null;
IReportDocument document = null;
try{
//System.setProperty("java.io.tmpdir", "c:/temp/test/testsampledb");
config = new EngineConfig( );
config.setResourcePath("C:/test93/APIs/resource2");
config.setLogConfig(null, Level.FINEST);
Platform.startup( config );
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
engine = factory.createReportEngine( config );
IReportRunnable design = null;
Map moptions = new HashMap( );
moptions.put( IModuleOption.RESOURCE_FOLDER_KEY,
"C:/test93/APIs/resource1" );
moptions.put( IModuleOption.PARSER_SEMANTIC_CHECK_KEY, Boolean.FALSE );
Map moptions2 = new HashMap( );
moptions2.put( IModuleOption.RESOURCE_FOLDER_KEY,
"C:/test93/APIs/resource2" );
moptions2.put( IModuleOption.PARSER_SEMANTIC_CHECK_KEY, Boolean.FALSE );
design = engine.openReportDesign("Reports/usemasterpagelib.rptdesign");//, mrl1);
//design = engine.openReportDesign("Reports/usemasterpagelib.rptdesign");
ReportDesignHandle rdh = (ReportDesignHandle)design.getDesignHandle();
IRunTask task = engine.createRunTask(design);
task.setLocale(new Locale("en", "US"));
//task.getAppContext().put("birt.viewer.resource.path", "C:/test93/APIs/resource2");
task.run("output/resample/usemaster1.rptdocument");
task.close();
document = engine.openReportDocument("Reports/usemasterpagelib.rptdesign","output/resample/usemaster1.rptdocument", moptions);
PDFRenderOption options = new PDFRenderOption();
options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");
options.setOutputFileName("output/resample/usemaster1.pdf");
options.setOutputFormat("pdf");
IRenderTask rtask = engine.createRenderTask(document);
rtask.setLocale(new Locale("en", "US"));
rtask.setRenderOption(options);
rtask.render();
rtask.close();
document.close();
design = engine.openReportDesign("Reports/usemasterpagelib.rptdesign");//, mrl2);
//design = engine.openReportDesign("Reports/usemasterpagelib.rptdesign");
task = engine.createRunTask(design);
task.setLocale(new Locale("en", "US"));
//task.getAppContext().put("birt.viewer.resource.path", "/C:/test93/APIs/resource1");
task.run("output/resample/usemaster2.rptdocument");
task.close();
document = engine.openReportDocument("Reports/usemasterpagelib.rptdesign","output/resample/usemaster2.rptdocument", moptions2);
options = new PDFRenderOption();
options.setSupportedImageFormats("PNG;GIF;JPG;BMP;SWF;SVG");
options.setOutputFileName("output/resample/usemaster2.pdf");
options.setOutputFormat("pdf");
rtask = engine.createRenderTask(document);
rtask.setLocale(new Locale("en", "US"));
rtask.setRenderOption(options);
rtask.render();
rtask.close();
document.close();
engine.destroy();
System.out.println("Finished");
}catch( Exception ex){
ex.printStackTrace();
}
finally
{
Platform.shutdown( );
System.out.println("Finished");
}
}
/**
*
@param
args
*/
public static void main(String[] args) {
try
{
RunAndRenderTaskUseLib ex = new RunAndRenderTaskUseLib( );
ex.runReport();
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
Jason
gernot
Many thanks!! Now it works!
gernot
JasonW
Thanks for updating.
Jason
donino
Hi,
I'm not sure i understand this example, because the resource path is set just before the render step. It works for images, but what happens if the report makes use of a dataset,a parameter or a file defined in a .rptlibrary? Is there a way to set a resource path before generating the rptdocument?
Thanks!