Class SvgConverter


  • public final class SvgConverter
    extends java.lang.Object
    This is the main container class for static methods that do high-level conversion operations from input to PDF, either by drawing on a canvas or by returning an XObject, which can then be used by the calling class for further processing and drawing operations.
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
    • Constructor Detail

      • SvgConverter

        private SvgConverter()
    • Method Detail

      • checkNull

        private static void checkNull​(java.lang.Object o)
      • drawOnDocument

        public static void drawOnDocument​(java.lang.String content,
                                          PdfDocument document,
                                          int pageNo)
        Draws a String containing valid SVG to a document, on a given page number at the origin of the page.
        Parameters:
        content - the String value containing valid SVG content
        document - the PdfDocument instance to draw on
        pageNo - the page to draw on
      • drawOnDocument

        public static void drawOnDocument​(java.lang.String content,
                                          PdfDocument document,
                                          int pageNo,
                                          float x,
                                          float y)
        Draws a String containing valid SVG to a document, on a given page number on the provided x and y coordinate.
        Parameters:
        content - the String value containing valid SVG content
        document - the PdfDocument instance to draw on
        pageNo - the page to draw on
        x - x-coordinate of the location to draw at
        y - y-coordinate of the location to draw at
      • drawOnDocument

        public static void drawOnDocument​(java.lang.String content,
                                          PdfDocument document,
                                          int pageNo,
                                          ISvgConverterProperties props)
        Draws a String containing valid SVG to a document, on a given page number on the provided x and y coordinate.
        Parameters:
        content - the String value containing valid SVG content
        document - the PdfDocument instance to draw on
        pageNo - the page to draw on
        props - a container for extra properties that customize the behavior
      • drawOnDocument

        public static void drawOnDocument​(java.lang.String content,
                                          PdfDocument document,
                                          int pageNo,
                                          float x,
                                          float y,
                                          ISvgConverterProperties props)
        Draws a String containing valid SVG to a document, on a given page number on the provided x and y coordinate.
        Parameters:
        content - the String value containing valid SVG content
        document - the PdfDocument instance to draw on
        pageNo - the page to draw on
        x - x-coordinate of the location to draw at
        y - y-coordinate of the location to draw at
        props - a container for extra properties that customize the behavior
      • drawOnDocument

        public static void drawOnDocument​(java.io.InputStream stream,
                                          PdfDocument document,
                                          int pageNo)
                                   throws java.io.IOException
        Draws a Stream containing valid SVG to a document, on a given page number ate the origni of the page.
        Parameters:
        stream - the Stream containing valid SVG content
        document - the PdfDocument instance to draw on
        pageNo - the page to draw on
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • drawOnDocument

        public static void drawOnDocument​(java.io.InputStream stream,
                                          PdfDocument document,
                                          int pageNo,
                                          float x,
                                          float y)
                                   throws java.io.IOException
        Draws a Stream containing valid SVG to a document, on a given page number on the provided x and y coordinate.
        Parameters:
        stream - the Stream containing valid SVG content
        document - the PdfDocument instance to draw on
        pageNo - the page to draw on
        x - x-coordinate of the location to draw at
        y - y-coordinate of the location to draw at
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • drawOnDocument

        public static void drawOnDocument​(java.io.InputStream stream,
                                          PdfDocument document,
                                          int pageNo,
                                          ISvgConverterProperties props)
                                   throws java.io.IOException
        Draws a Stream containing valid SVG to a document, on a given page number on the provided x and y coordinate.
        Parameters:
        stream - the Stream containing valid SVG content
        document - the PdfDocument instance to draw on
        pageNo - the page to draw on
        props - a container for extra properties that customize the behavior
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • drawOnDocument

        public static void drawOnDocument​(java.io.InputStream stream,
                                          PdfDocument document,
                                          int pageNo,
                                          float x,
                                          float y,
                                          ISvgConverterProperties props)
                                   throws java.io.IOException
        Draws a Stream containing valid SVG to a document, on a given page number on the provided x and y coordinate.
        Parameters:
        stream - the Stream containing valid SVG content
        document - the PdfDocument instance to draw on
        pageNo - the page to draw on
        x - x-coordinate of the location to draw at
        y - y-coordinate of the location to draw at
        props - a container for extra properties that customize the behavior
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • drawOnPage

        public static void drawOnPage​(java.lang.String content,
                                      PdfPage page)
        Draws a String containing valid SVG to a given page at the origin of the page.
        Parameters:
        content - the String value containing valid SVG content
        page - the PdfPage instance to draw on
      • drawOnPage

        public static void drawOnPage​(java.lang.String content,
                                      PdfPage page,
                                      float x,
                                      float y)
        Draws a String containing valid SVG to a given page on the provided x and y coordinate.
        Parameters:
        content - the String value containing valid SVG content
        page - the PdfPage instance to draw on
        x - x-coordinate of the location to draw at
        y - y-coordinate of the location to draw at
      • drawOnPage

        public static void drawOnPage​(java.lang.String content,
                                      PdfPage page,
                                      ISvgConverterProperties props)
        Draws a String containing valid SVG to a given page on the provided x and y coordinate.
        Parameters:
        content - the String value containing valid SVG content
        page - the PdfPage instance to draw on
        props - a container for extra properties that customize the behavior
      • drawOnPage

        public static void drawOnPage​(java.lang.String content,
                                      PdfPage page,
                                      float x,
                                      float y,
                                      ISvgConverterProperties props)
        Draws a String containing valid SVG to a given page on the provided x and y coordinate.
        Parameters:
        content - the String value containing valid SVG content
        page - the PdfPage instance to draw on
        x - x-coordinate of the location to draw at
        y - y-coordinate of the location to draw at
        props - a container for extra properties that customize the behavior
      • drawOnPage

        public static void drawOnPage​(java.io.InputStream stream,
                                      PdfPage page)
                               throws java.io.IOException
        Draws a Stream containing valid SVG to a given page at coordinate 0,0.
        Parameters:
        stream - the Stream object containing valid SVG content
        page - the PdfPage instance to draw on
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • drawOnPage

        public static void drawOnPage​(java.io.InputStream stream,
                                      PdfPage page,
                                      float x,
                                      float y)
                               throws java.io.IOException
        Draws a Stream containing valid SVG to a given page, at a given location.
        Parameters:
        stream - the Stream object containing valid SVG content
        page - the PdfPage instance to draw on
        x - x-coordinate of the location to draw at
        y - y-coordinate of the location to draw at
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • drawOnPage

        public static void drawOnPage​(java.io.InputStream stream,
                                      PdfPage page,
                                      ISvgConverterProperties props)
                               throws java.io.IOException
        Draws a Stream containing valid SVG to a given page at a given location.
        Parameters:
        stream - the Stream object containing valid SVG content
        page - the PdfPage instance to draw on
        props - a container for extra properties that customize the behavior
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • drawOnPage

        public static void drawOnPage​(java.io.InputStream stream,
                                      PdfPage page,
                                      float x,
                                      float y,
                                      ISvgConverterProperties props)
                               throws java.io.IOException
        Draws a Stream containing valid SVG to a given page at a given location.
        Parameters:
        stream - the Stream object containing valid SVG content
        page - the PdfPage instance to draw on
        x - x-coordinate of the location to draw at
        y - y-coordinate of the location to draw at
        props - a container for extra properties that customize the behavior
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • drawOnCanvas

        public static void drawOnCanvas​(java.lang.String content,
                                        PdfCanvas canvas)
        Draws a String containing valid SVG to a pre-made canvas object.
        Parameters:
        content - the String value containing valid SVG content
        canvas - the PdfCanvas instance to draw on
      • drawOnCanvas

        public static void drawOnCanvas​(java.lang.String content,
                                        PdfCanvas canvas,
                                        float x,
                                        float y)
        Draws a String containing valid SVG to a pre-made canvas object.
        Parameters:
        content - the String value containing valid SVG content
        canvas - the PdfCanvas instance to draw on
        x - x-coordinate of the location to draw at
        y - y-coordinate of the location to draw at
      • drawOnCanvas

        public static void drawOnCanvas​(java.lang.String content,
                                        PdfCanvas canvas,
                                        ISvgConverterProperties props)
        Draws a String containing valid SVG to a pre-made canvas object.
        Parameters:
        content - the String value containing valid SVG content
        canvas - the PdfCanvas instance to draw on
        props - a container for extra properties that customize the behavior
      • drawOnCanvas

        public static void drawOnCanvas​(java.lang.String content,
                                        PdfCanvas canvas,
                                        float x,
                                        float y,
                                        ISvgConverterProperties props)
        draws a String containing valid SVG to a pre-made canvas object, at a specified location.
        Parameters:
        content - the String value containing valid SVG content
        canvas - the PdfCanvas instance to draw on
        x - x-coordinate of the location to draw at
        y - y-coordinate of the location to draw at
        props - a container for extra properties that customize the behavior
      • drawOnCanvas

        public static void drawOnCanvas​(java.io.InputStream stream,
                                        PdfCanvas canvas)
                                 throws java.io.IOException
        Draws a Stream containing valid SVG to a pre-made canvas object.
        Parameters:
        stream - the Stream object containing valid SVG content
        canvas - the PdfCanvas instance to draw on
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • drawOnCanvas

        public static void drawOnCanvas​(java.io.InputStream stream,
                                        PdfCanvas canvas,
                                        float x,
                                        float y)
                                 throws java.io.IOException
        Draws a Stream containing valid SVG to a pre-made canvas object, to a specified location.
        Parameters:
        stream - the Stream object containing valid SVG content
        canvas - the PdfCanvas instance to draw on
        x - x-coordinate of the location to draw at
        y - y-coordinate of the location to draw at
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • drawOnCanvas

        public static void drawOnCanvas​(java.io.InputStream stream,
                                        PdfCanvas canvas,
                                        ISvgConverterProperties props)
                                 throws java.io.IOException
        Draws a Stream containing valid SVG to a pre-made canvas object.
        Parameters:
        stream - the Stream object containing valid SVG content
        canvas - the PdfCanvas instance to draw on
        props - a container for extra properties that customize the behavior
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • drawOnCanvas

        public static void drawOnCanvas​(java.io.InputStream stream,
                                        PdfCanvas canvas,
                                        float x,
                                        float y,
                                        ISvgConverterProperties props)
                                 throws java.io.IOException
        Draws a String containing valid SVG to a pre-made canvas object, at a specified position on the canvas.
        Parameters:
        stream - the Stream object containing valid SVG content
        canvas - the PdfCanvas instance to draw on
        x - x-coordinate of the location to draw at
        y - y-coordinate of the location to draw at
        props - a container for extra properties that customize the behavior
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • createPdf

        public static void createPdf​(java.io.File svgFile,
                                     java.io.File pdfFile)
                              throws java.io.IOException
        Converts SVG stored in a File to a PDF File.
        Parameters:
        svgFile - the File containing the source SVG
        pdfFile - the File containing the resulting PDF
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • createPdf

        public static void createPdf​(java.io.File svgFile,
                                     java.io.File pdfFile,
                                     ISvgConverterProperties props)
                              throws java.io.IOException
        Converts SVG stored in a File to a PDF File, using specific ISvgConverterProperties.
        Parameters:
        svgFile - the File containing the source SVG
        pdfFile - the File containing the resulting PDF
        props - a ISvgConverterProperties an instance for extra properties to customize the behavior
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • createPdf

        public static void createPdf​(java.io.File svgFile,
                                     java.io.File pdfFile,
                                     WriterProperties writerProps)
                              throws java.io.IOException
        Converts SVG stored in a File to a PDF File, using WriterProperties
        Parameters:
        svgFile - the File containing the source SVG
        pdfFile - the File containing the resulting PDF
        writerProps - theWriterProperties for the pdf document
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • createPdf

        public static void createPdf​(java.io.File svgFile,
                                     java.io.File pdfFile,
                                     ISvgConverterProperties props,
                                     WriterProperties writerProps)
                              throws java.io.IOException
        Converts SVG stored in a File to a PDF File, using specific ISvgConverterProperties and WriterProperties.
        Parameters:
        svgFile - the File containing the source SVG
        pdfFile - the File containing the resulting PDF
        props - a ISvgConverterProperties an instance for extra properties to customize the behavior
        writerProps - a WriterProperties for the pdf document
        Throws:
        java.io.IOException - Signals that an I/O exception has occurred.
      • convertToSvgConverterProps

        private static SvgConverterProperties convertToSvgConverterProps​(ISvgConverterProperties props,
                                                                         java.lang.String baseUri)
        Copies properties from custom ISvgConverterProperties into new SvgConverterProperties. Since ISvgConverterProperties itself is immutable we have to do it.
        Parameters:
        props - ISvgConverterProperties an instance for extra properties to customize the behavior
        baseUri - the directory of new SvgConverterProperties
        Returns:
        new SvgConverterProperties.
      • createPdf

        public static void createPdf​(java.io.InputStream svgStream,
                                     java.io.OutputStream pdfDest)
                              throws java.io.IOException
        Create a single page pdf containing the SVG on its page using the default processing and drawing logic
        Parameters:
        svgStream - Stream containing the SVG
        pdfDest - PDF destination outputStream
        Throws:
        java.io.IOException - when the one of the streams cannot be read correctly
      • createPdf

        public static void createPdf​(java.io.InputStream svgStream,
                                     java.io.OutputStream pdfDest,
                                     WriterProperties writerProps)
                              throws java.io.IOException
        Create a single page pdf containing the SVG on its page using the default processing and drawing logic
        Parameters:
        svgStream - Stream containing the SVG
        pdfDest - PDF destination outputStream
        writerProps - writer properties for the pdf document
        Throws:
        java.io.IOException - when the one of the streams cannot be read correctly
      • createPdf

        public static void createPdf​(java.io.InputStream svgStream,
                                     java.io.OutputStream pdfDest,
                                     ISvgConverterProperties props)
                              throws java.io.IOException
        Create a single page pdf containing the SVG on its page using the default processing and drawing logic
        Parameters:
        svgStream - Stream containing the SVG
        pdfDest - PDF destination outputStream
        props - ISvgConverterProperties an instance for extra properties to customize the behavior
        Throws:
        java.io.IOException - when the one of the streams cannot be read correctly
      • createPdf

        public static void createPdf​(java.io.InputStream svgStream,
                                     java.io.OutputStream pdfDest,
                                     ISvgConverterProperties props,
                                     WriterProperties writerProps)
                              throws java.io.IOException
        Create a single page pdf containing the SVG on its page using the default processing and drawing logic
        Parameters:
        svgStream - Stream containing the SVG
        pdfDest - PDF destination outputStream
        props - ISvgConverterProperties an instance for extra properties to customize the behavior
        writerProps - WriterProperties for the pdf document
        Throws:
        java.io.IOException - when the one of the streams cannot be read correctly
      • parseAndProcess

        public static ISvgProcessorResult parseAndProcess​(java.io.InputStream svgStream)
        Parse and process an Inputstream containing an SVG, using the default Svg processor (DefaultSvgProcessor) The parsing of the stream is done using UTF-8 as the default charset. The properties used by the processor are the SvgConverterProperties
        Parameters:
        svgStream - Stream containing the SVG to parse and process
        Returns:
        ISvgProcessorResult containing the root renderer and metadata of the svg
      • process

        public static ISvgProcessorResult process​(INode root,
                                                  ISvgConverterProperties props)
        Use the default implementation of ISvgProcessor to convert an XML DOM tree to a node renderer tree. The passed properties can modify the default behaviour
        Parameters:
        root - the XML DOM tree
        props - ISvgConverterProperties an instance for extra properties to customize the behavior
        Returns:
        a node renderer tree corresponding to the passed XML DOM tree
      • parse

        public static INode parse​(java.lang.String content)
        Parse a String containing valid SVG into an XML DOM node, using the default JSoup XML parser.
        Parameters:
        content - the String value containing valid SVG content
        Returns:
        an XML DOM tree corresponding to the passed String input
      • parse

        public static INode parse​(java.io.InputStream stream)
                           throws java.io.IOException
        Parse a Stream containing valid SVG into an XML DOM node, using the default JSoup XML parser. This method will assume that the encoding of the Stream is UTF-8.
        Parameters:
        stream - the Stream containing valid SVG content
        Returns:
        an XML DOM tree corresponding to the passed String input
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • parse

        public static INode parse​(java.io.InputStream stream,
                                  ISvgConverterProperties props)
                           throws java.io.IOException
        Parse a Stream containing valid SVG into an XML DOM node, using the default JSoup XML parser. This method will assume that the encoding of the Stream is UTF-8, unless specified otherwise by the method ISvgConverterProperties.getCharset() of the props parameter.
        Parameters:
        stream - the Stream containing valid SVG content
        props - ISvgConverterProperties an instance for extra properties to customize the behavior
        Returns:
        an XML DOM tree corresponding to the passed String input
        Throws:
        java.io.IOException - when the Stream cannot be read correctly
      • extractWidthAndHeight

        public static float[] extractWidthAndHeight​(ISvgNodeRenderer topSvgRenderer)
        Extract width and height of the passed SVGNodeRenderer, defaulting to respective viewbox values if either one is not present or to browser default if viewbox is missing as well
        Parameters:
        topSvgRenderer - the ISvgNodeRenderer instance that contains the renderer tree
        Returns:
        float[2], width is in position 0, height in position 1