Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save justincbagley/b973a13b6c0b58cc564c477ec6e3e5bc to your computer and use it in GitHub Desktop.
Save justincbagley/b973a13b6c0b58cc564c477ec6e3e5bc to your computer and use it in GitHub Desktop.
How I Got svg-resizer Working On Mac

How I got svg-resizer working on Mac OSX:

svg-resizer is a command line interface Javascript utility useful for single or batch resizing SVG files (with '.svg' file extensions). It's great for OSX, and I am attempting to use it in my latest shell scripts. Below I provide notes on the series of steps that I used to install svg-resizer and get it working on my macOS Sierra machine, on date February 20, 2017.

1. Install librsvg2 using Homebrew

librsvg2 is a key dependency of svg-resizer. So, I checked if this library was already on my machine by seeing whether a list check returned 0 (=absent) or 1 (=present):

$ brew list | grep "librsvg" | wc -l

Brew list check returned 0, so I installed librsvg2 using $ brew install librsvg.

You could also automate this in a script using a clean little if-else-fi, as follows:

$ BREW_CHECK="$(brew list | grep "librsvg" | wc -l)"
$ if [ $BREW_CHECK = "0" ]; then
$ echo "You need librsvg2, the main dependency of svg-resizer."
$	brew install librsvg
$ else
$	echo "librsvg2 is already installed on your local machine."
$ fi 

2. Download svg-resizer from GitHub

Downloaded as zip from https://github.com/vieron/svg-resizer, and then placed in ~/scripts folder on my local machine. No other installation procedures are required.

3. Install Node.js

Downloaded lastest version dmg installer (https://nodejs.org/dist/v7.5.0/node-v7.5.0.pkg) of Node.js from website (https://nodejs.org/). Simply get this, go through install steps (e.g. agree to license), and finish the install.

4. Set NODE_PATH

I opened bash profile file ($ nano ~/.bash_profile) and added the lines below to it, then sourced bash profile to save settings ($ source ~/.bash_profile).

# Added after installing Node.js Feb 2017
export NODE_PATH=$NODE_PATH:/usr/local/lib/node_modules

5. Install requisite modules using Node package manager (npm)

Here, npm is the Node.js Package Manager, a command line software management utility responsible for installing packages and updating packages and dependencies. npm comes bundled with the Node.js distro. I updated several Node.js modules in order to meet prerequisites for running svg-resizer, using the following code (note use of super user do, 'sudo', meaning you have to have root/admin access to replicate this):

$ sudo npm install shelljs -g
$ sudo npm install fs-extra -g
$ sudo npm install lodash -g
$ sudo npm install nomnom -g

6. Appropriately call svg-resizer to conduct resizing!

Finally, I used svg-resizer with the following code to successfully (and nicely!) reduce the size of one of my .svg files. The path to the .svg file is the (only) required positional parameter/input, the x and y flags specify the final width and height of the output image, and the . specifies for the modified file to be placed in my current working directory.

$ cd /Users/justinbagley/scripts/svg-resizer-master
$ node ./svg-resizer.js -f -x 400 -y 300 -o . \
../DISTRUCT_plots/3hfiles_x2_vertical.svg

7. Last, convert modified .svg to .pdf using cli Inkscape

I used the following code to do the conversion to PDF, and the WARNING message had no effect on the outcome, hence can be ignored.

$ inkscape -z -A 3hfiles_x2_vertical.pdf ./3hfiles_x2_vertical.svg
2017-02-20 14:32:16.559 inkscape[6277:12242726] *** WARNING: Method
userSpaceScaleFactor in class NSView is deprecated on 10.7 and later. It 
should not be used in new applications. Use convertRectToBacking: instead.  
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment