changeset 556:47d9180d02ed

macro.pdf2img: refactored for additional output format png
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Thu, 10 Mar 2011 10:55:48 +0100
parents f1ee661e1c46
children 8421731685d4
files data/plugin/macro/pdf2img.py
diffstat 1 files changed, 51 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/data/plugin/macro/pdf2img.py	Thu Mar 10 08:53:01 2011 +0100
+++ b/data/plugin/macro/pdf2img.py	Thu Mar 10 10:55:48 2011 +0100
@@ -1,14 +1,14 @@
 # -*- coding: iso-8859-1 -*-
 """
-    MoinMoin - pdf2svg converts pdf files to svg and shows rendered svg
+    MoinMoin - pdf2img converts pdf files to images of type svg or png and shows it rendered
 
     Features:
-    * fetches pdf files from urls and converts them to svg,
-      The new svg image is stored and rendered from its cachefile
+    * fetches pdf files from urls and converts them to svg or png
+      The new image is stored and rendered from its cachefile
     * can also be used for an attached pdf file 
     * checks timestamp of content for updating cached image
 
-    Requires posix platform and pdf2svg
+    Requires posix platform and pdf2svg and pstoimg
 
     @copyright: 2011 MoinMoin:ReimarBauer
     @license: GNU GPL, see COPYING for details.
@@ -30,6 +30,8 @@
 
 CACHE_ARENA = 'sendcache'
 CACHE_SCOPE = 'wiki'
+SVG_CMD = "pdf2svg %(pdf_file)s %(img_file)s %(pageno)s"
+PS2TOIMG_CMD = "pstoimg -quiet -crop tblr -density 200 -type png %(pdf_file)s -out %(img_file)s"
 
 def url_exists(url):
     content = ""
@@ -55,19 +57,19 @@
     response = conn.getresponse()
     return response.getheader('last-modified')
 
-def get_svg_key(request, url, pageno, identifier):
+def get_img_key(request, url, pageno, identifier, format):
     """
-    gets the key needed for the svg cache file 
+    gets the key needed for the cache file 
     """
-    return cache.key(request, itemname="SVG", content="%s.%s.%s.SVG" % (url, pageno, identifier))
+    return cache.key(request, itemname=format, content="%s.%s.%s.%s" % (url, pageno, identifier, format))
 
-def prepare_svg_cache(request, url, pageno, identifier):
+def prepare_img_cache(request, url, pageno, identifier, format):
     """
-    prepares the svg cache file and returns its file name
+    prepares the cache file and returns its file name
     """
-    key = get_svg_key(request, url, pageno, identifier)
-    cache.put(request, key, "<svg></svg>",
-              content_type="image/svg+xml")
+    key = get_img_key(request, url, pageno, identifier, format)
+    cache.put(request, key, " ",
+              content_type=format)
     data_cache = caching.CacheEntry(request, CACHE_ARENA, key+'.data', CACHE_SCOPE, do_locking=False)
     return data_cache._fname
 
@@ -89,16 +91,18 @@
         data_cache = caching.CacheEntry(request, CACHE_ARENA, key+'.data', CACHE_SCOPE, do_locking=False)
         return data_cache._fname
 
-def macro_pdf2svg(macro, attachment='', url='', width=1200, height=800, pageno=1):
+def macro_pdf2img(macro, attachment='', url='', width=1200, height=800, pageno=1, format=("svg", "png")):
     """
-    converts pdf from urls to svg files using MoinMoins cache and renders from there
+    converts pdf from urls to image files using MoinMoins cache and renders from there
     """
     request = macro.request
     pagename = request.page.page_name
-    content_type = 'image/svg+xml'
-    template_cmd = "pdf2svg %(pdf_file)s %(svg_file)s %(pageno)s"
+    if format == "svg":
+        content_type = 'image/svg+xml'
+    if format == "png":
+        content_type = 'image/png'
     pdf_file = None
-
+    # only for posix implemented
     if os.name == 'posix':
         if attachment:
             if not AttachFile.exists(request, pagename, attachment):
@@ -111,7 +115,7 @@
             identifier = last_modified(url=url)
         logging.debug("%s: %s" % (url, identifier))
 
-        key = get_svg_key(request, url, pageno, identifier)
+        key = get_img_key(request, url, pageno, identifier, format)
         if not cache.exists(request, key):
             if attachment and attachment.lower().endswith('.pdf') and AttachFile.exists(request, pagename, attachment):
                 pdf_file = os.path.join(AttachFile.getAttachDir(request, pagename), attachment).encode(config.charset)
@@ -119,31 +123,43 @@
                 #TODO: a mimetype check is needed beforehand 
                 pdf_file = fetch_pdf_item(request, url)
             if pdf_file:
-                svg_file = prepare_svg_cache(request, url, pageno, identifier)
-                cmd = template_cmd % {"pdf_file": pdf_file,
-                                      "svg_file": svg_file,
-                                      "pageno": pageno
-                                      }
+                img_file = prepare_img_cache(request, url, pageno, identifier, format)
+                if format == "svg":
+                    cmd = SVG_CMD % {
+                                     "pdf_file": pdf_file,
+                                     "img_file": img_file,
+                                     "pageno": pageno
+                                     }
+                if format == "png":
+                    #without -multipage we get only the first page
+                    cmd = PS2TOIMG_CMD % {
+                                          "pdf_file": pdf_file,
+                                          "img_file": img_file,
+                                          }
                 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, svg_image.read(),
-                              content_type="image/svg+xml")
-                    svg_image.close()
+                    image = open(img_file, 'rb')
+                    cache.put(request, key, image.read(),
+                              content_type=content_type)
+                    image.close()
 
         if cache.exists(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))
+            if format == "svg":
+                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))
+            else:
+                html_object = request.formatter.image(src=cache.url(request, key), alt="", width=width)
             if attachment:
                 url = AttachFile.getAttachUrl(pagename, attachment, request, addts=0, do='get')
-                return '<B>SVG Image from:</B> "<a href=%s>%s</a>"<hr>%s<br>' % (url, attachment, html_object)
+                return '<B>%s Image from:</B> "<a href=%s>%s</a>"<hr>%s<br>' % (format.upper(), url, attachment, html_object)
             else:
-                return '<B>SVG Image from:</B> "<a href=%s>%s</a>"<hr>%s<br>' % (url, url, html_object)
+                return '<B>%s Image from:</B> "<a href=%s>%s</a>"<hr>%s<br>' % (format.upper(), url, url, html_object)
         else:
              return ""
+