Inkscape for bulk SVG conversion

I have a Windows machine with Cygwin on it.

It has a Windows install of Inkscape in (in Cygwin notation):

/cygdrive/c/Program Files/Inkscape/bin/inkscape.exe

So Cygwin gives me all the useful Linux/UNIX command line tools, and the one that we’re going to work with now is find.

I can see a list of all my SVG files, after I cd to the parent directory.

I can see what PNG files I already have:

$ find . -iname "*png" | more

I can see how to use Inkscape from the command line:

$ /cygdrive/c/Program\ Files/Inkscape/bin/inkscape.exe --help

And I see that I can ask it to export to a file type, and it will guess the output format based on the output file extension. Or I can specify a type and it will use the old file name with a new extension:

$ /cygdrive/c/Program\ Files/Inkscape/bin/inkscape.exe --export-dpi=72 --export-type=png filename.svg

So we can use find to walk the file hierarchy and inkscape to convert things when we find them.

$ find /path/to/parent/folder -name "*.svg" -exec /cygdrive/c/Program\ Files/Inkscape/bin/inkscape.exe --export-dpi=72 --export-type=png {} \;

Various command line switches can be used to tune the results.

Having said that, the non-native (Cairo) import into SVG seems to work better than the native one.

Switch

sed for a colleague

Want to change fonts in a whole bunch of SVG files, from Open Sans to Source Sans Pro. SVG (scaleable vector graphics files are text files, so we can use the LInux/BSD command line. Now, this here is for Linux — the BSD versions of some commands differ a little.

See: https://www.cyberciti.biz/faq/how-to-use-sed-to-find-and-replace-text-in-files-in-linux-unix-shell/

To search through one file and change all occurrences, something like:

$ sed -i.bak 's/Open Sans/Source Sans Pro/g' file.svg

should do it.

sed = stream editor (the program that does the work)

-i = inplace (change the file in place, rather than writing a new one); but create a backup with extension .bak

s = substitute (ie find and replace)

/ = field separator

Open Sans = string to find

Source Sans Pro = string to replace it with (on BSD/Mac you may need to use Open\ Sans, where the backslash ‘escapes’ the spaces and makes the program read it literally).

g = global (do it everywhere — otherwise will do only first appearance on a line)

file.svg = name of your svg file.

If they are all in the same folder, you can just go (Linux; not sure about BSD/Mac):

$ sed -i.bak 's/Open Sans/Source Sans Pro/g' *.svg

And it should do them all. If that does not work, look over them:

$ for f in *.svg ; do sed -i.bak ‘s/Open Sans/Source Sans Pro/g’ $f.svg ; done

If the svg files are in many directories, you’ll need to find them first. If you go to the root of the file Figures tree, something like:

$ find . -name "*.svg" -type f -exec sed -i .bak 's/Open Sans/Source Sans Pro/g' {} \;

find = program to find files

. = begin the search here (for example, you have first gone “cd /path/to/my/files”; can specify another directory here if you want to search elsewhere

-name = search by file name (case sensitive — -iname is insensitive)

“*.svg” = find files that end in svg (note, will NOT match .SVG; if some files have caps, use -iname)

-type f = search for files (can also search for directories, -type d)

-exec = when you find a file that matches, execute the following command

then it is as above sed command

{} = put the name of the file you found here (eg file.svg)

\; = ‘we are done’

Now, sed and find on Mac may not behave exactly as they do on Linux, which is what I use. I suggest you test the sed command on a single file first.

Here is my test file, test.txt

Open Sans
Fred fred fred Open Sans
Open SansOpen SansOpen Sans

I run the command:

$ sed -i.bak 's/Open Sans/Source Sans Pro/g' test.txt

and now this is my file:

Source Sans Pro
Fred fred fred Source Sans Pro
Source Sans ProSource Sans ProSource Sans Pro

and I also have a backup file: test.txt.bak

If you type simply:

$ find . -name "*.svg"

It will show you all the svg files that sed will operate on. Good to do before running the ‘real’ command.

You can customise the find command; for example:

$ find -name "*final*.svg"

this will only find files with lower case ‘final’ in the name and ending in svg. I can make it more complicated — say I only and files with numbers 30 to 49 in the title:

$ find . -name "*[3-4][0-9]*.svg"
./URB_45/URB_45-01-06Oct21.svg
./URB_33/URB_33-attempt1-hori2-combined.svg
./URB_33/URB_33-attempt1.svg
./URB_33/URB_33-attempt1-hori2.svg
./URB_33/URB_33-attempt1-hori.svg

etc

here [3-4][0-9] will cover character combinations 30 to 39 then 40 to 49.

On Mac

sed on Mac might need:

$ sed -i '.bak' -e 's/string1/string2/' {} \;

That is, it might be different from what worked for me. These methods will be possible, but Mac is not Linux and some fine tuning will be needed.

Thus endeth the random ramble

Convert pdf to svg — Linux command line or Cygwin

2 options come to mind. First is dvisvgm, that is generally part of a modern LaTeX suite, or can radily be installed alongside. It can convert PDF and EPS as well as DVI files to SVG. I found that it worked well unless the PDF had been cropped, in which case it seemed to use a box the size of the cropped PDF but the box had an origin where the uncropped PDF did, so it cut off some of the image.

The problem might relate to how the image were cropped. I used pdfcrop.sh, which uses a combination of gs and pdftk and perl. No other programs have problems with the cropped PDFs, but you never know! Also, the dvisvgm on Cygwin is version 2.9, not quite the latest.

$ dvisvgm -V
dvisvgm 2.9.1

There is also pdf2svg, a separate program.

First, go here:

https://github.com/dawbarton/pdf2svg

Then here is the bit of Cygwin bash history (Cygwin = no use of sudo); I’m not sure exactly what the dependencies are, I just installed things until ./configure worked; apt-cyg is very nice:

cd installs/
mkdir pdf2svg
cd pdf2svg/
wget https://github.com/dawbarton/pdf2svg/archive/master.zip
unzip master.zip 
cd pdf2svg-master/
./configure
apt-cyg search poppler
apt-cyg install libpoppler-glib-devel libpoppler-devel
./configure
make
make install
pdf2svg --help

And it works a treat; for a 1-page file:

$ pdf2svg.exe unsrt-eg.pdf unsrt-eg.svg

Also works on cropped ones. pdfcrop.sh by default crops white space off the edges. Running pf2svg on the output of pdfcrop.sh gives the right -hand image below:

Left: dvisvgm. Right: pdf2svg.

 

Fantastic!

xFig convert all

xFig is hardly cutting edge any more, but it has some useful advantages; .fig files can be edited in a text editor, and much more readily than eps files, and fig2dev can output a whole bunch of other formats and can be run on the command line.

This is just a ‘note to self’ giving a little script that converts all the fig files into other formats ready for insertion into web pages or  documents. It generates eps, emf and svg files and puts them into appropriate directories, creating the directories if need be.

Can add any number of conversion targets to the list, but bitmap ones (png etc) generally need options to ensure the resolution is good enough. That precludes putting them inside the inner loop. The png example shows how this works. I have magnified it by 4, which, when the image is shrunk back to original size, has the effect of quadrupling the pixels per inch relative to the default. The default is low — a screen resolution, so something like 96 ppi.

$ cat convert_all.sh
for f in *.fig
do
  prename=`basename $f .fig`
# Vector formats don't need custom arguments
  for extn in eps emf svg
  do
    mkdir -p $extn
    echo "fig2dev -L $extn $f > $extn/$prename.$extn"
    fig2dev -L $extn $f > $extn/$prename.$extn
  done
# Bitmap formats need custom arguments
  mkdir -p png
  echo "fig2dev -L png -m 4 $f > png/$prename.png"
  fig2dev -L png -m 4 $f > png/$prename.png
done

Just run it in the folder where the files are.

 

fig2devilish

PDF to emf or svg for putting vector graphics in Word

Started with multipage PDF document.

Cropped out the bit I wanted.

First, I extracted the page I wanted. I have all the Poppler stuff installed in Cygwin, so:

$ pdfseparate -f 4 -l 4 whatiscc1.pdf page4.pdf

(-f means first page to extract, -l means last) Then I cropped out the bit I wanted of that page 4.

I did that by opening it in gv and noting the pixel coordinates.

I used pdfcrop from TeXLive, and I’ll confess I iterated a few time to get the coordinates I wanted. Remember that it trims off white space then adds margins, so the margin option works relative to the crop box, not the original page!

$ pdfcrop --margins "-52 -250 -20 -25" page4.pdf page4-crop.pdf

So now I had my PDF image.

GUI

Fired up Inkscape and use the internal import, but that looked a mess.

Tried ‘Poppler/Cairo’ import — looked good.

Saved as an Inkscape svg, wmf and emf, using default settings.

Back in Word — Insert → PIcture Chose the emf; looked good. So did the wmf and the svg.

Now, it is not a complex image with many paths, gradients, fills etc, so YMMV.

Command line

Inkscape can also be used thus:

$ inkscape.exe page4-crop.pdf -l page4_test.svg

And this svg file was fine too.

Note that the command line use seems to use the native PDF import, which is not as good as the Poppler/Cairo-based one. I have had a couple of occasions when the command line run did not work and I used Poppler/Cairo import via the GUI.

Inkscape.