Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional pisaDocument argument to set metadata #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions tests/test_document.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from __future__ import unicode_literals

import io
import tempfile

from nose import tools

from PyPDF2 import PdfFileReader

from xhtml2pdf.document import pisaDocument


HTML_CONTENT = """<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<h1> Hello, world! </div>

<p>
The quick red fox jumps over the lazy brown dog.
</p>
</div>
</body>
</html>"""


METADATA = {
"author": "MyCorp Ltd.",
"title": "My Document Title",
"subject": "My Document Subject",
"keywords": "pdf, documents",
}


def _compare_pdf_metadata(pdf_file, assertion):

# Ensure something has been written
tools.assert_not_equal(pdf_file.tell(), 0)

# Rewind to the start of the file to read the pdf and get the
# docuemnt's metadata
pdf_file.seek(0)
pdf_reader = PdfFileReader(pdf_file)
pdf_info = pdf_reader.documentInfo

# Check the received metadata matches the expected metadata
for original_key in METADATA:
actual_key = "/{}".format(original_key.capitalize())
actual_value = pdf_info[actual_key]
expected_value = METADATA[original_key]

assertion(actual_value, expected_value)


def test_document_creation_without_metadata():
with tempfile.TemporaryFile() as pdf_file:
pisaDocument(
src=io.StringIO(HTML_CONTENT),
dest=pdf_file
)
_compare_pdf_metadata(pdf_file, tools.assert_not_equal)


def test_document_creation_with_metadata():
with tempfile.TemporaryFile() as pdf_file:
pisaDocument(
src=io.StringIO(HTML_CONTENT),
dest=pdf_file,
context_meta=METADATA
)
_compare_pdf_metadata(pdf_file, tools.assert_equal)
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ envlist =
[testenv]
commands =
{envpython} -c "from reportlab import Version; print('%s %s' % ('Reportlab Version', Version))"
nosetests --with-xunit --with-coverage --cover-package=xhtml2pdf
{envpython} -m nose.core --with-xunit --with-coverage --cover-package=xhtml2pdf
deps =
Pillow>=2.0
coverage
Expand Down
24 changes: 18 additions & 6 deletions xhtml2pdf/document.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import io

from xhtml2pdf.context import pisaContext
from xhtml2pdf.default import DEFAULT_CSS
from xhtml2pdf.parser import pisaParser
Expand Down Expand Up @@ -72,24 +74,30 @@ def pisaStory(src, path=None, link_callback=None, debug=0, default_css=None,

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",
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)
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 = pisaTempFile(capacity=context.capacity)
out = io.BytesIO()

doc = PmlBaseDoc(
out,
Expand Down Expand Up @@ -165,10 +173,14 @@ def pisaDocument(src, dest=None, path=None, link_callback=None, debug=0,

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

data = out.getvalue() # TODO: That load all the tempfile in RAM - Why bother with a swapping tempfile then?
data = out.getvalue()

if isinstance(dest, io.BytesIO):
data = data.encode("utf-8")

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

return context