/usr/lib/python2.7/dist-packages/sx/pisa3/pisa_document.py is in python-pisa 3.0.32-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | # -*- coding: ISO-8859-1 -*-
#############################################
## (C)opyright by Dirk Holtwick, 2002-2007 ##
## All rights reserved ##
#############################################
__reversion__ = "$Revision: 20 $"
__author__ = "$Author: holtwick $"
__date__ = "$Date: 2007-10-09 12:58:24 +0200 (Di, 09 Okt 2007) $"
from pisa_context import pisaContext
from pisa_parser import pisaParser
from pisa_util import *
from pisa_reportlab import *
from pisa_default import DEFAULT_CSS
from reportlab.platypus.flowables import Spacer
#import os
#import types
#import cgi
#import logging
log = logging.getLogger("ho.pisa")
def pisaErrorDocument(dest, c):
out = pisaTempFile(capacity=c.capacity)
out.write("<p style='background-color:red;'><strong>%d error(s) occured:</strong><p>" % c.err)
for mode, line, msg, code in c.log:
if mode=="error":
out.write("<pre>%s in line %d: %s</pre>" % (mode, line, cgi.escape(msg)))
out.write("<p><strong>%d warning(s) occured:</strong><p>" % c.warn)
for mode, line, msg, code in c.log:
if mode=="warning":
out.write("<p>%s in line %d: %s</p>" % (mode, line, cgi.escape(msg)))
return pisaDocument(out.getvalue(), dest, raise_exception=False)
def pisaStory(
src,
path = None,
link_callback = None,
debug = 0,
default_css = None,
xhtml = False,
encoding = None,
c = None,
xml_output = None,
**kw):
# Prepare Context
if not c:
c = pisaContext(path, debug=debug)
c.pathCallback = link_callback
# Use a default set of CSS definitions to get an expected output
if default_css is None:
default_css = DEFAULT_CSS
# Parse and fill the story
pisaParser(src, c, default_css, xhtml, encoding, xml_output)
#if 0:
# import reportlab.pdfbase.pdfmetrics as pm
# pm.dumpFontData()
# Avoid empty documents
if not c.story:
c.story = [Spacer(1,1)]
# c.addPara(force=True)
# Remove anchors if they do not exist (because of a bug in Reportlab)
for frag, anchor in c.anchorFrag:
if anchor not in c.anchorName:
frag.link = None
return c
def pisaDocument(
src,
dest = None,
path = None,
link_callback = None,
debug = 0,
show_error_as_pdf = False,
default_css = None,
xhtml = False,
encoding = None,
xml_output = None,
raise_exception = True,
capacity = 100 * 1024, # -1,
**kw):
c = None
if show_error_as_pdf:
raise_exception = False
try:
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
c = pisaContext(path, debug=debug, capacity=capacity)
c.pathCallback = link_callback
if dest is None:
dest = pisaTempFile(capacity=c.capacity)
c.dest = dest
# Build story
c = pisaStory(src, path, link_callback, debug, default_css, xhtml, encoding, c=c, xml_output=xml_output)
# Buffer PDF into memory
out = pisaTempFile(capacity=c.capacity)
doc = PmlBaseDoc(
out,
pagesize = c.pageSize,
author = c.meta["author"].strip(),
subject = c.meta["subject"].strip(),
keywords = [x.strip() for x in c.meta["keywords"].strip().split(",") if x],
title = c.meta["title"].strip(),
showBoundary = 0,
allowSplitting = 1)
# XXX It is not possible to access PDF info, because it is private in canvas
# doc.info.producer = "pisa <http://www.holtwick.it>"
# Prepare templates and their frames
if c.templateList.has_key("body"):
body = c.templateList["body"]
del c.templateList["body"]
else:
x, y, w, h = getBox("1cm 1cm -1cm -1cm", c.pageSize)
body = PmlPageTemplate(
id="body",
frames=[
Frame(x, y, w, h,
id = "body",
leftPadding = 0,
rightPadding = 0,
bottomPadding = 0,
topPadding = 0)],
pagesize = c.pageSize)
# print body.frames
# print [body] + c.templateList.values()
doc.addPageTemplates([body] + c.templateList.values())
# Use multibuild e.g. if a TOC has to be created
if c.multiBuild:
doc.multiBuild(c.story)
else:
doc.build(c.story)
# Add watermarks
if pyPdf:
for bgouter in c.pisaBackgroundList:
# If we have at least one background, then lets do it
if bgouter:
istream = out
try:
output = pyPdf.PdfFileWriter()
input1 = pyPdf.PdfFileReader(istream)
ctr = 0
for bg in c.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(c.warning("Background PDF %s doesn't exist.", bg))
output.addPage(page)
ctr += 1
out = pisaTempFile(capacity=c.capacity)
output.write(out)
# data = sout.getvalue()
except Exception:
log.exception(c.error("pyPDF error"))
if raise_exception:
raise
# Found a background? So leave loop after first occurence
break
else:
log.warn(c.warning("pyPDF not installed!"))
# In web frameworks for debugging purposes maybe an output of
# errors in a PDF is preferred
if show_error_as_pdf and c and c.err:
return pisaErrorDocument(c.dest, c)
# Get the resulting PDF and write it to the file object
# passed from the caller
data = out.getvalue()
c.dest.write(data)
except:
# log.exception(c.error("Document error"))
log.exception("Document error")
c.err += 1
if raise_exception:
raise
if raise_exception and c.err:
raise Exception("Errors occured, please see log files for more informations")
return c
|