Skip to content

Commit

Permalink
TST: Add test for correct rendering of watermarks (#2130)
Browse files Browse the repository at this point in the history
This adds a test for the correct rendering of watermarked files.

Closes #2112
  • Loading branch information
stefan6419846 authored Sep 3, 2023
1 parent af41173 commit b8d3bea
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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):
"""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

0 comments on commit b8d3bea

Please sign in to comment.