Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TST: Add test for correct rendering of watermarks #2130

Merged
merged 7 commits into from
Sep 3, 2023
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
6 changes: 6 additions & 0 deletions .github/workflows/github-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ jobs:
- python-version: "3.10"
use-crypto-lib: ""
steps:
- name: Update APT packages
run:
sudo apt-get update
- name: Install APT dependencies
run:
sudo apt-get install ghostscript
- name: Checkout Code
uses: actions/checkout@v3
with:
Expand Down
33 changes: 33 additions & 0 deletions tests/test_writer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Test the pypdf._writer module."""
import re
import shutil
import subprocess
from io import BytesIO
from pathlib import Path

Expand Down Expand Up @@ -29,11 +31,13 @@
)

from . import get_data_from_url, is_sublist
from .test_images import image_similarity

TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"
SAMPLE_ROOT = Path(PROJECT_ROOT) / "sample-files"
GHOSTSCRIPT_BINARY = shutil.which("gs")


def test_writer_exception_non_binary(tmp_path, caplog):
Expand Down Expand Up @@ -1575,6 +1579,35 @@ def test_watermarking_speed():
assert pdf_size_in_mib < 20


@pytest.mark.enable_socket()
@pytest.mark.skipif(GHOSTSCRIPT_BINARY is None, reason="Requires Ghostscript")
def test_watermark_rendering(tmp_path):
MartinThoma marked this conversation as resolved.
Show resolved Hide resolved
"""Ensure the visual appearance of watermarking stays correct."""
url = "https://github.com/py-pdf/pypdf/files/11985889/bg.pdf"
name = "bgwatermark.pdf"
watermark = PdfReader(BytesIO(get_data_from_url(url, name=name))).pages[0]
url = "https://github.com/py-pdf/pypdf/files/11985888/source.pdf"
name = "srcwatermark.pdf"
page = PdfReader(BytesIO(get_data_from_url(url, name=name))).pages[0]
writer = PdfWriter()
page.merge_page(watermark, over=False)
writer.add_page(page)

target_png_path = tmp_path / "target.png"
url = "https://github.com/py-pdf/pypdf/assets/96178532/d5c72d0e-7047-4504-bbf6-bc591c80d7c0"
name = "dstwatermark.png"
target_png_path.write_bytes(get_data_from_url(url, name=name))

pdf_path = tmp_path / "out.pdf"
png_path = tmp_path / "out.png"
writer.write(pdf_path)

# False positive: https://github.com/PyCQA/bandit/issues/333
subprocess.run([GHOSTSCRIPT_BINARY, "-sDEVICE=pngalpha", "-o", png_path, pdf_path]) # noqa: S603
assert png_path.is_file()
assert image_similarity(png_path, target_png_path) >= 0.95


@pytest.mark.enable_socket()
def test_da_missing_in_annot():
url = "https://github.com/py-pdf/pypdf/files/12136285/Building.Division.Permit.Application.pdf"
Expand Down