Exemplo n.º 1
0
 def __call__(self):
     context = aq_inner(self.context)
     request = context.REQUEST
     alsoProvides(context, IPrintable)
     IStatusMessage(request).addStatusMessage(
         _("This document is now printable. Click on the 'print' tab for the printed version."), type="info"
     )
     request.response.redirect(context.absolute_url())
Exemplo n.º 2
0
    def __call__(self):
        """Returns the PDF printout.
        """
        context = aq_inner(self.context)
        body = context.getRawText()

        # Check there is a template
        if self.default_template == "":
            IStatusMessage(self.request).addStatusMessage(
                _("Template is empty. Check your Printout control panel."), type="info"
            )
            return self.request.response.redirect(context.absolute_url())

        # Render Chameleon Template
        plone_portal_state = getMultiAdapter((context, self.request), name="plone_portal_state")
        plone_tools = getMultiAdapter((context, self.request), name="plone_tools")
        plone_context_state = getMultiAdapter((context, self.request), name="plone_portal_state")
        try:
            pt = PageTemplate(self.default_template, format="xml", encoding="utf-8", debug=True)
            cooked_body = cook_body(body, self.default_body_stylesheet)
            output = pt.render(
                context=context,
                plone_portal_state=plone_portal_state,
                plone_tools=plone_tools,
                plone_context_state=plone_context_state,
                body=cooked_body,
            )
        except ExpatError, e:
            error = {
                "title": "Error rendering the default (chameleon/zpt) template",
                "message": str(e),
                "fo_output": "",
                "template": escape(self.default_template),
                "stylesheet": escape(self.default_body_stylesheet),
                "body": "",
                "cooked_body": "",
            }
            return self.error(error)
Exemplo n.º 3
0
            log.info("--------")
            log.info("\n\n" + self.default_template + "\n\n")
            log.info("--------")
            error = {
                "title": "Error running FOP (XSL-FO to PDF transformation)",
                "message": str(e),
                "fo_output": escape(output),
                "template": escape(self.default_template),
                "stylesheet": escape(self.default_body_stylesheet),
                "body": "",
                "cooked_body": "",
            }
            return self.error(error)

        # Set status message
        IStatusMessage(self.request).addStatusMessage(_("Printout"), type="info")

        # Deliver PDF
        return self.deliver(output_filename)

    def deliver(self, pdf_filename):
        """Stream generated PDF file back to client.
        """
        log.info("Deliver %s" % pdf_filename)
        request = self.request.RESPONSE
        request.setHeader("content-type", "application/pdf")
        request.setHeader("content-length", os.stat(pdf_filename)[6])
        request.setHeader("content-disposition", "attachment; filename=%s" % pdf_filename)
        return filestream_iterator(pdf_filename, "rb")

    def error(self, error):