def end(self, c): attrs = self.attr name = attrs["name"] if len(c.frameList) <= 0: log.warn(c.warning("missing frame definitions for template")) pt = PmlPageTemplate( id=name, frames=c.frameList, pagesize=A4, ) pt.pisaStaticList = c.frameStaticList pt.pisaBackgroundList = c.pisaBackgroundList pt.pisaBackground = self.attr.background # self._pagesize) # pt.pml_statics = self._statics # pt.pml_draw = self._draw # pt.pml_drawing = self._drawing # pt.pml_background = attrs.background # pt.pml_bgstory = self._bgstory c.templateList[name] = pt c.template = None c.frameList = [] c.frameStaticList = []
def end(self, c): attrs = self.attr name = attrs["name"] if len(c.frameList) <= 0: log.warn(c.warning("missing frame definitions for template")) pt = PmlPageTemplate(id=name, frames=c.frameList, pagesize=A4) pt.pisaStaticList = c.frameStaticList pt.pisaBackgroundList = c.pisaBackgroundList pt.pisaBackground = self.attr.background c.templateList[name] = pt c.template = None c.frameList = [] c.frameStaticList = []
def end(self, c): attrs = self.attr name = attrs["name"] if len(c.frameList) <= 0: log.warn(c.warning("missing frame definitions for template")) pt = PmlPageTemplate( id=name, frames=c.frameList, pagesize=A4, ) pt.pisaStaticList = c.frameStaticList pt.pisaBackgroundList = c.pisaBackgroundList pt.pisaBackground = self.attr.background c.templateList[name] = pt c.template = None c.frameList = [] c.frameStaticList = []
def atPage(self, name, pseudopage, declarations): c = self.c data = {} name = name or "body" pageBorder = None if declarations: result = self.ruleset([self.selector('*')], declarations) if declarations: data = result[0].values()[0] pageBorder = data.get("-pdf-frame-border", None) if name in c.templateList: log.warn( self.c.warning("template '%s' has already been defined", name)) if "-pdf-page-size" in data: c.pageSize = xhtml2pdf.default.PML_PAGESIZES.get( str(data["-pdf-page-size"]).lower(), c.pageSize) isLandscape = False if "size" in data: size = data["size"] if type(size) is not types.ListType: size = [size] sizeList = [] for value in size: valueStr = str(value).lower() if type(value) is types.TupleType: sizeList.append(getSize(value)) elif valueStr == "landscape": isLandscape = True elif valueStr in xhtml2pdf.default.PML_PAGESIZES: c.pageSize = xhtml2pdf.default.PML_PAGESIZES[valueStr] else: log.warn(c.warning("Unknown size value for @page")) if len(sizeList) == 2: c.pageSize = tuple(sizeList) if isLandscape: c.pageSize = landscape(c.pageSize) for prop in ("margin-top", "margin-left", "margin-right", "margin-bottom", "top", "left", "right", "bottom", "width", "height"): if prop in data: c.frameList.append( self._pisaAddFrame(name, data, first=True, border=pageBorder, size=c.pageSize)) break # Frames have to be calculated after we know the pagesize frameList = [] staticList = [] for fname, static, border, x, y, w, h, fdata in c.frameList: #fix frame sizing problem. if static: x, y, w, h = getFrameDimensions(fdata, c.pageSize[0], c.pageSize[1]) x, y, w, h = getCoords(x, y, w, h, c.pageSize) if w <= 0 or h <= 0: log.warn( self.c.warning( "Negative width or height of frame. Check @frame definitions." )) frame = Frame(x, y, w, h, id=fname, leftPadding=0, rightPadding=0, bottomPadding=0, topPadding=0, showBoundary=border or pageBorder) if static: frame.pisaStaticStory = [] c.frameStatic[static] = [frame] + c.frameStatic.get(static, []) staticList.append(frame) else: frameList.append(frame) background = data.get("background-image", None) if background: #should be relative to the css file background = self.c.getFile(background, relative=self.c.cssParser.rootPath) if not frameList: log.warn( c.warning( "missing explicit frame definition for content or just static frames" )) fname, static, border, x, y, w, h, data = self._pisaAddFrame( name, data, first=True, border=pageBorder, size=c.pageSize) x, y, w, h = getCoords(x, y, w, h, c.pageSize) if w <= 0 or h <= 0: log.warn( c.warning( "Negative width or height of frame. Check @page definitions." )) frameList.append( Frame(x, y, w, h, id=fname, leftPadding=0, rightPadding=0, bottomPadding=0, topPadding=0, showBoundary=border or pageBorder)) pt = PmlPageTemplate( id=name, frames=frameList, pagesize=c.pageSize, ) pt.pisaStaticList = staticList pt.pisaBackground = background pt.pisaBackgroundList = c.pisaBackgroundList if isLandscape: pt.pageorientation = pt.LANDSCAPE c.templateList[name] = pt c.template = None c.frameList = [] c.frameStaticList = [] return {}, {}
def atPage(self, name, pseudopage, declarations): c = self.c data = {} name = name or "body" pageBorder = None if declarations: result = self.ruleset([self.selector('*')], declarations) if declarations: data = list(result[0].values())[0] pageBorder = data.get("-pdf-frame-border", None) if name in c.templateList: log.warning(self.c.warning("template '%s' has already been defined", name)) if "-pdf-page-size" in data: c.pageSize = xhtml2pdf.default.PML_PAGESIZES.get(str(data["-pdf-page-size"]).lower(), c.pageSize) isLandscape = False if "size" in data: size = data["size"] if type(size) is not list: size = [size] sizeList = [] for value in size: valueStr = str(value).lower() if type(value) is tuple: sizeList.append(getSize(value)) elif valueStr == "landscape": isLandscape = True elif valueStr in xhtml2pdf.default.PML_PAGESIZES: c.pageSize = xhtml2pdf.default.PML_PAGESIZES[valueStr] else: log.warning(c.warning("Unknown size value for @page")) if len(sizeList) == 2: c.pageSize = tuple(sizeList) if isLandscape: c.pageSize = landscape(c.pageSize) padding_top = self._getFromData(data, 'padding-top', 0, getSize) padding_left = self._getFromData(data, 'padding-left', 0, getSize) padding_right = self._getFromData(data, 'padding-right', 0, getSize) padding_bottom = self._getFromData(data, 'padding-bottom', 0, getSize) border_color = self._getFromData(data, ('border-top-color', 'border-bottom-color',\ 'border-left-color', 'border-right-color'), None, getColor) border_width = self._getFromData(data, ('border-top-width', 'border-bottom-width',\ 'border-left-width', 'border-right-width'), 0, getSize) for prop in ("margin-top", "margin-left", "margin-right", "margin-bottom", "top", "left", "right", "bottom", "width", "height"): if prop in data: c.frameList.append(self._pisaAddFrame(name, data, first=True, border=pageBorder, size=c.pageSize)) break # Frames have to be calculated after we know the pagesize frameList = [] staticList = [] for fname, static, border, x, y, w, h, fdata in c.frameList: fpadding_top = self._getFromData(fdata, 'padding-top', padding_top, getSize) fpadding_left = self._getFromData(fdata, 'padding-left', padding_left, getSize) fpadding_right = self._getFromData(fdata, 'padding-right', padding_right, getSize) fpadding_bottom = self._getFromData(fdata, 'padding-bottom', padding_bottom, getSize) fborder_color = self._getFromData(fdata, ('border-top-color', 'border-bottom-color',\ 'border-left-color', 'border-right-color'), border_color, getColor) fborder_width = self._getFromData(fdata, ('border-top-width', 'border-bottom-width',\ 'border-left-width', 'border-right-width'), border_width, getSize) if border or pageBorder: frame_border = ShowBoundaryValue() else: frame_border = ShowBoundaryValue(color=fborder_color, width=fborder_width) #fix frame sizing problem. if static: x, y, w, h = getFrameDimensions(fdata, c.pageSize[0], c.pageSize[1]) x, y, w, h = getCoords(x, y, w, h, c.pageSize) if w <= 0 or h <= 0: log.warning(self.c.warning("Negative width or height of frame. Check @frame definitions.")) frame = Frame( x, y, w, h, id=fname, leftPadding=fpadding_left, rightPadding=fpadding_right, bottomPadding=fpadding_bottom, topPadding=fpadding_top, showBoundary=frame_border) if static: frame.pisaStaticStory = [] c.frameStatic[static] = [frame] + c.frameStatic.get(static, []) staticList.append(frame) else: frameList.append(frame) background = data.get("background-image", None) if background: #should be relative to the css file background = self.c.getFile(background, relative=self.c.cssParser.rootPath) if not frameList: log.warning(c.warning("missing explicit frame definition for content or just static frames")) fname, static, border, x, y, w, h, data = self._pisaAddFrame(name, data, first=True, border=pageBorder, size=c.pageSize) x, y, w, h = getCoords(x, y, w, h, c.pageSize) if w <= 0 or h <= 0: log.warning(c.warning("Negative width or height of frame. Check @page definitions.")) if border or pageBorder: frame_border = ShowBoundaryValue() else: frame_border = ShowBoundaryValue(color=border_color, width=border_width) frameList.append(Frame( x, y, w, h, id=fname, leftPadding=padding_left, rightPadding=padding_right, bottomPadding=padding_bottom, topPadding=padding_top, showBoundary=frame_border)) pt = PmlPageTemplate( id=name, frames=frameList, pagesize=c.pageSize, ) pt.pisaStaticList = staticList pt.pisaBackground = background pt.pisaBackgroundList = c.pisaBackgroundList if isLandscape: pt.pageorientation = pt.LANDSCAPE c.templateList[name] = pt c.template = None c.frameList = [] c.frameStaticList = [] return {}, {}
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, **kw): log.debug( "pisaDocument options:\n src = %r\n dest = %r\n path = %r\n link_callback = %r\n xhtml = %r", src, dest, path, link_callback, xhtml) # Prepare simple context context = pisaContext(path, debug=debug, capacity=capacity) 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 = pisaTempFile(capacity=context.capacity) 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] + 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 pyPdf: for bgouter in context.pisaBackgroundList: # If we have at least one background, then lets do it if bgouter: istream = out output = pyPdf.PdfFileWriter() input1 = pyPdf.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 = pyPdf.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("pyPDF 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 = pisaTempFile(capacity=context.capacity) context.dest = dest data = out.getvalue( ) # TODO: That load all the tempfile in RAM - Why bother with a swapping tempfile then? context.dest.write(data) # TODO: context.dest is a tempfile as well... return context
def atPage(self, name, pseudopage, declarations): c = self.c data = {} name = name or "body" pageBorder = None if declarations: result = self.ruleset([self.selector('*')], declarations) # print "@PAGE", name, pseudopage, declarations, result if declarations: data = result[0].values()[0] pageBorder = data.get("-pdf-frame-border", None) if name in c.templateList: log.warn(self.c.warning("template '%s' has already been defined", name)) if "-pdf-page-size" in data: c.pageSize = xhtml2pdf.default.PML_PAGESIZES.get(str(data["-pdf-page-size"]).lower(), c.pageSize) isLandscape = False if "size" in data: size = data["size"] # print size, c.pageSize if type(size) is not types.ListType: size = [size] sizeList = [] for value in size: valueStr = str(value).lower() if type(value) is types.TupleType: sizeList.append(getSize(value)) elif valueStr == "landscape": isLandscape = True elif valueStr in xhtml2pdf.default.PML_PAGESIZES: c.pageSize = xhtml2pdf.default.PML_PAGESIZES[valueStr] else: log.warn(c.warning("Unknown size value for @page")) if len(sizeList) == 2: c.pageSize = sizeList if isLandscape: c.pageSize = landscape(c.pageSize) for prop in ("margin-top", "margin-left", "margin-right", "margin-bottom", "top", "left", "right", "bottom", "width", "height"): if prop in data: c.frameList.append(self._pisaAddFrame(name, data, first=True, border=pageBorder, size=c.pageSize)) break # self._drawing = PmlPageDrawing(self._pagesize) #if not c.frameList: # c.warning("missing frame definitions for template") # return {}, {} # Frames have to be calculated after we know the pagesize frameList = [] staticList = [] for fname, static, border, x, y, w, h, fdata in c.frameList: #fix frame sizing problem. if static: x, y, w, h = getFrameDimensions(fdata, c.pageSize[0], c.pageSize[1]) x, y, w, h = getCoords(x, y, w, h, c.pageSize) if w <= 0 or h <= 0: log.warn(self.c.warning("Negative width or height of frame. Check @frame definitions.")) frame = Frame( x, y, w, h, id=fname, leftPadding=0, rightPadding=0, bottomPadding=0, topPadding=0, showBoundary=border or pageBorder) if static: frame.pisaStaticStory = [] c.frameStatic[static] = [frame] + c.frameStatic.get(static, []) staticList.append(frame) else: frameList.append(frame) background = data.get("background-image", None) if background: background = self.c.getFile(background) # print background # print frameList if not frameList: # print 999 log.warn(c.warning("missing explicit frame definition for content or just static frames")) fname, static, border, x, y, w, h, data = self._pisaAddFrame(name, data, first=True, border=pageBorder, size=c.pageSize) x, y, w, h = getCoords(x, y, w, h, c.pageSize) if w <= 0 or h <= 0: log.warn(c.warning("Negative width or height of frame. Check @page definitions.")) frameList.append(Frame( x, y, w, h, id=fname, leftPadding=0, rightPadding=0, bottomPadding=0, topPadding=0, showBoundary=border or pageBorder)) pt = PmlPageTemplate( id=name, frames=frameList, pagesize=c.pageSize, ) pt.pisaStaticList = staticList pt.pisaBackground = background pt.pisaBackgroundList = c.pisaBackgroundList if isLandscape: pt.pageorientation = pt.LANDSCAPE # self._pagesize) # pt.pml_statics = self._statics # pt.pml_draw = self._draw # pt.pml_drawing = self._drawing # pt.pml_background = attrs.background # pt.pml_bgstory = self._bgstory c.templateList[name] = pt c.template = None c.frameList = [] c.frameStaticList = [] return {}, {}
def atPage(self, name, pseudopage, declarations): c = self.c data = {} name = name or "body" pageBorder = None if declarations: result = self.ruleset([self.selector('*')], declarations) # print "@PAGE", name, pseudopage, declarations, result if declarations: data = result[0].values()[0] pageBorder = data.get("-pdf-frame-border", None) if c.templateList.has_key(name): log.warn(self.c.warning("template '%s' has already been defined", name)) if data.has_key("-pdf-page-size"): c.pageSize = xhtml2pdf.default.PML_PAGESIZES.get(str(data["-pdf-page-size"]).lower(), c.pageSize) if data.has_key("size"): size = data["size"] # print size, c.pageSize if type(size) is not types.ListType: size = [size] isLandscape = False sizeList = [] for value in size: valueStr = str(value).lower() if type(value) is types.TupleType: sizeList.append(getSize(value)) elif valueStr == "landscape": isLandscape = True elif xhtml2pdf.default.PML_PAGESIZES.has_key(valueStr): c.pageSize = xhtml2pdf.default.PML_PAGESIZES[valueStr] else: log.warn(c.warning("Unknown size value for @page")) if len(sizeList) == 2: c.pageSize = sizeList if isLandscape: c.pageSize = landscape(c.pageSize) for prop in ["margin-top", "margin-left", "margin-right", "margin-bottom", "top", "left", "right", "bottom", "width", "height"]: if data.has_key(prop): c.frameList.append(self._pisaAddFrame(name, data, first=True, border=pageBorder, size=c.pageSize)) break # self._drawing = PmlPageDrawing(self._pagesize) #if not c.frameList: # c.warning("missing frame definitions for template") # return {}, {} # Frames have to be calculated after we know the pagesize frameList = [] staticList = [] for fname, static, border, x, y, w, h in c.frameList: x, y, w, h = getCoords(x, y, w, h, c.pageSize) if w <= 0 or h <= 0: log.warn(self.c.warning("Negative width or height of frame. Check @frame definitions.")) frame = Frame( x, y, w, h, id=fname, leftPadding=0, rightPadding=0, bottomPadding=0, topPadding=0, showBoundary=border or pageBorder) if static: frame.pisaStaticStory = [] c.frameStatic[static] = [frame] + c.frameStatic.get(static, []) staticList.append(frame) else: frameList.append(frame) background = data.get("background-image", None) if background: background = self.c.getFile(background) # print background # print frameList if not frameList: # print 999 log.warn(c.warning("missing explicit frame definition for content or just static frames")) fname, static, border, x, y, w, h = self._pisaAddFrame(name, data, first=True, border=pageBorder, size=c.pageSize) x, y, w, h = getCoords(x, y, w, h, c.pageSize) if w <= 0 or h <= 0: log.warn(c.warning("Negative width or height of frame. Check @page definitions.")) frameList.append(Frame( x, y, w, h, id=fname, leftPadding=0, rightPadding=0, bottomPadding=0, topPadding=0, showBoundary=border or pageBorder)) pt = PmlPageTemplate( id=name, frames=frameList, pagesize=c.pageSize, ) pt.pisaStaticList = staticList pt.pisaBackground = background pt.pisaBackgroundList = c.pisaBackgroundList # self._pagesize) # pt.pml_statics = self._statics # pt.pml_draw = self._draw # pt.pml_drawing = self._drawing # pt.pml_background = attrs.background # pt.pml_bgstory = self._bgstory c.templateList[name] = pt c.template = None c.frameList = [] c.frameStaticList = [] return {}, {}
def atPage(self, name, pseudopage, data, isLandscape, pageBorder): c = self.c name = name or "body" if name in c.templateList: log.warning( self.c.warning("template '%s' has already been defined", name)) padding_top = self._getFromData(data, 'padding-top', 0, getSize) padding_left = self._getFromData(data, 'padding-left', 0, getSize) padding_right = self._getFromData(data, 'padding-right', 0, getSize) padding_bottom = self._getFromData(data, 'padding-bottom', 0, getSize) border_color = self._getFromData( data, ('border-top-color', 'border-bottom-color', 'border-left-color', 'border-right-color'), None, getColor) border_width = self._getFromData( data, ('border-top-width', 'border-bottom-width', 'border-left-width', 'border-right-width'), 0, getSize) for prop in ("margin-top", "margin-left", "margin-right", "margin-bottom", "top", "left", "right", "bottom", "width", "height"): if prop in data: c.frameList.append( self._pisaAddFrame(name, data, first=True, border=pageBorder, size=c.pageSize)) break # Frames have to be calculated after we know the pagesize frameList = [] staticList = [] for fname, static, border, x, y, w, h, fdata in c.frameList: fpadding_top = self._getFromData(fdata, 'padding-top', padding_top, getSize) fpadding_left = self._getFromData(fdata, 'padding-left', padding_left, getSize) fpadding_right = self._getFromData(fdata, 'padding-right', padding_right, getSize) fpadding_bottom = self._getFromData(fdata, 'padding-bottom', padding_bottom, getSize) fborder_color = self._getFromData( fdata, ('border-top-color', 'border-bottom-color', 'border-left-color', 'border-right-color'), border_color, getColor) fborder_width = self._getFromData( fdata, ('border-top-width', 'border-bottom-width', 'border-left-width', 'border-right-width'), border_width, getSize) if border or pageBorder: frame_border = ShowBoundaryValue( width=int(border)) #frame_border = ShowBoundaryValue() to #frame_border = ShowBoundaryValue(width=int(border)) else: frame_border = ShowBoundaryValue(color=fborder_color, width=fborder_width) # fix frame sizing problem. if static: x, y, w, h = getFrameDimensions(fdata, c.pageSize[0], c.pageSize[1]) x, y, w, h = getCoords(x, y, w, h, c.pageSize) if w <= 0 or h <= 0: log.warning( self.c.warning( "Negative width or height of frame. Check @frame definitions." )) frame = Frame(x, y, w, h, id=fname, leftPadding=fpadding_left, rightPadding=fpadding_right, bottomPadding=fpadding_bottom, topPadding=fpadding_top, showBoundary=frame_border) if static: frame.pisaStaticStory = [] c.frameStatic[static] = [frame] + c.frameStatic.get(static, []) staticList.append(frame) else: frameList.append(frame) background = data.get("background-image", None) if background: # should be relative to the css file background = self.c.getFile(background, relative=self.c.cssParser.rootPath) if not frameList: log.warning( c.warning( "missing explicit frame definition for content or just static frames" )) fname, static, border, x, y, w, h, data = self._pisaAddFrame( name, data, first=True, border=pageBorder, size=c.pageSize) x, y, w, h = getCoords(x, y, w, h, c.pageSize) if w <= 0 or h <= 0: log.warning( c.warning( "Negative width or height of frame. Check @page definitions." )) if border or pageBorder: frame_border = ShowBoundaryValue() else: frame_border = ShowBoundaryValue(color=border_color, width=border_width) frameList.append( Frame(x, y, w, h, id=fname, leftPadding=padding_left, rightPadding=padding_right, bottomPadding=padding_bottom, topPadding=padding_top, showBoundary=frame_border)) pt = PmlPageTemplate( id=name, frames=frameList, pagesize=c.pageSize, ) pt.pisaStaticList = staticList pt.pisaBackground = background pt.pisaBackgroundList = c.pisaBackgroundList if isLandscape: pt.pageorientation = pt.LANDSCAPE c.templateList[name] = pt c.template = None c.frameList = [] c.frameStaticList = [] return {}, {}
def atPage(self, name, pseudopage, declarations): c = self.c data = {} name = name or "body" pageBorder = None if declarations: result = self.ruleset([self.selector('*')], declarations) if declarations: try: data = result[0].values()[0] except Exception: data = result[0].popitem()[1] pageBorder = data.get("-pdf-frame-border", None) if name in c.templateList: log.warn( self.c.warning("template '%s' has already been defined", name)) if "-pdf-page-size" in data: c.pageSize = xhtml2pdf.default.PML_PAGESIZES.get( str(data["-pdf-page-size"]).lower(), c.pageSize) isLandscape = False if "size" in data: size = data["size"] if type(size) is not ListType: size = [size] sizeList = [] for value in size: valueStr = str(value).lower() if type(value) is TupleType: sizeList.append(getSize(value)) elif valueStr == "landscape": isLandscape = True elif valueStr == "portrait": isLandscape = False elif valueStr in xhtml2pdf.default.PML_PAGESIZES: c.pageSize = xhtml2pdf.default.PML_PAGESIZES[valueStr] else: raise RuntimeError("Unknown size value for @page") if len(sizeList) == 2: c.pageSize = tuple(sizeList) if isLandscape: c.pageSize = landscape(c.pageSize) padding_top = self._getFromData(data, 'padding-top', 0, getSize) padding_left = self._getFromData(data, 'padding-left', 0, getSize) padding_right = self._getFromData(data, 'padding-right', 0, getSize) padding_bottom = self._getFromData(data, 'padding-bottom', 0, getSize) border_color = self._getFromData( data, ('border-top-color', 'border-bottom-color', 'border-left-color', 'border-right-color'), None, getColor) border_width = self._getFromData( data, ('border-top-width', 'border-bottom-width', 'border-left-width', 'border-right-width'), 0, getSize) for prop in ("margin-top", "margin-left", "margin-right", "margin-bottom", "top", "left", "right", "bottom", "width", "height"): if prop in data: c.frameList.append( self._pisaAddFrame(name, data, first=True, border=pageBorder, size=c.pageSize)) break # Frames have to be calculated after we know the pagesize frameList = [] staticList = [] for fname, static, border, x, y, w, h, fdata in c.frameList: fpadding_top = self._getFromData(fdata, 'padding-top', padding_top, getSize) fpadding_left = self._getFromData(fdata, 'padding-left', padding_left, getSize) fpadding_right = self._getFromData(fdata, 'padding-right', padding_right, getSize) fpadding_bottom = self._getFromData(fdata, 'padding-bottom', padding_bottom, getSize) fborder_color = self._getFromData( fdata, ('border-top-color', 'border-bottom-color', 'border-left-color', 'border-right-color'), border_color, getColor) fborder_width = self._getFromData( fdata, ('border-top-width', 'border-bottom-width', 'border-left-width', 'border-right-width'), border_width, getSize) if border or pageBorder: frame_border = ShowBoundaryValue() else: frame_border = ShowBoundaryValue(color=fborder_color, width=fborder_width) # fix frame sizing problem. if static: x, y, w, h = getFrameDimensions(fdata, c.pageSize[0], c.pageSize[1]) x, y, w, h = getCoords(x, y, w, h, c.pageSize) if w <= 0 or h <= 0: log.warn( self.c.warning( "Negative width or height of frame. Check @frame definitions." )) frame = Frame(x, y, w, h, id=fname, leftPadding=fpadding_left, rightPadding=fpadding_right, bottomPadding=fpadding_bottom, topPadding=fpadding_top, showBoundary=frame_border) if static: frame.pisaStaticStory = [] c.frameStatic[static] = [frame] + c.frameStatic.get(static, []) staticList.append(frame) else: frameList.append(frame) background = data.get("background-image", None) if background: # should be relative to the css file background = self.c.getFile(background, relative=self.c.cssParser.rootPath) if not frameList: log.warn( c.warning( "missing explicit frame definition for content or just static frames" )) fname, static, border, x, y, w, h, data = self._pisaAddFrame( name, data, first=True, border=pageBorder, size=c.pageSize) x, y, w, h = getCoords(x, y, w, h, c.pageSize) if w <= 0 or h <= 0: log.warn( c.warning( "Negative width or height of frame. Check @page definitions." )) if border or pageBorder: frame_border = ShowBoundaryValue() else: frame_border = ShowBoundaryValue(color=border_color, width=border_width) frameList.append( Frame(x, y, w, h, id=fname, leftPadding=padding_left, rightPadding=padding_right, bottomPadding=padding_bottom, topPadding=padding_top, showBoundary=frame_border)) next_page = data.get('-pdf-page-nextpage', None) pt = PmlPageTemplate(id=name, frames=frameList, pagesize=c.pageSize, autoNextPageTemplate=next_page) pt.pisaStaticList = staticList pt.pisaBackground = background pt.pisaBackgroundList = c.pisaBackgroundList if isLandscape: pt.pageorientation = pt.LANDSCAPE c.templateList[name] = pt c.template = None c.frameList = [] c.frameStaticList = [] return {}, {}
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