def test_add_named_destination(): reader = PdfReader(os.path.join(RESOURCE_ROOT, "pdflatex-outline.pdf")) writer = PdfWriter() for page in reader.pages: writer.add_page(page) from PyPDF2.generic import NameObject writer.add_named_destination(NameObject("A named dest"), 2) writer.add_named_destination(NameObject("A named dest2"), 2) from PyPDF2.generic import IndirectObject assert writer.get_named_dest_root() == [ "A named dest", IndirectObject(7, 0, writer), "A named dest2", IndirectObject(10, 0, writer), ] # 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_encrypt(use_128bit): reader = PdfReader(os.path.join(RESOURCE_ROOT, "form.pdf")) writer = PdfWriter() page = reader.pages[0] orig_text = page.extract_text() writer.add_page(page) writer.encrypt(user_pwd="userpwd", owner_pwd="ownerpwd", use_128bit=use_128bit) # write "output" to PyPDF2-output.pdf tmp_filename = "dont_commit_encrypted.pdf" with open(tmp_filename, "wb") as output_stream: writer.write(output_stream) with open(tmp_filename, "rb") as input_stream: data = input_stream.read() assert b"foo" not in data reader = PdfReader(tmp_filename, password="******") new_text = reader.pages[0].extract_text() assert reader.metadata.get("/Producer") == "PyPDF2" assert new_text == orig_text # Cleanup os.remove(tmp_filename)
def test_write_metadata(): pdf_path = os.path.join(RESOURCE_ROOT, "crazyones.pdf") reader = PdfReader(pdf_path) writer = PdfWriter() for page in reader.pages: writer.add_page(page) metadata = reader.metadata writer.add_metadata(metadata) writer.add_metadata({"/Title": "The Crazy Ones"}) # finally, write data to PyPDF2-output.pdf tmp_filename = "dont_commit_writer_added_metadata.pdf" with open(tmp_filename, "wb") as output_stream: writer.write(output_stream) # Check if the title was set reader = PdfReader(tmp_filename) metadata = reader.metadata assert metadata.get("/Title") == "The Crazy Ones" # Cleanup os.remove(tmp_filename)
def test_regression_issue670(): filepath = os.path.join(RESOURCE_ROOT, "crazyones.pdf") reader = PdfReader(filepath, strict=False) for _ in range(2): writer = PdfWriter() writer.add_page(reader.pages[0]) with open("dont_commit_issue670.pdf", "wb") as f_pdf: writer.write(f_pdf)
def test_pdf_header(): writer = PdfWriter() assert writer.pdf_header == b"%PDF-1.3" reader = PdfReader(os.path.join(RESOURCE_ROOT, "crazyones.pdf")) writer.add_page(reader.pages[0]) assert writer.pdf_header == b"%PDF-1.5" writer.pdf_header = b"%PDF-1.6" assert writer.pdf_header == b"%PDF-1.6"
def test_remove_child_in_tree(): pdf = os.path.join(RESOURCE_ROOT, "form.pdf") tree = TreeObject() reader = PdfReader(pdf) writer = PdfWriter() writer.add_page(reader.pages[0]) writer.add_bookmark("foo", pagenum=0) obj = writer._objects[-1] tree.add_child(obj, writer) tree.remove_child(obj) tree.add_child(obj, writer) tree.empty_tree()
def test_fill_form(): reader = PdfReader(os.path.join(RESOURCE_ROOT, "form.pdf")) writer = PdfWriter() page = reader.pages[0] writer.add_page(page) writer.update_page_form_field_values(writer.pages[0], {"foo": "some filled in text"}, flags=1) # write "output" to PyPDF2-output.pdf tmp_filename = "dont_commit_filled_pdf.pdf" with open(tmp_filename, "wb") as output_stream: writer.write(output_stream)
def test_add_bookmark(): reader = PdfReader(os.path.join(RESOURCE_ROOT, "pdflatex-outline.pdf")) writer = PdfWriter() for page in reader.pages: writer.add_page(page) bookmark = writer.add_bookmark("A bookmark", 1, None, (255, 0, 15), True, True, "/Fit", 200, 0, None) writer.add_bookmark("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_link(): reader = PdfReader(os.path.join(RESOURCE_ROOT, "pdflatex-outline.pdf")) writer = PdfWriter() for page in reader.pages: writer.add_page(page) from PyPDF2.generic import RectangleObject writer.add_link( 1, 2, RectangleObject([0, 0, 100, 100]), border=[1, 2, 3, [4]], fit="/Fit", ) writer.add_link(2, 3, RectangleObject([20, 30, 50, 80]), [1, 2, 3], "/FitH", None) writer.add_link( 3, 0, "[ 200 300 250 350 ]", [0, 0, 0], "/XYZ", 0, 0, 2, ) writer.add_link( 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)
def test_overlay(base_path, overlay_path): if base_path.startswith("http"): base_path = BytesIO(get_pdf_from_url(base_path, name="tika-935981.pdf")) else: base_path = os.path.join(PROJECT_ROOT, base_path) reader = PdfReader(base_path) writer = PdfWriter() reader_overlay = PdfReader(os.path.join(PROJECT_ROOT, overlay_path)) overlay = reader_overlay.pages[0] for page in reader.pages: page.merge_page(overlay) writer.add_page(page) with open("dont_commit_overlay.pdf", "wb") as fp: writer.write(fp) # Cleanup os.remove("dont_commit_overlay.pdf")
def test_add_uri(): reader = PdfReader(os.path.join(RESOURCE_ROOT, "pdflatex-outline.pdf")) writer = PdfWriter() for page in reader.pages: writer.add_page(page) from PyPDF2.generic import RectangleObject writer.add_uri( 1, "http://www.example.com", RectangleObject([0, 0, 100, 100]), border=[1, 2, 3, [4]], ) writer.add_uri( 2, "https://pypdf2.readthedocs.io/en/latest/", RectangleObject([20, 30, 50, 80]), border=[1, 2, 3], ) writer.add_uri( 3, "https://pypdf2.readthedocs.io/en/latest/user/adding-pdf-annotations.html", "[ 200 300 250 350 ]", border=[0, 0, 0], ) writer.add_uri( 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_basic_features(): pdf_path = os.path.join(RESOURCE_ROOT, "crazyones.pdf") reader = PdfReader(pdf_path) writer = PdfWriter() assert len(reader.pages) == 1 # add page 1 from input1 to output document, unchanged writer.add_page(reader.pages[0]) # add page 2 from input1, but rotated clockwise 90 degrees writer.add_page(reader.pages[0].rotate(90)) # add page 3 from input1, but first add a watermark from another PDF: page3 = reader.pages[0] watermark_pdf = pdf_path watermark = PdfReader(watermark_pdf) page3.merge_page(watermark.pages[0]) writer.add_page(page3) # add page 4 from input1, but crop it to half size: page4 = reader.pages[0] page4.mediabox.upper_right = ( page4.mediabox.right / 2, page4.mediabox.top / 2, ) writer.add_page(page4) # add some Javascript to launch the print window on opening this PDF. # the password dialog may prevent the print dialog from being shown, # comment the the encription lines, if that's the case, to try this out writer.add_js("this.print({bUI:true,bSilent:false,bShrinkToFit:true});") # encrypt your new PDF and add a password password = "******" writer.encrypt(password) # finally, write "output" to PyPDF2-output.pdf tmp_path = "PyPDF2-output.pdf" with open(tmp_path, "wb") as output_stream: writer.write(output_stream) # cleanup os.remove(tmp_path)