[Checkins] SVN: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/base.py injecting BASE tag in order to call converters with the absolute

Andreas Jung andreas at andreas-jung.com
Sun Jul 12 06:45:32 EDT 2009


Log message for revision 101828:
  injecting BASE tag in order to call converters with the absolute
  path of the html input file
  
  

Changed:
  U   zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/base.py

-=-
Modified: zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/base.py
===================================================================
--- zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/base.py	2009-07-12 09:58:01 UTC (rev 101827)
+++ zopyx.smartprintng.server/trunk/zopyx/smartprintng/server/base.py	2009-07-12 10:45:31 UTC (rev 101828)
@@ -24,6 +24,20 @@
 class ServerCore(object):
     """ SmartPrintNG Server Core Implementation """
 
+    def _inject_base_tag(self, html_filename):
+        """ All input HTML files contain relative urls (relative
+            to the path of the main HTML file (the "working dir").
+            So we must inject a BASE tag in order to call the external
+            converters properly with the full path of the html input file
+            since we do not want to change the process working dir (not
+            acceptable in a multi-threaded environment).
+            ATT: this should perhaps handled within zopyx.convert2
+        """
+        html = file(html_filename).read()
+        pos = html.lower().find('<head>')
+        html = html[:pos] + '<head><base href="%s"/>' % html_filename + html[pos+6:]
+        file(html_filename, 'wb').write(html)
+
     def convertZIP(self, zip_archive, converter_name='pdf-prince'):
         """ Process html-file + images within a ZIP archive """
 
@@ -51,6 +65,8 @@
         if len(html_files) > 1:
             raise RuntimeError('Archive contains more than one html file')
         html_filename = html_files[0]
+        # inject BASE tag containing the full local path (required by PrinceXML)
+        self._inject_base_tag(html_filename)
         result = self._convert(html_filename, 
                                converter_name=converter_name)
         basename, ext = os.path.splitext(os.path.basename(result))



More information about the Checkins mailing list