Commits

ajung committed 8d14ce3

cleanup

  • Participants
  • Parent commits 9e12931

Comments (0)

Files changed (2)

File src/pp.client-plone/pp/client/plone/browser/configure.zcml

     <!-- standard PDF view -->
 
     <browser:page
-      name="asPlainPDF"
+      name="asPDF"
       for="*"
       permission="zope2.View"
       class=".pdf.PDFDownloadView"
     />
 
-    <browser:page
-      name="ppConvert"
-      for="*"
-      permission="zope2.View"
-      class=".pdf.GenericDownloadView"
-    />
-
 </configure>

File src/pp.client-plone/pp/client/plone/browser/pdf.py

 class ProducePublishView(BrowserView):
     """ Produce & Publish view (using Produce & Publish server) """
 
-    template = ViewPageTemplateFile('../resources_default/pdf_template_standalone.pt')
+    def __init__(self, context, request):
+        self.request = request
+        self.context = context
+        self.resource = default_resource
 
     # default transformations used for the default PDF view.
     # 'transformations' can be overriden within a derived ProducePublishView.
 
     transformations = (
         'makeImagesLocal',
-#        'removeEmptyElements',
-#        'removeInternalLinks',
-#        'annotateInternalLinks',
-#        'cleanupTables',
         'convertFootnotes',
         'removeCrapFromHeadings',
         'fixHierarchies',
         'addTableOfContents',
         )
 
-    def copyResources(self, resources_dir, destdir):
+    def copyResourceFiles(self, destdir):
         """ Copy over resources for a global or local resources directory into the 
             destination directory.
         """
-        if os.path.exists(resources_dir):
-            for name in os.listdir(resources_dir):
-                fullname = os.path.join(resources_dir, name)
-                if os.path.isfile(fullname):
-                    shutil.copy(fullname, destdir)
+
+        fslayer = self.resource.fslayer
+        for dirname, filenames in fslayer.walk():
+            for filename in filenames:
+                fullpath = os.path.join(dirname, filename)
+                with fslayer.open(fullpath, 'rb') as fp:
+                    content = fp.read()
+
+                if fullpath.startswith('/'):
+                    fullpath = fullpath[1:]
+                destpath = os.path.join(destdir, fullpath)
+                if not os.path.exists(os.path.dirname(destpath)):
+                    os.makedirs(os.path(dirname))
+                with open(destpath, 'wb') as fp:
+                    fp.write(content)
 
     def transformHtml(self, html, destdir, transformations=None):
         """ Perform post-rendering HTML transformations """
         data = params.get('data', None)
 
         template = PageTemplate()
-        template.write(default_resource.read('pdf_template.pt'))
+        template.write(self.resource.read('pdf_template.pt'))
+
+        # copy resource files
+        self.copyResourceFiles(destdir)
 
         # Now render the complete HTML document
         html = template(self,
         # and apply transformations
         html = self.transformHtml(html, destdir)
 
-        # hack to replace '&' with '&amp;'
-        html = html.replace('& ', '&amp; ')
-
         # and store it in a dedicated working directory
         dest_filename = os.path.join(destdir, 'index.html')
         with codecs.open(dest_filename, 'wb', encoding='utf-8') as fp:
             fp.write(html)
 
-        # copy over global styles etc.
-        resources_dir = os.path.join(cwd, 'resources')
-        self.copyResources(resources_dir, destdir)
-
-        # copy over language dependent hyphenation data
-        if language:
-            hyphen_file = os.path.join(resources_dir, 'hyphenation', language + '.hyp')
-            if os.path.exists(hyphen_file):
-                shutil.copy(hyphen_file, destdir)
-
-            hyphen_css_file = os.path.join(resources_dir, 'languages', language + '.css')
-            if os.path.exists(hyphen_css_file):
-                shutil.copy(hyphen_css_file, destdir)
 
-        # now copy over resources (of a derived view)
-        self.copyResources(getattr(self, 'local_resources', ''), destdir)
+        # create a local ZIP file containing all the data for the conversion
+        # basically for debugging purposes only.
         if ZIP_OUTPUT or 'zip_output' in params:
             archivename = tempfile.mktemp(suffix='.zip')
             fp = zipfile.ZipFile(archivename, "w", zipfile.ZIP_DEFLATED) 
             for root, dirs, files in os.walk(destdir):
-                #NOTE: ignore empty directories
                 for fn in files:
                     absfn = os.path.join(root, fn)
                     zfn = absfn[len(destdir)+len(os.sep):] #XXX: relative path
         if 'no_conversion' in params:
             return destdir
         
-        if LOCAL_CONVERSION:
-            from zopyx.convert2 import Converter
-            c = Converter(dest_filename)
-            result = c(params.get('converter', 'pdf-pisa'))
-            if result['status'] != 0:
-                raise RuntimeError('Error during PDF conversion (%r)' % result)
-            pdf_file = result['output_filename']
-            LOG.info('Output file: %s' % pdf_file)
-            return pdf_file
-        else:
-            # Produce & Publish server integration
-            from pp.client.python import pdf
-            result = pdf.pdf(destdir, self.request.get('converter', 'princexml'))
-            output_filename = tempfile.mktemp(suffix='.pdf')
-            with open(output_filename, 'wb') as fp:
-                fp.write(zlib.decompress(result['data'].data))
-            LOG.info('Output file: %s' % output_filename)
-            return output_filename
+        # Produce & Publish server integration
+        from pp.client.python import pdf
+        result = pdf.pdf(destdir, self.request.get('converter', 'princexml'))
+        output_filename = tempfile.mktemp(suffix='.pdf')
+        with open(output_filename, 'wb') as fp:
+            fp.write(zlib.decompress(result['data'].data))
+        LOG.info('Output file: %s' % output_filename)
+        return output_filename
 
 InitializeClass(ProducePublishView)
 
 
     def __call__(self, *args, **kw):
 
-        if not 'resource' in kw:
-            kw['resource'] = 'pp-default'
-        if not 'template' in kw:
-            kw['template'] = 'pdf_template_standalone'
-        kw['no-split'] = True
-
         output_file = super(PDFDownloadView, self).__call__(*args, **kw)
         mimetype = os.path.splitext(os.path.basename(output_file))[1]
         R = self.request.response
 InitializeClass(PDFDownloadView)
 
 
-class GenericDownloadView(ProducePublishView):
-
-    def __call__(self, *args, **kw):
-
-        output_file = super(GenericDownloadView, self).__call__(*args, **kw)
-        mimetype = os.path.splitext(os.path.basename(output_file))[1]
-        # return output file over HTTP
-        R = self.request.response
-        R.setHeader('content-type', 'application/%s' % mimetype)
-        R.setHeader('content-disposition', 'attachment; filename="%s.%s"' % (self.context.getId(), mimetype))
-        R.setHeader('content-length', os.path.getsize(output_file))
-        R.setHeader('pragma', 'no-cache')
-        R.setHeader('cache-control', 'no-cache')
-        R.setHeader('Expires', 'Fri, 30 Oct 1998 14:19:41 GMT')
-        return filestream_iterator(output_file, 'rb')
-
-InitializeClass(GenericDownloadView)