How to use the xhtml2pdf.util.pisaTempFile function in xhtml2pdf

To help you get started, we’ve selected a few xhtml2pdf examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github xhtml2pdf / xhtml2pdf / tests / test_utils.py View on Github external
def test_unicode(self):
        """Asserts bytes generated by reportlab are returned"""
        src = pisaTempFile()
        value = b'%PDF-1.4\r\n%\x93\x8c\x8b\x9e ReportLab Generated PDF document http://www.reportlab.com'
        try:
            src.write(value)
        except UnicodeDecodeError as error:
            self.fail(error)
github xhtml2pdf / xhtml2pdf / xhtml2pdf / util.py View on Github external
conn = httplib.HTTPConnection(server)
                conn.request("GET", path)
                r1 = conn.getresponse()
                # log.debug("HTTP %r %r %r %r", server, path, uri, r1)
                if (r1.status, r1.reason) == (200, "OK"):
                    self.mimetype = r1.getheader(
                        "Content-Type", '').split(";")[0]
                    self.uri = uri
                    log.debug("here")
                    if r1.getheader("content-encoding") == "gzip":
                        import gzip

                        self.file = gzip.GzipFile(
                            mode="rb", fileobj=six.BytesIO(r1.read()))
                    else:
                        self.file = pisaTempFile(r1.read())
                else:
                    log.debug(
                        "Received non-200 status: {}".format((r1.status, r1.reason)))
                    try:
                        urlResponse = urllib2.urlopen(uri)
                    except urllib2.HTTPError as e:
                        log.error("Could not process uri: {}".format(e))
                        return
                    self.mimetype = urlResponse.info().get(
                        "Content-Type", '').split(";")[0]
                    self.uri = urlResponse.geturl()
                    self.file = urlResponse

            else:

                log.debug("Unrecognized scheme, assuming local file path")
github xhtml2pdf / xhtml2pdf / xhtml2pdf / document.py View on Github external
def pisaErrorDocument(dest, c):
    out = pisaTempFile(capacity=c.capacity)
    out.write("<p style="background-color:red;"><strong>%d error(s) occured:</strong></p><p>" % c.err)
    for mode, line, msg, _ in c.log:
        if mode == "error":
            out.write("</p><pre>%s in line %d: %s</pre>" % (mode, line, cgi.escape(msg)))

    out.write("<p><strong>%d warning(s) occured:</strong></p><p>" % c.warn)
    for mode, line, msg, _ in c.log:
        if mode == "warning":
            out.write("</p><p>%s in line %d: %s</p>" % (mode, line, cgi.escape(msg)))

    return pisaDocument(out.getvalue(), dest, raise_exception=False)
github xhtml2pdf / xhtml2pdf / xhtml2pdf / util.py View on Github external
def getFile(self):
        if self.file is not None:
            return self.file
        if self.data is not None:
            return pisaTempFile(self.data)
        return None
github xhtml2pdf / xhtml2pdf / xhtml2pdf / document.py View on Github external
for bg in context.pisaBackgroundList:
                    page = input1.getPage(ctr)
                    if (
                            bg and not bg.notFound() and
                            (bg.mimetype == "application/pdf")
                    ):
                        bginput = PyPDF2.PdfFileReader(bg.getFile())
                        pagebg = bginput.getPage(0)
                        pagebg.mergePage(page)
                        page = pagebg
                    else:
                        log.warn(context.warning(
                            "Background PDF %s doesn't exist.", bg))
                    output.addPage(page)
                    ctr += 1
                out = pisaTempFile(capacity=context.capacity)
                output.write(out)
                # data = sout.getvalue()
                # Found a background? So leave loop after first occurence
                break
    else:
        log.warn(context.warning("PyPDF2 not installed!"))

    # Get the resulting PDF and write it to the file object
    # passed from the caller

    if dest is None:
        # No output file was passed - Let's use a pisaTempFile
        dest = io.BytesIO()
    context.dest = dest

    data = out.getvalue()
github xhtml2pdf / xhtml2pdf / xhtml2pdf / parser.py View on Github external
global CSSAttrCache
    CSSAttrCache = {}

    if xhtml:
        #TODO: XHTMLParser doesn't see to exist...
        parser = html5lib.XHTMLParser(tree=treebuilders.getTreeBuilder("dom"))
    else:
        parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("dom"))

    if isinstance(src, six.text_type):
        # If an encoding was provided, do not change it.
        if not encoding:
            encoding = "utf-8"
        src = src.encode(encoding)
        src = pisaTempFile(src, capacity=context.capacity)

    # # Test for the restrictions of html5lib
    # if encoding:
    #     # Workaround for html5lib&lt;0.11.1
    #     if hasattr(inputstream, "isValidEncoding"):
    #         if encoding.strip().lower() == "utf8":
    #             encoding = "utf-8"
    #         if not inputstream.isValidEncoding(encoding):
    #             log.error("%r is not a valid encoding e.g. 'utf8' is not valid but 'utf-8' is!", encoding)
    #     else:
    #         if inputstream.codecName(encoding) is None:
    #             log.error("%r is not a valid encoding", encoding)
    document = parser.parse(
        src,
        )#encoding=encoding)
github xhtml2pdf / xhtml2pdf / xhtml2pdf / util.py View on Github external
def getFile(self):
        if self.file is not None:
            return self.file
        if self.data is not None:
            return pisaTempFile(self.data)
        return None