6

in short, I tried

for f in `ls *.pdf`; do inkscape -z --export-background=ffffff --export-background-opacity=1.0 --export-plain-svg=${f}.svg $f; done

It throws no errors, produces SVG files, but doesn't add background to them. Does someone know what's wrong?

It's Inkscape 0.92.4

UPD thanks Billy Kerr for pointing towards the solution. I combined his suggestion with use of GNU sed to process many files automatically. It seems like meaningful data in SVG starts from <defs> tag, so I just add the rectangle before the first <defs occurrence:

for f in "*.pdf.svg"; do sed -i '0,/<defs/s/<defs/<rect width="100%" height="100%" fill="#ffffff"\/>\n  <defs/' $f; done

which means: sed, please find the first occurrence of a pattern (0,/pattern) and substitute it s/pattern/new string \n pattern/, writing to the same file (sed -i flag). It looks like a very ad hoc solution, but it works.

2
  • 1
    The ls in your for-loop-head is superflous and should be abandoned. Just use "for f in "*.pdf"; do ..." Commented Aug 20, 2019 at 18:59
  • @userunknown thanks, I didn't know that! Commented Aug 21, 2019 at 8:17

2 Answers 2

4

Inkscape has no background attribute as such, although it does have pagecolor which can be set in the document properties (it shows up as "background" in the UI, but appears as "pagecolor" in the XML). The problem however is that the pagecolor attribute is not supported in SVG and so it will be lost when you export as plain SVG.

Instead you could try editing the SVG in a text editor to add a white filled rectangle the same size as the canvas, and before (i.e. underneath) the artwork layer. Sorry, I don't know how to do that with the command line, but hopefully there's enough information here to help you work it out.

for example:

<rect width="100%" height="100%" fill="#ffffff"/>
2
  • Doesn't work on the Inkscape command line currently (0.92.x), so no wonder you don't know how ;-)
    – Moini
    Commented Aug 19, 2019 at 14:45
  • @Moini Then perhaps just copy and pasting the code into the SVGs would be an option for the OP? Obviously it could be rather tedious if there are a lot of SVGs.
    – Billy Kerr
    Commented Aug 19, 2019 at 21:31
3

Typing inkscape --help shows all the commands. One of which being:

 -b, --export-background=COLOR              Background color of exported
                                             bitmap (any SVG-supported color
                                             string)

As an example, I use LaTeX and other tools a lot of mathematics. A part of the workflow is converting images from SVG to PNG (where I explicitly need a white background). I often use a command like:

inkscape MyFile.svg -e MyFile.png -w 300 -b white

This simply converts to a png with a width of 300 and with a white background.

As an example, here is my original:

original image

And here is an export with a RED background:

image with red background

2
  • Thank you for answering, but I don't consider changing from SVG to a raster image. Commented Aug 10, 2022 at 16:49
  • @KarenFidanyan no problem. Just thought to leave my suggestion in case all else fails :) Commented Aug 11, 2022 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.