Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion Tests/test_file_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def helper_save_as_pdf(tmp_path, mode, **kwargs):
with open(outfile, "rb") as fp:
contents = fp.read()
size = tuple(
int(d) for d in contents.split(b"/MediaBox [ 0 0 ")[1].split(b"]")[0].split()
float(d) for d in contents.split(b"/MediaBox [ 0 0 ")[1].split(b"]")[0].split()
)
assert im.size == size

Expand Down Expand Up @@ -86,6 +86,27 @@ def test_unsupported_mode(tmp_path):
im.save(outfile)


def test_resolution(tmp_path):
im = hopper()

outfile = str(tmp_path / "temp.pdf")
im.save(outfile, resolution=150)

with open(outfile, "rb") as fp:
contents = fp.read()

size = tuple(
float(d)
for d in contents.split(b"stream\nq ")[1].split(b" 0 0 cm")[0].split(b" 0 0 ")
)
assert size == (61.44, 61.44)

size = tuple(
float(d) for d in contents.split(b"/MediaBox [ 0 0 ")[1].split(b"]")[0].split()
)
assert size == (61.44, 61.44)


@mark_if_feature_version(
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
)
Expand Down
10 changes: 5 additions & 5 deletions src/PIL/PdfImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,18 @@ def _save(im, fp, filename, save_all=False):
MediaBox=[
0,
0,
int(width * 72.0 / resolution),
int(height * 72.0 / resolution),
width * 72.0 / resolution,
height * 72.0 / resolution,
],
Contents=contents_refs[pageNumber],
)

#
# page contents

page_contents = b"q %d 0 0 %d 0 0 cm /image Do Q\n" % (
int(width * 72.0 / resolution),
int(height * 72.0 / resolution),
page_contents = b"q %f 0 0 %f 0 0 cm /image Do Q\n" % (
width * 72.0 / resolution,
height * 72.0 / resolution,
)

existing_pdf.write_obj(contents_refs[pageNumber], stream=page_contents)
Expand Down
2 changes: 2 additions & 0 deletions src/PIL/PdfParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ def pdf_repr(x):
return bytes(x)
elif isinstance(x, int):
return str(x).encode("us-ascii")
elif isinstance(x, float):
return str(x).encode("us-ascii")
elif isinstance(x, time.struct_time):
return b"(D:" + time.strftime("%Y%m%d%H%M%SZ", x).encode("us-ascii") + b")"
elif isinstance(x, dict):
Expand Down