Skip to content

Output pdf has wrong colour, incorrect translation of markup, and wrong scaling #1615

@zain910128

Description

@zain910128

I am converting a PDF to A4 paper size and the output pdf has several bugs:

  1. Wrong colour of markup. Gone from Red to black.
  2. Incorrect translation of markup. It is slightly to the right whereas it should be exactly overlapping the background grid.
  3. Wrong scaling (see comment in the attached screenshot)
  4. 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.
  5. 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

A side by side comparison is:
comparison

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    is-bugFrom a users perspective, this is a bug - a violation of the expected behavior with a compliant PDF

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions