view data/plugin/macro/ @ 547:983196519144

macro.pdf2svg: fetches a pdf file from given url and converts it to svg. It is rendered as svg image on the wiki page and shows a link to the original pdf file.
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Thu, 24 Feb 2011 15:53:05 +0100
children 9e59fdd04742
line wrap: on
line source
# -*- coding: iso-8859-1 -*-
    MoinMoin - pdf2svg converts pdf files to svg and shows rendered svg

    * fetches pdf files from urls and converts them to svg,
      The new svg image is stored and rendered from its cachefile

    Requires posix platform and pdf2svg

    @copyright: 2011 MoinMoin:ReimarBauer
    @license: GNU GPL, see COPYING for details.

from MoinMoin import log
logging = log.getLogger(__name__)

import os
import tempfile
import urllib2

from MoinMoin import caching
from MoinMoin.util.SubProcess import exec_cmd
from MoinMoin.action import cache
from MoinMoin.Page import Page

CACHE_ARENA = 'sendcache'
CACHE_SCOPE = 'wiki'

def get_svg_key(request, url, pageno):
    gets the key needed for the svg cache file 
    return cache.key(request, itemname="SVG", content="%s.%s.SVG" % (url, pageno))

def prepare_svg_cache(request, url, pageno):
    prepares the svg cache file and returns its file name
    key = get_svg_key(request, url, pageno)
    cache.put(request, key, "<svg></svg>",
    data_cache = caching.CacheEntry(request, CACHE_ARENA, key+'.data', CACHE_SCOPE, do_locking=False)
    return data_cache._fname

def fetch_pdf_item(request, url):
    fetches the pdf item and stores it as cache file 
    key = cache.key(request, itemname="PDF", content=url)
        item = urllib2.urlopen(url)
    except (IOError, urllib2.HTTPError, ValueError), err:
        logging.debug("%s: %s" % (url, err))
        return ""
        cache.put(request, key,,
        data_cache = caching.CacheEntry(request, CACHE_ARENA, key+'.data', CACHE_SCOPE, do_locking=False)
        return data_cache._fname

def macro_pdf2svg(macro, url='', width=1200, height=800, pageno=1):
    converts pdf from urls to svg files using MoinMoins cache and renders from there
    request = macro.request
    pagename =
    content_type = 'image/svg+xml'
    template_cmd = "pdf2svg %(pdf_file)s %(svg_file)s %(pageno)s"

    if == 'posix':
        key = get_svg_key(request, url, pageno)
        if not cache.exists(request, key):
            pdf_file = fetch_pdf_item(request, url)
            svg_file = prepare_svg_cache(request, url, pageno)
            cmd = template_cmd % {"pdf_file": pdf_file,
                                  "svg_file": svg_file,
                                  "pageno": pageno
            data, errors, rc = exec_cmd(cmd, timeout=300)
            logging.debug("Command '%s', rc: %d, stdout: %d bytes, stderr: %s" % (cmd, rc, len(data), errors))
            if not errors:
                svg_image = open(svg_file, 'rb')
                cache.put(request, key,,

        cache_url = "%s%s%s" % (request.getQualifiedURL(), Page(request, pagename).url(request), cache.url(request, key))
        from MoinMoin import macro as _macro
        from MoinMoin.parser.text import Parser
        _macro.formatter = request.html_formatter
        parser = Parser("##\n", request)
        m = _macro.Macro(parser)
        html_object = m.execute('EmbedObject', u'width=%s, height=%s, target=%s, url_mimetype=%s' % (width, height, cache_url, content_type))
        return '<B>Converted from:</B> "<a href=%s>%s</a>" <b>to SVG</b><hr>%s<br>' % (url, url, html_object)