Skip to content

Instantly share code, notes, and snippets.

@joshschmelzle
Created December 22, 2016 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshschmelzle/40ea9a61d7942885872d50252f168b49 to your computer and use it in GitHub Desktop.
Save joshschmelzle/40ea9a61d7942885872d50252f168b49 to your computer and use it in GitHub Desktop.
Using Pandoc with SublimeText3 to Export Markdown+SVG to PDF on win32

Make sure to install inkscape, pip, python and that they are all in your win32 path.

To check installation and version-

  • pip -V
  • python -V
  • pandoc -v
  • inkscape -V

Pandoc.sublime-settings:

{

  // There are 2 possible top level settings keys, "user" and "default". If you
  // use "default" in your user settings file, the default settings will be
  // overwritten, but if you use "user" your settings will be merged into the
  // default settings.
  "user": {

    // transformations
    "transformations": {
      "PDF SVG": {
        "scope": {
          "text.html": "html",
          "text.html.markdown": "markdown"
        },  
        "pandoc-arguments": [
            "-V", "geometry:margin=1.0in",
            "-t", "pdf",
            "--filter=C:\\Josh\\python\\scripts\\pandoc-svg.py",
        ]
      },

      "PDF SVG TOC": {
        "scope": {
          "text.html": "html",
          "text.html.markdown": "markdown"
        },  
        "pandoc-arguments": [
            "-V", "geometry:margin=1.0in",
            "-s", "--toc", "--number-sections", "--parse-raw",
            "-t", "pdf",
            "--filter=C:\\Josh\\python\\scripts\\pandoc-svg.py",
        ]
      }
    }
  }
}

pandoc-svg.py:

#!"C:\Users\Josh\AppData\Local\Programs\Python\Python35-32\python.exe"
"""
Pandoc filter to convert svg files to pdf as suggested at:
https://github.com/jgm/pandoc/issues/265#issuecomment-27317316
"""

__author__ = "Jerome Robert"

import mimetypes
import subprocess
import os
import sys
from pandocfilters import toJSONFilter, Str, Para, Image

fmt_to_option = {
    "latex": ("--export-pdf","pdf"),
    "beamer": ("--export-pdf","pdf"),
    #use PNG because EMF and WMF break transparency
    "docx": ("--export-png", "png"),
    #because of IE
    "html": ("--export-png", "png")
}

def svg_to_any(key, value, fmt, meta):
    if key == 'Image':
       if len(value) == 2:
           # before pandoc 1.16
           alt, [src, title] = value
           attrs = None
       else:
           attrs, alt, [src, title] = value
       mimet,_ = mimetypes.guess_type(src)
       option = fmt_to_option.get(fmt)
       if mimet == 'image/svg+xml' and option:
           base_name,_ = os.path.splitext(src)
           eps_name = base_name + "." + option[1]
           try:
               mtime = os.path.getmtime(eps_name)
           except OSError:
               mtime = -1
           if mtime < os.path.getmtime(src):
               cmd_line = ['inkscape', option[0], eps_name, src]
               sys.stderr.write("Running %s\n" % " ".join(cmd_line))
               subprocess.call(cmd_line, stdout=sys.stderr.fileno())
           if attrs:
               return Image(attrs, alt, [eps_name, title])
           else:
               return Image(alt, [eps_name, title])

if __name__ == "__main__":
  toJSONFilter(svg_to_any)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment