mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-27 17:54:32 +03:00
Merge pull request #5459 from radarhere/pdf_float
Do not round dimensions when saving PDF
This commit is contained in:
commit
0836e388e8
|
@ -30,7 +30,7 @@ def helper_save_as_pdf(tmp_path, mode, **kwargs):
|
||||||
with open(outfile, "rb") as fp:
|
with open(outfile, "rb") as fp:
|
||||||
contents = fp.read()
|
contents = fp.read()
|
||||||
size = tuple(
|
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
|
assert im.size == size
|
||||||
|
|
||||||
|
@ -86,6 +86,27 @@ def test_unsupported_mode(tmp_path):
|
||||||
im.save(outfile)
|
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(
|
@mark_if_feature_version(
|
||||||
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
|
pytest.mark.valgrind_known_error, "libjpeg_turbo", "2.0", reason="Known Failing"
|
||||||
)
|
)
|
||||||
|
|
|
@ -202,8 +202,8 @@ def _save(im, fp, filename, save_all=False):
|
||||||
MediaBox=[
|
MediaBox=[
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
int(width * 72.0 / resolution),
|
width * 72.0 / resolution,
|
||||||
int(height * 72.0 / resolution),
|
height * 72.0 / resolution,
|
||||||
],
|
],
|
||||||
Contents=contents_refs[pageNumber],
|
Contents=contents_refs[pageNumber],
|
||||||
)
|
)
|
||||||
|
@ -211,9 +211,9 @@ def _save(im, fp, filename, save_all=False):
|
||||||
#
|
#
|
||||||
# page contents
|
# page contents
|
||||||
|
|
||||||
page_contents = b"q %d 0 0 %d 0 0 cm /image Do Q\n" % (
|
page_contents = b"q %f 0 0 %f 0 0 cm /image Do Q\n" % (
|
||||||
int(width * 72.0 / resolution),
|
width * 72.0 / resolution,
|
||||||
int(height * 72.0 / resolution),
|
height * 72.0 / resolution,
|
||||||
)
|
)
|
||||||
|
|
||||||
existing_pdf.write_obj(contents_refs[pageNumber], stream=page_contents)
|
existing_pdf.write_obj(contents_refs[pageNumber], stream=page_contents)
|
||||||
|
|
|
@ -330,6 +330,8 @@ def pdf_repr(x):
|
||||||
return bytes(x)
|
return bytes(x)
|
||||||
elif isinstance(x, int):
|
elif isinstance(x, int):
|
||||||
return str(x).encode("us-ascii")
|
return str(x).encode("us-ascii")
|
||||||
|
elif isinstance(x, float):
|
||||||
|
return str(x).encode("us-ascii")
|
||||||
elif isinstance(x, time.struct_time):
|
elif isinstance(x, time.struct_time):
|
||||||
return b"(D:" + time.strftime("%Y%m%d%H%M%SZ", x).encode("us-ascii") + b")"
|
return b"(D:" + time.strftime("%Y%m%d%H%M%SZ", x).encode("us-ascii") + b")"
|
||||||
elif isinstance(x, dict):
|
elif isinstance(x, dict):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user