"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "xhtml2pdf/document.py" between
xhtml2pdf-0.2.6.tar.gz and xhtml2pdf-0.2.7.tar.gz

About: xhtml2pdf is a HTML/CSS to PDF converter written in Python.

document.py  (xhtml2pdf-0.2.6):document.py  (xhtml2pdf-0.2.7)
skipping to change at line 19 skipping to change at line 19
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import io import io
import logging import logging
from reportlab.lib import pdfencrypt
from reportlab.platypus.flowables import Spacer from reportlab.platypus.flowables import Spacer
from reportlab.platypus.frames import Frame from reportlab.platypus.frames import Frame
from xhtml2pdf.builders.signs import PDFSignature
from xhtml2pdf.builders.watermarks import WaterMarks
from xhtml2pdf.context import pisaContext from xhtml2pdf.context import pisaContext
from xhtml2pdf.default import DEFAULT_CSS from xhtml2pdf.default import DEFAULT_CSS
from xhtml2pdf.parser import pisaParser from xhtml2pdf.parser import pisaParser
from xhtml2pdf.util import PyPDF3, getBox from xhtml2pdf.util import PyPDF3, getBox
from xhtml2pdf.files import pisaTempFile, cleanFiles from xhtml2pdf.files import pisaTempFile, cleanFiles
from xhtml2pdf.xhtml2pdf_reportlab import PmlBaseDoc, PmlPageTemplate from xhtml2pdf.xhtml2pdf_reportlab import PmlBaseDoc, PmlPageTemplate
from html import escape as html_escape from html import escape as html_escape
log = logging.getLogger("xhtml2pdf") log = logging.getLogger("xhtml2pdf")
skipping to change at line 77 skipping to change at line 81
if context.indexing_story: if context.indexing_story:
context.story.append(context.indexing_story) context.story.append(context.indexing_story)
# Remove anchors if they do not exist (because of a bug in Reportlab) # Remove anchors if they do not exist (because of a bug in Reportlab)
for frag, anchor in context.anchorFrag: for frag, anchor in context.anchorFrag:
if anchor not in context.anchorName: if anchor not in context.anchorName:
frag.link = None frag.link = None
return context return context
def get_encrypt_instance(data):
if data is None:
return
if isinstance(data, str):
return pdfencrypt.StandardEncryption(data)
return data
def pisaDocument(src, dest=None, path=None, link_callback=None, debug=0, def pisaDocument(src, dest=None, path=None, link_callback=None, debug=0,
default_css=None, xhtml=False, encoding=None, xml_output=None, default_css=None, xhtml=False, encoding=None, xml_output=None,
raise_exception=True, capacity=100 * 1024, context_meta=None, raise_exception=True, capacity=100 * 1024, context_meta=None,
encrypt=None, signature=None,
**kw): **kw):
log.debug("pisaDocument options:\n src = %r\n dest = %r\n path = %r\n li nk_callback = %r\n xhtml = %r\n context_meta = %r", log.debug("pisaDocument options:\n src = %r\n dest = %r\n path = %r\n li nk_callback = %r\n xhtml = %r\n context_meta = %r",
src, src,
dest, dest,
path, path,
link_callback, link_callback,
xhtml, xhtml,
context_meta) context_meta)
# Prepare simple context # Prepare simple context
skipping to change at line 113 skipping to change at line 127
doc = PmlBaseDoc( doc = PmlBaseDoc(
out, out,
pagesize=context.pageSize, pagesize=context.pageSize,
author=context.meta["author"].strip(), author=context.meta["author"].strip(),
subject=context.meta["subject"].strip(), subject=context.meta["subject"].strip(),
keywords=[x.strip() for x in keywords=[x.strip() for x in
context.meta["keywords"].strip().split(",") if x], context.meta["keywords"].strip().split(",") if x],
title=context.meta["title"].strip(), title=context.meta["title"].strip(),
showBoundary=0, showBoundary=0,
encrypt=get_encrypt_instance(encrypt),
allowSplitting=1) allowSplitting=1)
# Prepare templates and their frames # Prepare templates and their frames
if "body" in context.templateList: if "body" in context.templateList:
body = context.templateList["body"] body = context.templateList["body"]
del context.templateList["body"] del context.templateList["body"]
else: else:
x, y, w, h = getBox("1cm 1cm -1cm -1cm", context.pageSize) x, y, w, h = getBox("1cm 1cm -1cm -1cm", context.pageSize)
body = PmlPageTemplate( body = PmlPageTemplate(
id="body", id="body",
skipping to change at line 141 skipping to change at line 156
doc.addPageTemplates([body] + list(context.templateList.values())) doc.addPageTemplates([body] + list(context.templateList.values()))
# Use multibuild e.g. if a TOC has to be created # Use multibuild e.g. if a TOC has to be created
if context.multiBuild: if context.multiBuild:
doc.multiBuild(context.story) doc.multiBuild(context.story)
else: else:
doc.build(context.story) doc.build(context.story)
# Add watermarks # Add watermarks
if PyPDF3: output=io.BytesIO()
file_handler = None output, has_bg=WaterMarks.process_doc(context, out, output)
for bgouter in context.pisaBackgroundList:
# If we have at least one background, then lets do it if not has_bg:
if bgouter: output=out
istream = out if signature:
signoutput = io.BytesIO()
output = PyPDF3.PdfFileWriter() do_ok=PDFSignature.sign(output,signoutput, signature)
input1 = PyPDF3.PdfFileReader(istream) if do_ok:
ctr = 0 output=signoutput
# 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.getMimeType() == "application/pdf")
):
file_handler = open(bg.getAbsPath(), 'rb')
bginput = PyPDF3.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 wh
ole "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("PyPDF3 not installed!"))
# Get the resulting PDF and write it to the file object # Get the resulting PDF and write it to the file object
# passed from the caller # passed from the caller
if dest is None: if dest is None:
# No output file was passed - Let's use a pisaTempFile # No output file was passed - Let's use a pisaTempFile
dest = io.BytesIO() dest = io.BytesIO()
context.dest = dest context.dest = dest
data = out.getvalue() data = output.getvalue()
context.dest.write(data) # TODO: context.dest is a tempfile as well... context.dest.write(data) # TODO: context.dest is a tempfile as well...
cleanFiles() cleanFiles()
return context return context
 End of changes. 7 change blocks. 
54 lines changed or deleted 26 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)