Esempio n. 1
0
def main(pdf_output_filepath):

    template = jinja2_env.get_template('template10.rml')
    context = {
        "STATIC_ROOT": STATIC_ROOT,
        'xcoordinate': '140',
        'ycoordinate': '30'
    }

    rml = template.render(**context)

    with open("/tmp/tmp_output.rml", 'wb') as f:
        f.write(rml)

    rml = unicode(rml).encode('utf-8').strip()

    # create a buffer in memory
    buf = StringIO.StringIO()
    buf_in = StringIO.StringIO(rml)

    root = etree.parse(buf_in).getroot()
    doc = document.Document(root)
    doc.filename = "imax-feedback.pdf"

    doc.process(buf)

    # Rewind the buffer.
    buf.seek(0)

    with open(pdf_output_filepath, 'wb') as f:
        f.write(buf.getvalue())
Esempio n. 2
0
    def __call__(self, path, inline=False, **kwargs):
        auth = self._checkAuth()
        if auth != AUTH_OK:
            self.raiseUnauthorized()
        tpldirectories = [
            os.path.dirname(path), self.parentdirpath
        ] + self.resourceDirs + [
            self.resolvePath(
                'gnrjs', 'gnr_d%s' % self.dojo_version, 'tpl', folder='*lib')
        ]
        lookup = TemplateLookup(directories=tpldirectories,
                                output_encoding='utf-8',
                                encoding_errors='replace')
        template = lookup.get_template(os.path.basename(path))
        self.response.content_type = 'application/pdf'
        filename = os.path.split(path)[-1].split('.')[0]
        inline_attr = (inline and 'inline') or 'attachment'
        self.response.add_header(
            "Content-Disposition",
            str("%s; filename=%s.pdf" % (inline_attr, filename)))
        import cStringIO
        from lxml import etree
        from z3c.rml import document

        tmp = template.render(mainpage=self, **kwargs)
        tmp = tmp.replace('&', '&')
        root = etree.fromstring(tmp)
        doc = document.Document(root)
        output = cStringIO.StringIO()
        doc.process(output)
        output.seek(0)
        return output.read()
Esempio n. 3
0
def go(xmlInputName, outputFileName=None, outDir=None, dtdDir=None):
    if dtdDir is not None:
        sys.stderr.write('The ``dtdDir`` option is not yet supported.\n')

    if hasattr(xmlInputName, 'read'):
        # it is already a file-like object
        xmlFile = xmlInputName
        xmlInputName = 'input.pdf'
    else:
        xmlFile = open(xmlInputName, 'rb')
    root = etree.parse(xmlFile).getroot()
    doc = document.Document(root)
    doc.filename = xmlInputName

    outputFile = None

    # If an output filename is specified, create an output filepointer for it
    if outputFileName is not None:
        if hasattr(outputFileName, 'write'):
            # it is already a file-like object
            outputFile = outputFileName
            outputFileName = 'output.pdf'
        else:
            if outDir is not None:
                outputFileName = os.path.join(outDir, outputFileName)
            outputFile = open(outputFileName, 'wb')

    # Create a Reportlab canvas by processing the document
    try:
        doc.process(outputFile)
    finally:
        if outputFile:
            outputFile.close()
        xmlFile.close()
Esempio n. 4
0
    def topdfstring(self):
        doc = document.Document(self)

        with DummyFile() as output_file:
            doc.process(output_file)

            return output_file.content
Esempio n. 5
0
    def topdffile(self, filename):
        def init_canvas(doc, init_canvas, canvas, filename):
            if not doc.postProcessors:
                canvas._filename = filename

            init_canvas(canvas)

        doc = document.Document(self)
        doc._initCanvas = lambda canvas, f=doc._initCanvas: init_canvas(doc, f, canvas, filename)

        with (open(filename, 'w') if doc.postProcessors else DummyFile()) as output_file:
            doc.process(output_file)
Esempio n. 6
0
    def create(self):
        buffer = StringIO()
        rml = render_to_string(self.template_name, self.data)

        buffer.write(rml)
        buffer.seek(0)
        root = etree.parse(buffer).getroot()
        doc = document.Document(root)

        new_buffer = BytesIO()
        doc.process(new_buffer)

        return new_buffer
Esempio n. 7
0
def parseString(xml, removeEncodingLine=True, filename=None):
    if isinstance(xml, six.text_type) and removeEncodingLine:
        # RML is a unicode string, but oftentimes documents declare their
        # encoding using <?xml ...>. Unfortuantely, I cannot tell lxml to
        # ignore that directive. Thus we remove it.
        if xml.startswith('<?xml'):
            xml = xml.split('\n', 1)[-1]
    root = etree.fromstring(xml)
    doc = document.Document(root)
    if filename:
        doc.filename = filename
    output = six.BytesIO()
    doc.process(output)
    output.seek(0)
    return output
Esempio n. 8
0
def go(xmlInputName, outputFileName=None, outDir=None, dtdDir=None):
    if dtdDir is not None:
        sys.stderr.write('The ``dtdDir`` option is not yet supported.')

    xmlFile = open(xmlInputName, 'r')
    root = etree.parse(xmlFile).getroot()
    doc = document.Document(root)
    doc.filename = xmlInputName

    outputFile = None

    # If an output filename is specified, create an output filepointer for it
    if outputFileName is not None:
        if outDir is not None:
            outputFileName = os.path.join(outDir, outputFileName)
        outputFile = open(outputFileName, 'wb')

    # Create a Reportlab canvas by processing the document
    doc.process(outputFile)