From 274aa2d29f1af7d7209b888efe7cf3214a0903ac Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Wed, 28 Dec 2022 13:08:17 -0500 Subject: [PATCH 1/3] use PdfReader and PdfMerger --- render50 | 10 +++++----- setup.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/render50 b/render50 index bf41b03..4879b2c 100755 --- a/render50 +++ b/render50 @@ -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 PdfMerger, PdfReader, PdfFileWriter from requests.exceptions import RequestException from tempfile import mkstemp from textwrap import fill @@ -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() @@ -551,9 +551,9 @@ def write(output, documents, echo=True): https://doc.courtbouillon.org/weasyprint/stable/api_reference.html#weasyprint.HTML.write_pdf """ if documents: - merger = PdfFileMerger() + merger = PdfMerger() for document in documents: - merger.append(PdfFileReader(io.BytesIO(document.write_pdf()))) + merger.append(PdfReader(io.BytesIO(document.write_pdf()))) merger.write(output) if echo: cprint("Rendered {}.".format(output), "green") diff --git a/setup.py b/setup.py index e6ef36c..1e051e5 100644 --- a/setup.py +++ b/setup.py @@ -18,5 +18,5 @@ python_requires=">=3.6", scripts=["render50"], url="https://github.com/cs50/render50", - version="9.0.2" + version="9.0.3" ) From f6f252cf5de72454aaa0fd6b545282f4372be173 Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Wed, 28 Dec 2022 13:12:34 -0500 Subject: [PATCH 2/3] updated setup-python action versions --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f02cb71..a8b2527 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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: | From c021f7c2b36814b42ae1ee95f680ce17af7200a6 Mon Sep 17 00:00:00 2001 From: Rongxin Liu Date: Wed, 28 Dec 2022 13:37:13 -0500 Subject: [PATCH 3/3] use alternative method for PDFs merging --- render50 | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/render50 b/render50 index 4879b2c..1e5416f 100755 --- a/render50 +++ b/render50 @@ -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 PdfMerger, PdfReader, PdfFileWriter +from PyPDF2 import PdfReader, PdfFileWriter from requests.exceptions import RequestException from tempfile import mkstemp from textwrap import fill @@ -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 = PdfMerger() - for document in documents: - merger.append(PdfReader(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