-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Closed
Labels
is-bugFrom a users perspective, this is a bug - a violation of the expected behavior with a compliant PDFFrom a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF
Description
I am converting a PDF to A4 paper size and the output pdf has several bugs:
- Wrong colour of markup. Gone from Red to black.
- Incorrect translation of markup. It is slightly to the right whereas it should be exactly overlapping the background grid.
- Wrong scaling (see comment in the attached screenshot)
- Lack of central alignment as per transformation in code. Expectation is that there should be equal gap on top and bottom of the page, but the output has only empty space on top.
- The thick and thin lines of the background grid have been interchanged (strangest bug).
The input file is:
graph_letter.pdf
The output file is:
graph_letter_output.pdf
I am using pypdf-3.4.0 in Google Colab.
The code used is:
from pypdf import PdfReader, PdfWriter, Transformation, PageObject, PaperSize
from pypdf.generic import RectangleObject
reader = PdfReader("input.pdf")
page = reader.pages[0]
writer = PdfWriter()
A4_w = PaperSize.A4.width
A4_h = PaperSize.A4.height
# resize page to fit *inside* A4
h = float(page.mediabox.height)
w = float(page.mediabox.width)
scale_factor = min(A4_h / h, A4_w / w)
print(scale_factor)
transform = (
Transformation()
.scale(scale_factor, scale_factor)
.translate(A4_w / 2 - w * scale_factor / 2, A4_h / 2 - h * scale_factor / 2)
)
page.add_transformation(transform)
page.cropbox = RectangleObject((0, 0, A4_w, A4_h))
# merge the pages to fit inside A4
# prepare A4 blank page
page_A4 = PageObject.create_blank_page(width=A4_w, height=A4_h)
page.mediabox = page_A4.mediabox
page_A4.merge_page(page)
writer.add_page(page_A4)
writer.write("output.pdf")Please let me know if I am doing something wrong.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
is-bugFrom a users perspective, this is a bug - a violation of the expected behavior with a compliant PDFFrom a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF
