Tim, you might want to test the PDF transcoder that is bundled with
Apache Batik. Or to get the latest version of the PDF transcoder, just
put the latest Apache FOP in the classpath instead of pdf-transcoder.jar.
That has the benefit that you don't have to care much about the scaling
problem and you get text as text (and not text converted to shapes) and
nice-looking gradients among other things.

On 11.02.2011 00:17:06 Tim Heuer wrote:
> Hi List,
> 
> I came across a problem with the SVG rendering onto a PDF:
> 
> The units seem to be wrong or I am doing a mistake.
> 
> When I let Batik parse my SVG image:
> <?xml version="1.0" encoding="UTF-8"?>
> <svg xmlns="http://www.w3.org/2000/svg"; 
> xmlns:xlink="http://www.w3.org/1999/xlink"; width="126pt" height="25pt" 
> viewBox="0 0 126 25" version="1.1">
> <g id="surface2">
> <rect x="0" y="0" width="126" height="25" 
> style="fill:rgb(100%,100%,100%);fill-opacity:1;stroke:none;"/>
> <path style=" 
> stroke:none;fill-rule:evenodd;fill:rgb(81.176471%,85.098039%,69.411765%);fill-opacity:1;"
>  d="M 5.5 5.5 L 34.5 5.5 L 34.5 19.5 L 5.5 19.5 Z M 5.5 5.5 "/>
> ...
> </g>
> </svg>
> 
> 
> My Java code is as follows:
> 
>                         PdfContentByte dc = context.getDirectContent();
>                         URI uri = URI.create(iconItem);
>                         URL url = uri.toURL();
>                         SVGDocumentFactory factory = new 
> SAXSVGDocumentFactory(XMLResourceDescriptor.getXMLParserClassName());
>                               UserAgent userAgent = new UserAgentAdapter();
>                             DocumentLoader loader = new 
> DocumentLoader(userAgent);
>                             BridgeContext ctx = new BridgeContext(userAgent, 
> loader);
>                           ctx.setDynamicState(BridgeContext.DYNAMIC);
>                           SVGDocument svgDoc = 
> factory.createSVGDocument(null, url.openStream());
>                           GVTBuilder builder = new GVTBuilder();
>                           GraphicsNode graphics = builder.build(ctx, svgDoc);
>                               String svgWidthString = 
> svgDoc.getDocumentElement().getAttribute("width");
>                               String svgHeightString = 
> svgDoc.getDocumentElement().getAttribute("height");
>                               float svgWidth = 
> Float.valueOf(svgWidthString.substring(0, svgWidthString.length() - 2));
>                               float svgHeight = 
> Float.valueOf(svgHeightString.substring(0, svgHeightString.length() - 2));
>                         PdfTemplate map = dc.createTemplate(svgWidth*1.25f, 
> svgHeight*1.25f);
>                               //PdfTemplate map = dc.createTemplate(svgWidth, 
> svgHeight);
>                         Graphics2D g2d = map.createGraphics(svgWidth*1.25f, 
> svgHeight*1.25f);
>                         graphics.paint(g2d);
>                         g2d.dispose();
>                         System.out.println("w="+ svgWidth +" h="+ svgHeight 
> +" maxW="+ maxIconWidth +" maxH="+ maxIconHeight);
>                         //...
>                         Image image = Image.getInstance(map);
> PdfPCell cell = new PdfPCell(...);
> cell.add(image);
> 
> I looked up the SVG specifications and found that pt is 1.25 pixels. However, 
> this renders wrongly on the PDF. The image becomes too big and is cut off. 
> When I enter 1.5f instead of 1.25f above, it seems more correct but still a 
> bit out.
> 
> I don't want to hack together a number that will just happen to look right, 
> because it is the wrong approach. Is there some kind of trickiness with the 
> units between SVG and PDF (using iText)?
> 
> Help is much appreciated. I am working on the legends' rendering in SVG in 
> the MapFish print module and will submit the source back to the project, so I 
> think they would appreciate your input too.
> 
> Kind regards,
> Tim
> 
> ________________________________
> Please consider the environment before printing this email
> Warning: This electronic message together with any attachments is 
> confidential. If you receive it in error: (i) you must not read, use, 
> disclose, copy or retain it; (ii) please contact the sender immediately by 
> reply email and then delete the emails.
> The views expressed in this email may not be those of Landcare Research New 
> Zealand Limited. http://www.landcareresearch.co.nz




Jeremias Maerki


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscr...@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-h...@xmlgraphics.apache.org

Reply via email to