You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Phil Scadden <P....@gns.cri.nz> on 2023/04/02 21:00:40 UTC

RE: Generate SVG with viewBox attribute

I am no expert at this, but looking at some old code, try:
svg.getRoot().setAttributeNS(null, "viewbox", "0 0 " + width + " " + fullHeight);

From: Aaron Weber <aa...@gmail.com>
Sent: Friday, 31 March 2023 9:57 AM
To: batik-users@xmlgraphics.apache.org
Subject: Generate SVG with viewBox attribute



CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe:
I'm trying to render a PDF page (from PDFBox) to SVG image, and in order to assist with browsers auto-scaling the resultant image, I find that the viewBox attribute should be set in the svg (root) element.  Currently using Java 8 and Batik 1.16.

I have looked at an old question in the list about this, but the mail archive seems incomplete and what I've tried from it does not work.  If I try to add the attribute before rendering the page via SVGGraphics2D, it simply does not appear in the resultant output.  If I try to add it after rendering the page, it actually ALSO does not appear, and my content of the element is also gone.

FWIW: I think this would be a great method to add to SVGGeneratorContext.

My code is as follows.  Can someone point out how to edit the XML/SVG such that the viewBox attribute is included successfully in the output?

            PDFRenderer r = new PDFRenderer(document);

            PDPage page = document.getPage(PageNumber - 1);
            PDRectangle rect = page.getMediaBox();
            int w = Math.round(rect.getWidth());
            int h = Math.round(rect.getHeight());
            String viewboxInfo = String.format("0 0 %d %d", w, h);

            String svgNS = "http://www.w3.org/2000/svg";
            DOMImplementation impl = GenericDOMImplementation.getDOMImplementation();
            Document myFactory = impl.createDocument(svgNS, "svg", null);

            SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(myFactory);
            ctx.setEmbeddedFontsOn(true);
            SVGGraphics2D g2d = new SVGGraphics2D(ctx, true);

            r.renderPageToGraphics(PageNumber - 1, g2d);

            Element root = g2d.getRoot();
            root.setAttribute("viewBox", viewboxInfo);

            try (Writer out = new OutputStreamWriter(Files.newOutputStream(new File(tgtFilePath).toPath()), "UTF-8"))
            {
                g2d.stream(out, true);
            }

Thanks.
-AJ
Notice: This email and any attachments are confidential and may not be used, published or redistributed without the prior written consent of the Institute of Geological and Nuclear Sciences Limited (GNS Science). If received in error please destroy and immediately notify GNS Science. Do not copy or disclose the contents.

Re: Generate SVG with viewBox attribute

Posted by Aaron Weber <aa...@gmail.com>.
I actually browsed the source on GitHub and found that you have to pull the
root Element after 'renderPageToGraphics()', set the Attribute on that
Element, then pass that in using the overloaded .stream(svgRoot, writer,
useCss, escaped) method to write it.  Otherwise, it will just ignore
anything you do to the element.

Thanks for the look.

On Sun, Apr 2, 2023 at 5:00 PM Phil Scadden <P....@gns.cri.nz> wrote:

> I am no expert at this, but looking at some old code, try:
>
> svg.getRoot().setAttributeNS(null, "viewbox", "0 0 " + width + " " +
> fullHeight);
>
>
>
> *From:* Aaron Weber <aa...@gmail.com>
> *Sent:* Friday, 31 March 2023 9:57 AM
> *To:* batik-users@xmlgraphics.apache.org
> *Subject:* Generate SVG with viewBox attribute
>
>
>
>
>
> *CAUTION:* This email originated from outside of the organization. Do not
> click links or open attachments unless you recognize the sender and know
> the content is safe:
>
> I'm trying to render a PDF page (from PDFBox) to SVG image, and in order
> to assist with browsers auto-scaling the resultant image, I find that the
> viewBox attribute should be set in the svg (root) element.  Currently using
> Java 8 and Batik 1.16.
>
>
>
> I have looked at an old question in the list about this, but the mail
> archive seems incomplete and what I've tried from it does not work.  If I
> try to add the attribute before rendering the page via SVGGraphics2D, it
> simply does not appear in the resultant output.  If I try to add it after
> rendering the page, it actually ALSO does not appear, and my content of the
> element is also gone.
>
>
>
> FWIW: I think this would be a great method to add to SVGGeneratorContext.
>
>
>
> My code is as follows.  Can someone point out how to edit the XML/SVG such
> that the viewBox attribute is included successfully in the output?
>
>
>
>             PDFRenderer r = new PDFRenderer(document);
>
>             PDPage page = document.getPage(PageNumber - 1);
>             PDRectangle rect = page.getMediaBox();
>             int w = Math.round(rect.getWidth());
>             int h = Math.round(rect.getHeight());
>             String viewboxInfo = String.format("0 0 %d %d", w, h);
>
>             String svgNS = "http://www.w3.org/2000/svg";
>             DOMImplementation impl =
> GenericDOMImplementation.getDOMImplementation();
>             Document myFactory = impl.createDocument(svgNS, "svg", null);
>
>             SVGGeneratorContext ctx =
> SVGGeneratorContext.createDefault(myFactory);
>             ctx.setEmbeddedFontsOn(true);
>             SVGGraphics2D g2d = new SVGGraphics2D(ctx, true);
>
>             r.renderPageToGraphics(PageNumber - 1, g2d);
>
>             Element root = g2d.getRoot();
>             root.setAttribute("viewBox", viewboxInfo);
>
>             try (Writer out = new
> OutputStreamWriter(Files.newOutputStream(new File(tgtFilePath).toPath()),
> "UTF-8"))
>             {
>                 g2d.stream(out, true);
>             }
>
>
>
> Thanks.
>
> -AJ
> Notice: This email and any attachments are confidential and may not be
> used, published or redistributed without the prior written consent of the
> Institute of Geological and Nuclear Sciences Limited (GNS Science). If
> received in error please destroy and immediately notify GNS Science. Do not
> copy or disclose the contents.
>