Jump to content
We've recently updated our Privacy Statement, available here ×

Embedding Flash and SVG into Jasper Report


singhipst

Recommended Posts

Hi All,

I am not sure whether it is possible in Jasper Report or not.

I wanted to embed SWF(Flash) file and SVG file in my jasper report using jasper API. Similarly like we embed JPG and GIF files in our report for logo.

Please let me know if anybody has tried this.

Best Regards:

Ritesh Kumar

Link to comment
Share on other sites

  • Replies 11
  • Created
  • Last Reply

Top Posters In This Topic

I'm not a Batik expert either, this is just some code I grabbed from the Batik mailing lists. You could give it a try and see whether you like the results.

The Batik image renderer looks like

Code:
package jr.util;import java.awt.Graphics2D;import java.awt.geom.AffineTransform;import java.awt.geom.Rectangle2D;import java.io.File;import java.io.IOException;import net.sf.jasperreports.engine.JRAbstractSvgRenderer;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JRRuntimeException;import org.apache.batik.bridge.BridgeContext;import org.apache.batik.bridge.DocumentLoader;import org.apache.batik.bridge.GVTBuilder;import org.apache.batik.bridge.UserAgent;import org.apache.batik.bridge.UserAgentAdapter;import org.apache.batik.bridge.ViewBox;import org.apache.batik.gvt.GraphicsNode;import org.w3c.dom.svg.SVGDocument;public class BatikRenderer extends JRAbstractSvgRenderer{    private static final long serialVersionUID = 1L;    private final String svgURI;	    public BatikRenderer(String svgURI)    {	this.svgURI = svgURI;    }	    public BatikRenderer(File svgFile)    {	this(svgFile.toURI().toString());    }	    public void render(Graphics2D grx, Rectangle2D rectangle) throws JRException    {	try	{	    UserAgent userAgent = new UserAgentAdapter();	    DocumentLoader loader = new DocumentLoader(userAgent);	    SVGDocument svgDoc = (SVGDocument) loader.loadDocument(svgURI);	    BridgeContext ctx = new BridgeContext(userAgent, loader);	    ctx.setDynamic(true);	    GVTBuilder builder = new GVTBuilder();	    GraphicsNode graphicsNode = builder.build(ctx, svgDoc);				    AffineTransform transform = ViewBox.getViewTransform(null, svgDoc.getRootElement(), 					(float) rectangle.getWidth(), (float) rectangle.getHeight(), ctx);	    Graphics2D graphics = (Graphics2D) grx.create();	    graphics.translate(rectangle.getX(), rectangle.getY());	    graphics.transform(transform);				    graphicsNode.paint(graphics);	}	catch (IOException e)	{	    throw new JRRuntimeException(e);	}    }}[/code]			


You would then use it in a report in this way:


...

new jr.util.BatikRenderer(new java.io.File("..svg file.."))




Regards,
Lucian

Link to comment
Share on other sites

Hi Lucian,

I'm working with Ritesh on this issue. I tried he Batik code to paint svg file. But, the image is rendered very minute (appears as dot). I tried to increse the height and width, but not much diffrence.
Can you please help.

Please find the JRXML and PDF enclosed.

thanks and regards,
Nandini [file name=test-dc1f41e07aee88fefd12b8e8226bf253.jrxml size=2405]
Post edited by: nandu_bc, at: 2008/02/22 12:35

Link to comment
Share on other sites

Hi Lucian,

 

Thanks for the reply. We are able to resolve this issue. The problem was that the svg file itself was corrupted. Now we are able to render a svg image on the jasper pdf using batik renderer.

 

 

Thanks and regards,

Nandini

Link to comment
Share on other sites

  • 4 weeks later...

Hi, Lucian,

We are currently using FOP and evaluating JasperReports and BIRT basically because we want to have a visual designer. I found that if a image is a SVG, the scaleImage doesn't work right. I set it to Clip, RetainShape and FillFrame, but it always looks like Clip. If the image is JPEG, it works correctly for Clip, RetainShape and FillFrame. Is this a bug? Can it be fixed or any workaround?

BTW, In the code you posted here, there are constructors for taking SVG file as parameter, but in net.sf.jasperreports.renderers.BatikRenderer class, there is not constructor to take SVG file. It is useful to have a constructor for take SVG file.

Thanks,
Dan

lucianc wrote:

I'm not a Batik expert either, this is just some code I grabbed from the Batik mailing lists. You could give it a try and see whether you like the results.

The Batik image renderer looks like

Code:

You would then use it in a report in this way:
 

     ...             new jr.util.BatikRenderer(new java.io.File("..svg file.."))     [/code]

Regards,
Lucian

Link to comment
Share on other sites

danc wrote:


I found that if a image is a SVG, the scaleImage doesn't work right. I set it to Clip, RetainShape and FillFrame, but it always looks like Clip. If the image is JPEG, it works correctly for Clip, RetainShape and FillFrame. Is this a bug? Can it be fixed or any workaround?


We could consider this as a bug, please log it
 

BTW, In the code you posted here, there are constructors for taking SVG file as parameter, but in net.sf.jasperreports.renderers.BatikRenderer class, there is not constructor to take SVG file. It is useful to have a constructor for take SVG file.


You could extend the built-in Batik renderer and add a java.io.File constructor, or post this as a enhancement request.

Regards,
Lucian

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...