Merge pull request #5459 from radarhere/pdf_float

Do not round dimensions when saving PDF
This commit is contained in:
Hugo van Kemenade 2021-05-02 11:16:34 +03:00 committed by GitHub
commit 0836e388e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 6 deletions

View File

@ -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
@ -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"
)

View File

@ -202,8 +202,8 @@ 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],
)
@ -211,9 +211,9 @@ def _save(im, fp, filename, save_all=False):
#
# 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)

View File

@ -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):