Skip to content

Commit

Permalink
Minor fixups in tutorial/add_*_page_with_pypdf.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Aug 20, 2023
1 parent 6ddbb82 commit ff55d98
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions tutorial/add_new_page_with_pypdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
import io, sys

from fpdf import FPDF
from PyPDF2 import PdfMerger
from pypdf import PdfReader, PdfWriter

IN_FILEPATH = sys.argv[1]
OUT_FILEPATH = sys.argv[2]
ON_PAGE_INDEX = 2 # Index at which the page will be inserted (starts at zero)


def new_page():
def build_page():
pdf = FPDF()
pdf.add_page()
pdf.set_font("times", "B", 19)
pdf.text(50, 10, "Hello World!")
return io.BytesIO(pdf.output())


merger = PdfMerger()
merger.merge(position=0, fileobj=IN_FILEPATH)
merger.merge(position=ON_PAGE_INDEX, fileobj=new_page())
merger.write(OUT_FILEPATH)
writer = PdfWriter(clone_from=IN_FILEPATH)
new_page = PdfReader(build_page()).pages[0]
writer.insert_page(new_page, index=ON_PAGE_INDEX)
writer.write(OUT_FILEPATH)
4 changes: 2 additions & 2 deletions tutorial/add_on_page_with_pypdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def new_content():
pdf.add_page()
pdf.set_font("times", "B", 30)
pdf.text(50, 150, "Hello World!")
return pdf.output()
return io.BytesIO(pdf.output())


reader = PdfReader(IN_FILEPATH)
page_overlay = PdfReader(io.BytesIO(new_content())).pages[0]
page_overlay = PdfReader(new_content()).pages[0]
reader.pages[ON_PAGE_INDEX].merge_page(page2=page_overlay)

writer = PdfWriter()
Expand Down

0 comments on commit ff55d98

Please sign in to comment.