web/views/basetemplates.py
changeset 3085 b415bca9a9ed
parent 3072 6fb42c53f6df
parent 3080 32c2b6a34ab2
child 3185 bd0126d17e83
equal deleted inserted replaced
3083:3084bc9ccc64 3085:b415bca9a9ed
   268 if can_do_pdf_conversion():
   268 if can_do_pdf_conversion():
   269     from xml.etree.cElementTree import ElementTree
   269     from xml.etree.cElementTree import ElementTree
   270     from subprocess import Popen as sub
   270     from subprocess import Popen as sub
   271     from StringIO import StringIO
   271     from StringIO import StringIO
   272     from tempfile import NamedTemporaryFile
   272     from tempfile import NamedTemporaryFile
   273     from cubicweb.web.xhtml2fo import ReportTransformer
   273     from cubicweb.ext.xhtml2fo import ReportTransformer
   274 
   274 
   275     class PdfMainTemplate(TheMainTemplate):
   275     class PdfMainTemplate(TheMainTemplate):
   276         id = 'pdf-main-template'
   276         id = 'pdf-main-template'
   277 
   277 
   278         def call(self, view):
   278         def call(self, view):
   279             """build the standard view, then when it's all done, convert xhtml to pdf
   279             """build the standard view, then when it's all done, convert xhtml to pdf
   280             """
   280             """
   281             super(PdfMainTemplate, self).call(view)
   281             super(PdfMainTemplate, self).call(view)
   282             pdf = self.to_pdf(self._stream)
   282             section = self.req.form.pop('section', 'contentmain')
       
   283             pdf = self.to_pdf(self._stream, section)
   283             self.req.set_content_type('application/pdf', filename='report.pdf')
   284             self.req.set_content_type('application/pdf', filename='report.pdf')
   284             self.binary = True
   285             self.binary = True
   285             self.w = None
   286             self.w = None
   286             self.set_stream()
   287             self.set_stream()
   287             # pylint needs help
   288             # pylint needs help
   288             self.w(pdf)
   289             self.w(pdf)
   289 
   290 
   290         def to_pdf(self, stream, section='contentmain'):
   291         def to_pdf(self, stream, section):
   291             # XXX see ticket/345282
   292             # XXX see ticket/345282
   292             stream = stream.getvalue().replace(' ', ' ').encode('utf-8')
   293             stream = stream.getvalue().replace(' ', ' ').encode('utf-8')
   293             xmltree = ElementTree()
   294             xmltree = ElementTree()
   294             xmltree.parse(StringIO(stream))
   295             xmltree.parse(StringIO(stream))
   295             foptree = ReportTransformer(section).transform(xmltree)
   296             foptree = ReportTransformer(section).transform(xmltree)