QGIS – Fixing Wrong Stroke Widths When Exporting SVG from QGIS to Illustrator CC

illustratorqgissvg

When I export SVG files from QGIS using the Print composer the stroke widths become a lot wider when importing into Adobe Illustrator. It seems like the stroke width in the SVG translates differently in Illustrator compared ti Inkscape.

Example

A line in QGIS with the width 0.26 mm is shown as

stroke-width="3.07087"

in the SVG file. And when I open the file in Inkscape, it's still 0.26 mm wide but when I open it in Illustrator it shows the width of stroke as 3.07087 pt.

Does anyone have an idea why Illustrator doesn't "translate" 3.0787 to the right width?

And is it a way of fixing this? I guess I could search and replace in the SVG xml file but I'd rather not..

Does it have to do with the SVG length specs?

"If a unit is not provided, the length value represents a distance in the current user coordinate system."

Edit: I have now noticed that the mm width is 0.08466669263 times the value in the SVG XML file.

Edit 2: I have noticed now that if I alter or remove the "viewbox" bit in the SVG file, Inkscape also display the line width wrong, so it seems like Illustrator doesn't understand the viewbox and therefore translates the widths wrong!?

Best Answer

When exporting to SVG, QGIS leverages Qt's QSvgGenerator in QgsComposer::exportCompositionAsSVG. Note the SVG Export warning dialog:

The SVG export function in QGIS has several problems due to bugs and deficiencies in the Qt4 svg code. In particular, there are problems with layers not being clipped to the map bounding box. If you require a vector-based output file from Qgis it is suggested that you try printing to PostScript if the SVG output is not satisfactory

With regards to opening the SVG directly in Illustrator, ensure that your base units in Illustrator for strokes are MM, not Points (the default), but do this prior to opening the SVG. You can also try setting the units to MM in the Document Setup if you are importing into an existing Illustrator document. These settings may not change the resultant import, though.

As you noted, Illustrator has trouble accepting the default MM units of the QGIS SVG export while Inkscape does a better job (Illustrator just doesn't seem to scale strokes at all). Therefore, you can try opening the SVG in Inkscape first, then exporting a PDF-formatted copy out. Opening the PDF copy in Illustrator should provide more accurate line width and font-rendering results than opening the SVG directly. I did not have good luck exporting to EPS from Inkscape, as it seemed to randomly rasterize parts of the output.

Likewise, you can also try exporting directly to PDF from QGIS and opening that in Illustrator. There will likely be differences in the result in Illustrator between the above SVG->Inkscape->PDF->Illustrator and PDF->Illustrator workflows. If you need more document structure, the former may be useful, though it destroys most of the SVG structure when the PDF format is introduced.

If you feel there is an issue that needs addressed here in the QGIS source code (if possible), please create a very detailed issue report, with lots of output sample comparisons, on the QGIS issue tracker. I could not find any existing issue ticket.

The best solution for SVG output in QGIS may be to create a custom exporter that is not tied to QSvgGenerator, or create a new QSvgGenerator subclass that pre-scales line widths for Illustrator-specific SVG output.

Related Question