Skip to content

Instantly share code, notes, and snippets.

@jeromerobert
Last active September 1, 2023 09:05
Show Gist options
  • Save jeromerobert/3996eca3acd12e4c3d40 to your computer and use it in GitHub Desktop.
Save jeromerobert/3996eca3acd12e4c3d40 to your computer and use it in GitHub Desktop.
Pandoc filter to create PDF files from SVG
#! /usr/bin/env python
"""
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)
@whateverforever
Copy link

           if re.match('https?\://',src):
               bsnm = urllib.unquote(os.path.basename(srcm).encode('utf8'))
               bsnm = re.sub('[^a-zA-Z0-9\.]','',bsnm)
               src,h = urllib.urlretrieve(src,bsnm)
               base_name,_ = os.path.splitext(bsnm)
               eps_name = base_name + "." + option[1]
           else:
               base_name, _ = os.path.splitext(src)
               eps_name = os.path.realpath(base_name + "." + option[1])
               src = os.path.realpath(src)

one further adjustment. Inkscape crashes on macos ** (inkscape-bin:91102): WARNING **: Can't open file: image.svg (doesn't exist), caused by some weird sh wrapper around inkscape, that sets some weird working directory. Using os.realpath() fixes that

@whateverforever
Copy link

whateverforever commented Jun 23, 2020

For the new Inkscape 1.0 on Catalina --export-filename has superseded --export-pdf

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

@ghamerly
Copy link

--export-filename should now be used in place of both --export-pdf and --export-png.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment