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

TST: Add tests for Writer #772

Merged
merged 4 commits into from
Apr 17, 2022
Merged
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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ coverage.xml
docs/_build/

# Files generated by some of the scripts
dont_commit_merged.pdf
dont_commit_writer.pdf
dont_commit_*.pdf
PyPDF2-output.pdf
Image9.png
PyPDF2_pdfLocation.txt
12 changes: 11 additions & 1 deletion PyPDF2/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,17 @@ def addBookmarkDict(self, bookmark, parent=None):

return bookmarkRef

def addBookmark(self, title, pagenum, parent=None, color=None, bold=False, italic=False, fit='/Fit', *args):
def addBookmark(
self,
title,
pagenum,
parent=None,
color=None,
bold=False,
italic=False,
fit='/Fit',
*args
):
"""
Add a bookmark to this PDF file.

Expand Down
8 changes: 8 additions & 0 deletions Tests/test_pagerange.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ def test_str(page_range, expected):
assert str(PageRange(page_range)) == expected


@pytest.mark.parametrize(
"page_range,expected",
[(slice(0, 5), "PageRange('0:5')"), (slice(0, 5, 2), "PageRange('0:5:2')")],
)
def test_repr(page_range, expected):
assert repr(PageRange(page_range)) == expected


def test_equality_other_objectc():
pr1 = PageRange(slice(0, 5))
pr2 = "PageRange(slice(0, 5))"
Expand Down
67 changes: 60 additions & 7 deletions Tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,69 @@ def test_get_page_of_encrypted_file():


@pytest.mark.parametrize(
"src,expected",
"src,expected,expected_method",
[
("form.pdf", {"foo": ""}),
("form_acrobatReader.pdf", {"foo": "Bar"}),
("form_evince.pdf", {"foo": "bar"}),
(
"form.pdf",
{"foo": ""},
{"foo": {"/DV": "", "/FT": "/Tx", "/T": "foo", "/V": ""}},
),
(
"form_acrobatReader.pdf",
{"foo": "Bar"},
{"foo": {"/DV": "", "/FT": "/Tx", "/T": "foo", "/V": "Bar"}},
),
(
"form_evince.pdf",
{"foo": "bar"},
{"foo": {"/DV": "", "/FT": "/Tx", "/T": "foo", "/V": "bar"}},
),
],
)
def test_form(src, expected):
def test_get_form(src, expected, expected_method):
"""Check if we can read out form data."""
src = os.path.join(RESOURCE_ROOT, src)
pdf = PdfFileReader(src)
fields = pdf.getFormTextFields()
reader = PdfFileReader(src)
fields = reader.getFormTextFields()
assert fields == expected

fields = reader.getFields()
assert fields == expected_method


@pytest.mark.parametrize(
"src,page_nb",
[
("form.pdf", 0),
("pdflatex-outline.pdf", 2),
],
)
def test_get_page_number(src, page_nb):
src = os.path.join(RESOURCE_ROOT, src)
reader = PdfFileReader(src)
page = reader.pages[page_nb]
assert reader.getPageNumber(page) == page_nb


@pytest.mark.parametrize(
"src,expected",
[
("form.pdf", None),
],
)
def test_get_page_layout(src, expected):
src = os.path.join(RESOURCE_ROOT, src)
reader = PdfFileReader(src)
assert reader.getPageLayout() == expected


@pytest.mark.parametrize(
"src,expected",
[
("form.pdf", "/UseNone"),
],
)
def test_get_page_mode(src, expected):
src = os.path.join(RESOURCE_ROOT, src)
reader = PdfFileReader(src)
assert reader.getPageMode() == expected
198 changes: 187 additions & 11 deletions Tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ def test_writer_operations():
writer.addBlankPage()
assert exc.value.args == ()
writer.insertPage(page, 1)
writer.removeText()
writer.insertPage(reader_outline.pages[0], 0)
writer.addBookmarkDestination(page)
writer.addBookmark("A bookmark", 0)
# output.addNamedDestination("A named destination", 1)
writer.removeLinks()
# assert output.getNamedDestRoot() == ['A named destination', IndirectObject(9, 0, output)]
writer.addBlankPage()
Expand All @@ -49,8 +46,8 @@ def test_writer_operations():
writer.insertBlankPage(width=100, height=100)
writer.insertBlankPage() # without parameters

# This gives "KeyError: '/Contents'" - is that a bug?
# output.removeImages()
# TODO: This gives "KeyError: '/Contents'" - is that a bug?
# writer.removeImages()

writer.addMetadata({"author": "Martin Thoma"})

Expand All @@ -65,15 +62,22 @@ def test_writer_operations():
os.remove(tmp_path)


def test_remove_images():
pdf_path = os.path.join(RESOURCE_ROOT, "side-by-side-subfig.pdf")
@pytest.mark.parametrize(
"input_path,ignoreByteStringObject",
[
("side-by-side-subfig.pdf", False),
("reportlab-inline-image.pdf", True),
],
)
def test_remove_images(input_path, ignoreByteStringObject):
pdf_path = os.path.join(RESOURCE_ROOT, input_path)

reader = PdfFileReader(pdf_path)
writer = PdfFileWriter()

page = reader.pages[0]
writer.insertPage(page, 0)
writer.removeImages()
writer.removeImages(ignoreByteStringObject=ignoreByteStringObject)

# finally, write "output" to PyPDF2-output.pdf
tmp_filename = "dont_commit_writer_removed_image.pdf"
Expand All @@ -82,7 +86,36 @@ def test_remove_images():

with open(tmp_filename, "rb") as input_stream:
reader = PdfFileReader(input_stream)
assert "Lorem ipsum dolor sit amet" in reader.getPage(0).extractText()
if input_path == "side-by-side-subfig.pdf":
assert "Lorem ipsum dolor sit amet" in reader.getPage(0).extractText()

# Cleanup
os.remove(tmp_filename)


@pytest.mark.parametrize(
"input_path,ignoreByteStringObject",
[
("side-by-side-subfig.pdf", False),
("side-by-side-subfig.pdf", True),
("reportlab-inline-image.pdf", False),
("reportlab-inline-image.pdf", True),
],
)
def test_remove_text(input_path, ignoreByteStringObject):
pdf_path = os.path.join(RESOURCE_ROOT, input_path)

reader = PdfFileReader(pdf_path)
writer = PdfFileWriter()

page = reader.pages[0]
writer.insertPage(page, 0)
writer.removeText(ignoreByteStringObject=ignoreByteStringObject)

# finally, write "output" to PyPDF2-output.pdf
tmp_filename = "dont_commit_writer_removed_text.pdf"
with open(tmp_filename, "wb") as output_stream:
writer.write(output_stream)

# Cleanup
os.remove(tmp_filename)
Expand Down Expand Up @@ -121,8 +154,6 @@ def test_fill_form():
writer = PdfFileWriter()

page = reader.pages[0]
fields = reader.getFields()
print(fields)

writer.addPage(page)

Expand All @@ -132,3 +163,148 @@ def test_fill_form():
tmp_filename = "dont_commit_filled_pdf.pdf"
with open(tmp_filename, "wb") as output_stream:
writer.write(output_stream)


def test_encrypt():
reader = PdfFileReader(os.path.join(RESOURCE_ROOT, "form.pdf"))
writer = PdfFileWriter()

page = reader.pages[0]

writer.addPage(page)
writer.encrypt(user_pwd="userpwd", owner_pwd="ownerpwd", use_128bit=False)

# write "output" to PyPDF2-output.pdf
tmp_filename = "dont_commit_encrypted.pdf"
with open(tmp_filename, "wb") as output_stream:
writer.write(output_stream)

# Cleanup
os.remove(tmp_filename)


def test_add_bookmark():
reader = PdfFileReader(os.path.join(RESOURCE_ROOT, "pdflatex-outline.pdf"))
writer = PdfFileWriter()

for page in reader.pages:
writer.addPage(page)

bookmark = writer.addBookmark(
"A bookmark", 1, None, (255, 0, 15), True, True, "/Fit", 200, 0, None
)
writer.addBookmark("Another", 2, bookmark, None, False, False, "/Fit", 0, 0, None)

# write "output" to PyPDF2-output.pdf
tmp_filename = "dont_commit_bookmark.pdf"
with open(tmp_filename, "wb") as output_stream:
writer.write(output_stream)

# Cleanup
os.remove(tmp_filename)


def test_add_named_destination():
reader = PdfFileReader(os.path.join(RESOURCE_ROOT, "pdflatex-outline.pdf"))
writer = PdfFileWriter()

for page in reader.pages:
writer.addPage(page)

from PyPDF2.pdf import NameObject

writer.addNamedDestination(NameObject("A bookmark"), 2)

# write "output" to PyPDF2-output.pdf
tmp_filename = "dont_commit_named_destination.pdf"
with open(tmp_filename, "wb") as output_stream:
writer.write(output_stream)

# Cleanup
os.remove(tmp_filename)


def test_add_uri():
reader = PdfFileReader(os.path.join(RESOURCE_ROOT, "pdflatex-outline.pdf"))
writer = PdfFileWriter()

for page in reader.pages:
writer.addPage(page)

from PyPDF2.pdf import RectangleObject

writer.addURI(
1,
"http://www.example.com",
RectangleObject([0, 0, 100, 100]),
border=[1, 2, 3, [4]],
)
writer.addURI(
2,
"https://pypdf2.readthedocs.io/en/latest/",
RectangleObject([20, 30, 50, 80]),
border=[1, 2, 3],
)
writer.addURI(
3,
"https://pypdf2.readthedocs.io/en/latest/user/adding-pdf-annotations.html",
"[ 200 300 250 350 ]",
border=[0, 0, 0],
)
writer.addURI(
3,
"https://pypdf2.readthedocs.io/en/latest/user/adding-pdf-annotations.html",
[100, 200, 150, 250],
border=[0, 0, 0],
)

# write "output" to PyPDF2-output.pdf
tmp_filename = "dont_commit_uri.pdf"
with open(tmp_filename, "wb") as output_stream:
writer.write(output_stream)

# Cleanup
os.remove(tmp_filename)


def test_add_link():
reader = PdfFileReader(os.path.join(RESOURCE_ROOT, "pdflatex-outline.pdf"))
writer = PdfFileWriter()

for page in reader.pages:
writer.addPage(page)

from PyPDF2.pdf import RectangleObject

writer.addLink(
1,
2,
RectangleObject([0, 0, 100, 100]),
border=[1, 2, 3, [4]],
fit="/Fit",
)
writer.addLink(2, 3, RectangleObject([20, 30, 50, 80]), [1, 2, 3], "/FitH", None)
writer.addLink(
3,
0,
"[ 200 300 250 350 ]",
[0, 0, 0],
"/XYZ",
0,
0,
2,
)
writer.addLink(
3,
0,
[100, 200, 150, 250],
border=[0, 0, 0],
)

# write "output" to PyPDF2-output.pdf
tmp_filename = "dont_commit_link.pdf"
with open(tmp_filename, "wb") as output_stream:
writer.write(output_stream)

# Cleanup
# os.remove(tmp_filename)
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ You can contribute to `PyPDF2 on Github <https://github.com/py-pdf/PyPDF2>`_.
user/add-watermark
user/reading-pdf-annotations
user/adding-pdf-annotations
user/forms


.. toctree::
Expand Down
Loading