Skip to content

Commit

Permalink
Merge pull request #84 from cs50/pypdf-updates
Browse files Browse the repository at this point in the history
Use PdfReader and PdfMerger
  • Loading branch information
rongxin-liu authored Dec 28, 2022
2 parents 747d6ec + c021f7c commit 98b7cbc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: actions/setup-python@v4
with:
python-version: '3.6'
python-version: '3.10'

- name: Run tests
run: |
Expand Down
16 changes: 8 additions & 8 deletions render50
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_for_filename, guess_lexer
from pygments.lexers.special import TextLexer
from pygments.styles import get_all_styles
from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter
from PyPDF2 import PdfReader, PdfFileWriter
from requests.exceptions import RequestException
from tempfile import mkstemp
from textwrap import fill
Expand Down Expand Up @@ -335,13 +335,13 @@ def concatenate(output, inputs):
"""Concatenate (PDF) inputs side by side."""

# Read files
readers = list(map(PdfFileReader, inputs))
readers = list(map(PdfReader, inputs))

# Render blank page, inferring size from first input's first page
temp = mkstemp()
size = "{}pt {}pt".format(readers[0].getPage(0).mediaBox[2], readers[0].getPage(0).mediaBox[3])
write(temp[1], [blank(size)], False)
page = PdfFileReader(temp[1]).getPage(0)
page = PdfReader(temp[1]).getPage(0)

# Concatenate files side by side
writer = PdfFileWriter()
Expand Down Expand Up @@ -548,13 +548,13 @@ def render(filename, size, align="right", browser=False, fontSize="10pt", margin
def write(output, documents, echo=True):
"""
Write documents to output as PDF.
https://doc.courtbouillon.org/weasyprint/stable/api_reference.html#weasyprint.HTML.write_pdf
https://github.com/Kozea/WeasyPrint/issues/212#issuecomment-52408306
https://github.com/Kozea/WeasyPrint/issues/1662#issuecomment-1226058917
"""
if documents:
merger = PdfFileMerger()
for document in documents:
merger.append(PdfFileReader(io.BytesIO(document.write_pdf())))
merger.write(output)
pages = [page for document in documents for page in document.pages]
documents[0].copy(pages).write_pdf(output)

if echo:
cprint("Rendered {}.".format(output), "green")
return True
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
python_requires=">=3.6",
scripts=["render50"],
url="https://github.com/cs50/render50",
version="9.0.2"
version="9.0.3"
)

0 comments on commit 98b7cbc

Please sign in to comment.