def join(self, file=None):
        output = PyPDF2.PdfFileWriter()
        for pdffile in self.files:
            input = PyPDF2.PdfFileReader(pdffile)
            for pageNumber in xrange(input.getNumPages()):
                output.addPage(input.getPage(pageNumber))

        if file is not None:
            output.write(file)
            return file
        out = pisaTempFile(capacity=self.capacity)
        output.write(out)
        return out.getvalue()
示例#2
0
def pisaDocument(src,
                 dest=None,
                 path=None,
                 link_callback=None,
                 debug=0,
                 default_css=None,
                 xhtml=False,
                 encoding=None,
                 xml_output=None,
                 raise_exception=True,
                 capacity=100 * 1024,
                 context_meta=None,
                 **kw):
    log.debug(
        "pisaDocument options:\n  src = %r\n  dest = %r\n  path = %r\n  link_callback = %r\n  xhtml = %r\n  context_meta = %r",
        src, dest, path, link_callback, xhtml, context_meta)

    # Prepare simple context
    context = pisaContext(path, debug=debug, capacity=capacity)

    if context_meta is not None:
        context.meta.update(context_meta)

    context.pathCallback = link_callback

    # Build story
    context = pisaStory(src,
                        path,
                        link_callback,
                        debug,
                        default_css,
                        xhtml,
                        encoding,
                        context=context,
                        xml_output=xml_output)

    # Buffer PDF into memory
    out = io.BytesIO()

    doc = PmlBaseDoc(out,
                     pagesize=context.pageSize,
                     author=context.meta["author"].strip(),
                     subject=context.meta["subject"].strip(),
                     keywords=[
                         x.strip()
                         for x in context.meta["keywords"].strip().split(",")
                         if x
                     ],
                     title=context.meta["title"].strip(),
                     showBoundary=0,
                     allowSplitting=1)

    # Prepare templates and their frames
    if "body" in context.templateList:
        body = context.templateList["body"]
        del context.templateList["body"]
    else:
        x, y, w, h = getBox("1cm 1cm -1cm -1cm", context.pageSize)
        body = PmlPageTemplate(id="body",
                               frames=[
                                   Frame(x,
                                         y,
                                         w,
                                         h,
                                         id="body",
                                         leftPadding=0,
                                         rightPadding=0,
                                         bottomPadding=0,
                                         topPadding=0)
                               ],
                               pagesize=context.pageSize)

    doc.addPageTemplates([body] + list(context.templateList.values()))

    # Use multibuild e.g. if a TOC has to be created
    if context.multiBuild:
        doc.multiBuild(context.story)
    else:
        doc.build(context.story)

    # Add watermarks
    if PyPDF2:
        for bgouter in context.pisaBackgroundList:
            # If we have at least one background, then lets do it
            if bgouter:
                istream = out

                output = PyPDF2.PdfFileWriter()
                input1 = PyPDF2.PdfFileReader(istream)
                ctr = 0
                # TODO: Why do we loop over the same list again?
                # see bgouter at line 137
                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()

    # data (bytes obj instance) doesn't have the attribute encode()
    # if isinstance(dest, io.BytesIO):
    #     data = data.encode("utf-8")

    context.dest.write(data)  # TODO: context.dest is a tempfile as well...

    return context
示例#3
0
def pisaDocument(src,
                 dest=None,
                 path=None,
                 link_callback=None,
                 debug=0,
                 default_css=None,
                 xhtml=False,
                 encoding=None,
                 xml_output=None,
                 raise_exception=True,
                 capacity=100 * 1024,
                 context_meta=None,
                 **kw):
    log.debug(
        "pisaDocument options:\n  src = %r\n  dest = %r\n  path = %r\n  link_callback = %r\n  xhtml = %r\n  context_meta = %r",
        src, dest, path, link_callback, xhtml, context_meta)

    # Prepare simple context
    context = pisaContext(path, debug=debug, capacity=capacity)

    if context_meta is not None:
        context.meta.update(context_meta)

    context.pathCallback = link_callback

    # Build story
    context = pisaStory(src,
                        path,
                        link_callback,
                        debug,
                        default_css,
                        xhtml,
                        encoding,
                        context=context,
                        xml_output=xml_output)

    # Buffer PDF into memory
    out = io.BytesIO()
    doc = PmlBaseDoc(out,
                     pagesize=context.pageSize,
                     author=context.meta["author"].strip(),
                     subject=context.meta["subject"].strip(),
                     keywords=[
                         x.strip()
                         for x in context.meta["keywords"].strip().split(",")
                         if x
                     ],
                     title=context.meta["title"].strip(),
                     showBoundary=0,
                     allowSplitting=1)
    # Prepare templates and their frames
    multi_template_list = False
    if "body" in context.templateList:
        body = context.templateList["body"]
        del context.templateList["body"]
    else:
        x, y, w, h = getBox("1cm 1cm -1cm -1cm", context.pageSize)
        body = PmlPageTemplate(id="body",
                               frames=[
                                   Frame(x,
                                         y,
                                         w,
                                         h,
                                         id="body",
                                         leftPadding=0,
                                         rightPadding=0,
                                         bottomPadding=0,
                                         topPadding=0)
                               ],
                               pagesize=context.pageSize)

    ptl = build_grid_templates(doc, context)
    if ptl == []:
        doc.addPageTemplates([body] + list(context.templateList.values()))
    if ptl != []:
        if out_grid == []:
            doc.addPageTemplates(ptl)
        else:
            doc.addPageTemplates([body] + ptl)

    # Use multibuild e.g. if a TOC has to be created
    if context.multiBuild:
        doc.multiBuild(context.story)
    else:
        doc.build(context.story)

    # Add watermarks
    if PyPDF2:
        file_handler = None
        for bgouter in context.pisaBackgroundList:
            # If we have at least one background, then lets do it
            if bgouter:
                istream = out

                output = PyPDF2.PdfFileWriter()
                input1 = PyPDF2.PdfFileReader(istream)
                ctr = 0
                # TODO: Why do we loop over the same list again?
                # see bgouter at line 137
                for bg in context.pisaBackgroundList:
                    page = input1.getPage(ctr)
                    if (bg and not bg.notFound()
                            and (bg.mimetype == "application/pdf")):
                        file_handler = open(bg.uri, 'rb')
                        bginput = PyPDF2.PdfFileReader(file_handler)
                        pagebg = bginput.getPage(0)
                        pagebg.mergePage(page)
                        page = pagebg

                    # Todo: the else-statement doesn't make a lot of sense to me; it's just throwing warnings
                    #  on unittesting \tests. Probably we have to rewrite the whole "background-image" stuff
                    #  to deal with cases like:
                    #  Page1 .jpg background
                    #  Page1 .pdf background
                    #  Page1 .jpg background, Page2 no background
                    #  Page1 .pdf background, Page2 no background
                    #  Page1 .jpg background, Page2 .pdf background
                    #  Page1 .pdf background, Page2 .jpg background
                    #  etc.
                    #  Right now it's kind of confusing. (fbernhart)
                    # else:
                    #     log.warning(context.warning(
                    #         "Background PDF %s doesn't exist.", bg))

                    output.addPage(page)

                    ctr += 1
                out = pisaTempFile(capacity=context.capacity)
                output.write(out)
                if file_handler:
                    file_handler.close()
                # data = sout.getvalue()
                # Found a background? So leave loop after first occurence
                break
    else:
        log.warning(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()
    context.dest.write(data)  # TODO: context.dest is a tempfile as well...

    return context