-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: support PDFs with pages of varying sizes
- Loading branch information
Showing
5 changed files
with
65 additions
and
11 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
Test for PDFs with different page sizes. | ||
""" | ||
|
||
import os | ||
|
||
import pytest | ||
|
||
from pdf_watermark.handler import add_watermark_from_options | ||
from pdf_watermark.options import DrawingOptions, FilesOptions, GridOptions | ||
from pdf_watermark.watermark import DEFAULTS | ||
from tests.utils import assert_pdfs_are_close | ||
|
||
INPUT = "tests/fixtures/different_sizes_input.pdf" | ||
OUTPUT = "output.pdf" | ||
FIXTURE = "tests/fixtures/different_sizes_output.pdf" | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def cleanup(): | ||
yield | ||
os.remove(OUTPUT) | ||
|
||
|
||
DRAWING_OPTIONS_FIXTURE = DrawingOptions( | ||
watermark="watermark", | ||
opacity=DEFAULTS.opacity, | ||
angle=DEFAULTS.angle, | ||
text_color=DEFAULTS.text_color, | ||
text_font=DEFAULTS.text_font, | ||
text_size=DEFAULTS.text_size, | ||
unselectable=DEFAULTS.unselectable, | ||
image_scale=DEFAULTS.image_scale, | ||
save_as_image=DEFAULTS.save_as_image, | ||
dpi=DEFAULTS.dpi, | ||
) | ||
|
||
FILES_OPTIONS_FIXTURE = FilesOptions(INPUT, OUTPUT) | ||
|
||
GRID_OPTIONS_FIXTURE = GridOptions( | ||
horizontal_boxes=DEFAULTS.horizontal_boxes, | ||
vertical_boxes=DEFAULTS.vertical_boxes, | ||
margin=DEFAULTS.margin, | ||
) | ||
|
||
|
||
def test_different_page_sizes(): | ||
add_watermark_from_options( | ||
files_options=FILES_OPTIONS_FIXTURE, | ||
drawing_options=DRAWING_OPTIONS_FIXTURE, | ||
specific_options=GRID_OPTIONS_FIXTURE, | ||
) | ||
assert_pdfs_are_close(OUTPUT, FIXTURE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import numpy as np | ||
from pdf2image import convert_from_path | ||
|
||
|
||
def assert_pdfs_are_close(path_1: str, path_2: str, epsilon: float = 1e-10): | ||
"""This function checks that two PDFs are close enough. We chose to convert the PDFs to images and then compare their L1 norms, because other techniques (hashing for instance) might break easily.""" | ||
images_1 = convert_from_path(path_1) | ||
images_2 = convert_from_path(path_2) | ||
|
||
for im1, im2 in zip(images_1, images_2): | ||
assert np.sum(np.abs(np.array(im1) - np.array(im2))) < epsilon |