|
2 | 2 | import json |
3 | 3 | import logging |
4 | 4 | import math |
| 5 | +import shutil |
| 6 | +import subprocess |
5 | 7 | from copy import deepcopy |
6 | 8 | from io import BytesIO |
7 | 9 | from pathlib import Path |
|
28 | 30 | ) |
29 | 31 |
|
30 | 32 | from . import get_data_from_url, normalize_warnings |
| 33 | +from .test_images import image_similarity |
31 | 34 |
|
32 | 35 | TESTS_ROOT = Path(__file__).parent.resolve() |
33 | 36 | PROJECT_ROOT = TESTS_ROOT.parent |
34 | 37 | RESOURCE_ROOT = PROJECT_ROOT / "resources" |
35 | 38 | SAMPLE_ROOT = PROJECT_ROOT / "sample-files" |
| 39 | +GHOSTSCRIPT_BINARY = shutil.which("gs") |
36 | 40 |
|
37 | 41 |
|
38 | 42 | def get_all_sample_files(): |
@@ -1504,3 +1508,67 @@ def __getitem__(self, item) -> Any: |
1504 | 1508 | page[NameObject("/Resources")] = resources |
1505 | 1509 | with mock.patch.object(none_reference, "get_object", return_value=None): |
1506 | 1510 | assert page.extract_text() == "" |
| 1511 | + |
| 1512 | + |
| 1513 | +@pytest.mark.enable_socket |
| 1514 | +def test_scale_by(): |
| 1515 | + """Tests for #3487""" |
| 1516 | + url = "https://github.com/user-attachments/files/22685841/input.pdf" |
| 1517 | + name = "issue3487.pdf" |
| 1518 | + reader = PdfReader(BytesIO(get_data_from_url(url, name=name))) |
| 1519 | + |
| 1520 | + original_box = RectangleObject((0, 0, 595.275604, 841.88974)) |
| 1521 | + expected_box = RectangleObject((0.0, 0.0, 297.637802, 420.94487)) |
| 1522 | + for page in reader.pages: |
| 1523 | + assert page.artbox == original_box |
| 1524 | + assert page.bleedbox == original_box |
| 1525 | + assert page.cropbox == original_box |
| 1526 | + assert page.mediabox == original_box |
| 1527 | + assert page.trimbox == original_box |
| 1528 | + |
| 1529 | + page.scale_by(0.5) |
| 1530 | + assert page.artbox == expected_box |
| 1531 | + assert page.bleedbox == expected_box |
| 1532 | + assert page.cropbox == expected_box |
| 1533 | + assert page.mediabox == expected_box |
| 1534 | + assert page.trimbox == expected_box |
| 1535 | + |
| 1536 | + |
| 1537 | +@pytest.mark.enable_socket |
| 1538 | +@pytest.mark.skipif(GHOSTSCRIPT_BINARY is None, reason="Requires Ghostscript") |
| 1539 | +def test_box_rendering(tmp_path): |
| 1540 | + """Tests for issue #3487.""" |
| 1541 | + url = "https://github.com/user-attachments/files/22685841/input.pdf" |
| 1542 | + name = "issue3487.pdf" |
| 1543 | + reader = PdfReader(BytesIO(get_data_from_url(url, name=name))) |
| 1544 | + writer = PdfWriter() |
| 1545 | + |
| 1546 | + for page in reader.pages: |
| 1547 | + page.scale_by(0.5) |
| 1548 | + writer.add_page(page) |
| 1549 | + |
| 1550 | + target_png_path = tmp_path / "target.png" |
| 1551 | + url = "https://github.com/user-attachments/assets/e9c2271c-bfc3-4a6f-8c91-ffefa24502e2" |
| 1552 | + name = "issue3487.png" |
| 1553 | + target_png_path.write_bytes(get_data_from_url(url, name=name)) |
| 1554 | + |
| 1555 | + pdf_path = tmp_path / "out.pdf" |
| 1556 | + writer.write(pdf_path) |
| 1557 | + |
| 1558 | + for box in ["Art", "Bleed", "Crop", "Media", "Trim"]: |
| 1559 | + png_path = tmp_path / f"{box}.png" |
| 1560 | + # False positive: https://github.com/PyCQA/bandit/issues/333 |
| 1561 | + subprocess.run( # noqa: S603 |
| 1562 | + [ |
| 1563 | + GHOSTSCRIPT_BINARY, |
| 1564 | + f"-dUse{box}Box", |
| 1565 | + "-dFirstPage=1", |
| 1566 | + "-dLastPage=1", |
| 1567 | + "-sDEVICE=pngalpha", |
| 1568 | + "-o", |
| 1569 | + png_path, |
| 1570 | + pdf_path, |
| 1571 | + ] |
| 1572 | + ) |
| 1573 | + assert png_path.is_file(), box |
| 1574 | + assert image_similarity(png_path, target_png_path) >= 0.95, box |
0 commit comments