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 2 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
7 changes: 7 additions & 0 deletions .github/workflows/github-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ on:
paths-ignore:
- '**/*.md'
- '**/*.rst'
workflow_dispatch:

jobs:
tests:
Expand All @@ -29,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
32 changes: 32 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,6 +31,7 @@
)

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
Expand Down Expand Up @@ -1552,6 +1555,35 @@ def test_watermarking_speed():
assert pdf_size_in_mib < 20


@pytest.mark.enable_socket()
def test_watermark_rendering(tmp_path):
MartinThoma marked this conversation as resolved.
Show resolved Hide resolved
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)

ghostscript_binary = shutil.which("gs")
assert ghostscript_binary is not None
stefan6419846 marked this conversation as resolved.
Show resolved Hide resolved
# 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()
image_similarity(png_path, target_png_path)


@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