Rsvg does not display related images

I use python rsvg to render the svg image to cairo and save to a file, which basically works. But if the svg file contains a linked image, for example:

 <image href="static/usrimgs/tmpDtIKpx.png" x="10" y="10" width="600px" height="400px"></image> 

the image is not displayed in the final file (the rest of svg is displayed just fine). The relative path is correct, based on where the script is running, but I assume there is some kind of problem with the fact that this is usually a relative URL, not a relative path to the file. How do I get around this?

+5
svg librsvg
source share
4 answers

The answer was to process the links, to start with file:/// and be the complete absolute path.

+2
source share

For LibRsvg, many factors must be satisfied that allow you to load an image resource. Starting with version 2.4, they include:

  • image must have xlink:href attribute
  • this attribute must contain a full, valid URI
  • the circuit may be data:// or file://
  • file:// paths must be absolute
  • file:// paths must be in or below the path of the original SVG file (after any symbolic links have been dereferenced)

Note that if the input is passed to rsvg-convert via stdin, then no paths are considered subdirectories of the input file and all file:// images.

 rsvg-convert < input.svg > output.png # images denied rsvg-convert -o output.png input.svg # images allowed 

The code determining the resolution of the image URL can be found in the LibRsvg source in rsvg-base.c , function _rsvg_handle_allow_load .

To add a debug notification to rsvg-convert if the image fails to load, you can add

 #define G_ENABLE_DEBUG 

to config.h in the source and recompile.

+11
source share

It looks like if the file you include is not on the same path or under the SVG file you want to convert, it will not find it. I consider this a mistake.

+2
source share

I wanted librsvg image rendering to work on my Mac to speed up SVG rendering. Ian McKinnon has a great answer with all the ingredients, but implementing changes in the library is complex. These steps made librsvg correctly display related images with relative paths. Hope this saves you a little time.

First I installed librsvg using:

 brew install --build-from-source librsvg 

At the time of this writing for installation, this installs version 2.40.13 and the required libraries. Then I downloaded and extracted the source archive into my home directory:

 wget https://download.gnome.org/sources/librsvg/2.40/librsvg-2.40.13.tar.xz tar jxvf librsvg-2.40.13.tar.xz cd librsvg-2.40.13 

I edited the _rsvg_handle_allow_load function in rsvg-base.c in this directory to get around the limitations of loading the path by adding this code to line 2275:

 2275 2276 goto allow; // Just try and load it! 2277 

I also needed to edit the rsvg_cairo_surface_new_from_href function in rsvg-image.c and stop its loading using mime types - just replace the function as follows:

 55 if (mime_type) { 56 // loader = gdk_pixbuf_loader_new_with_mime_type (mime_type, error); // This doesn't work on my mac 57 loader = gdk_pixbuf_loader_new (); // This works 58 } else { 59 loader = gdk_pixbuf_loader_new (); 60 } 

I needed to use these slightly modified commands to compile and install the modified library:

 make clean make install gdk_pixbuf_binarydir=/usr/local/Cellar/librsvg/2.40.13/lib/gdk-pixbuf-2.0/2.10.0/loaders gdk_pixbuf_moduledir=/usr/local/Cellar/librsvg/2.40.13/lib/gdk-pixbuf-2.0/2.10.0/loaders 

Depending on your system, you may need to add sudo to the commands above.

Once this has been done, I can display the relative SVG links using the rsvg-convert command-line rsvg-convert installed with librsvg:

 rsvg-convert test.svg -o test.png 

I was also able to use ImageMagick to convert SVG with relative links to images in PNG files if I installed it after installing librsvg as follows:

 convert test.svg test.png 

This will allow you to test the rsvg function and performance - I found that it was 2-3 times faster than Inkscape for my application. I recommend changing the code more intelligently if you use it in a production environment.

+2
source share

All Articles