2006 IR Open Dicussion Posted August 27, 2006 Share Posted August 27, 2006 By: Haissam Zaky - haissam SVG in PDF 2006-07-08 11:19Hi I am kind confused and new to jasperreport, little guidance will be appreciated. Does jasperreport support SVG? I am using jasperreports-0.6.5 I am trying to create a pdf document and include svg images. I am creating the JasperDesign in my code and not thru the jrxml file. Is that possible? Thank you Error: I am getting illegal argument exception From the class PdfGraphics2D, Method private boolean drawImage (Image img, Image mask, AffineTransform xform, Color bgColor, ImageObserver obs) { My code : JRDesignExpression expression = new JRDesignExpression(); expression.setValueClass(net.sf.jasperreports.engine.JRRenderable.class); expression.setText("ImageLoader.createRendableImage("1")" ); --------- public static final JRRenderable createRendableImage (String refId){ … File svgImageFile = new File ("myImages/Orc.svg"); while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) { offset += numRead; } …. JRRenderable pxRenderer = JRImageRenderer.getInstance(bytes); pxRenderer = new JRWrappingSvgRenderer( pxRenderer, new Dimension(100, 80), null); return pxRenderer; By: Maxim - lazy-max RE: SVG in PDF 2006-07-09 03:32Answer here: https://sourceforge.net/forum/message.php?msg_id=2895251 By: Haissam Zaky - haissam RE: SVG in PDF 2006-08-13 15:36Hi Thank you very much for your reply, I did follow the link and I was able to render the SVG image in the report. In my work I needed to create the report with PNG images and other time with svg image I noticed that with the SVG image I cant control neither the stretch or the alignment of the image in the border, as a work around I modified a little the Run method in the SVGThreadRenderer class. To get the same result when I am using png or svg images. Please see below Summary of my experience if you want high quality SVG images in a pdf document, Use batik to return pdf render image (pdf document with svg inside) the file will be smaller than a pure svg and the quality is the same. And to include it in the file, just use the itext library to include the pdf (with image) as image in your final document. Cheers Haissam // added code ***************************************************** double imageActuelWidth = 0; double imageActuelHeight =0 ; NodeList nodeList = docSvg.getElementsByTagName("svg"); for (int i = 0; i < nodeList.getLength(); i++) { Node child = nodeList.item(i); // System.out.println(child.getNodeName()); System.out.println("child width: " + child.getAttributes().getNamedItem("width").getNodeValue()); imageActuelWidth = Double.valueOf(child.getAttributes().getNamedItem("width").getNodeValue()); System.out.println("child height: " + child.getAttributes().getNamedItem("height").getNodeValue()); imageActuelHeight = Double.valueOf(child.getAttributes().getNamedItem("height").getNodeValue()); } double rectangleWidth = _rectangle.getWidth(); double rectangleHeight = _rectangle.getHeight(); // we do this caluclation because // with png and not svg , when we render a // png image even if we ask for 100 x 80 pixel we will get a png image with different //dimensions // ex: a png picture w=142 , h=348 and we request an image 100p X 80 p you will get an // image with w=33 h=80, we take the ratio of the bigger between // the width and the height and multiply by the smaller if (imageActuelHeight > imageActuelWidth ){ // if 348 > 142 // adjust the desired width instead of 100 to 142 * 80/348 = 33 rectangleWidth = Math.ceil (imageActuelWidth * rectangleHeight / imageActuelHeight); } else { // else adjust the height rectangleHeight = Math.ceil (imageActuelHeight * rectangleWidth / imageActuelWidth); } // added code ***************************************************** // Ratio Rectangle2D rectBounds = gvtTree.getBounds(); double ratioX = rectangleWidth / rectBounds.getWidth(); double ratioY = rectangleHeight / rectBounds.getHeight(); double ratio = ratioX < ratioY ? ratioX : ratioY; if(ratio > 1) ratio = 1; double rectWidth = rectBounds.getWidth() * ratio; double rectHeight = rectBounds.getHeight() * ratio; // Top Left point double startX = _rectangle.getX() + (rectangleWidth - rectWidth)/2; double startY = _rectangle.getY() + (rectangleHeight - rectHeight)/2; // Save original transform AffineTransform saveAT = _graphics.getTransform(); // Perform transformation _graphics.transform(new AffineTransform(ratio, 0, 0, ratio, startX, startY)); // Render gvtTree.paint(_graphics); // Restore original transform _graphics.setTransform(saveAT); vpInput = null; vpTranscoder = null; vpFactory = null; //bufferedInputStream.close(); byteArrayInputStream.close(); } catch(Exception e) { e.printStackTrace(); } } } Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now