Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Options:
--dpi INTEGER Render resolution
--output-dir DIRECTORY Diff output directory (saves text diffs and visual diff images on failure)
-v, --verbose Increase verbosity
--skip-compare-text Skip text content comparison
--version Show the version and exit.
--help Show this message and exit.
```
Expand Down
10 changes: 7 additions & 3 deletions src/diffpdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def diffpdf(
dpi: int = 96,
output_dir: str | Path | None = None,
verbose: bool = False,
skip_compare_text: bool = False,
) -> bool:
ref_path = Path(reference) if isinstance(reference, str) else reference
actual_path = Path(actual) if isinstance(actual, str) else actual
Expand All @@ -34,9 +35,12 @@ def diffpdf(
if not check_page_counts(ref_path, actual_path):
return False

logger.info("[3/4] Checking text content...")
if not check_text_content(ref_path, actual_path, out_path):
return False
if skip_compare_text:
logger.info("[3/4] Skipping text content check")
else:
logger.info("[3/4] Checking text content...")
if not check_text_content(ref_path, actual_path, out_path):
return False

logger.info("[4/4] Checking visual content...")
if not check_visual_content(ref_path, actual_path, threshold, dpi, out_path):
Expand Down
10 changes: 9 additions & 1 deletion src/diffpdf/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
is_flag=True,
help="Increase verbosity",
)
@click.option(
"--skip-compare-text",
is_flag=True,
help="Skip text content comparison (useful when different render engines produce identical-looking PDFs with different text extraction order)",
)
@click.version_option(package_name="diffpdf")
def cli(
reference: Path,
Expand All @@ -36,10 +41,13 @@ def cli(
dpi: int,
output_dir: Path | None,
verbose: bool,
skip_compare_text: bool,
) -> None:
"""Compare two PDF files for structural, textual, and visual differences."""
try:
if diffpdf(reference, actual, threshold, dpi, output_dir, verbose):
if diffpdf(
reference, actual, threshold, dpi, output_dir, verbose, skip_compare_text
):
sys.exit(0)
else:
sys.exit(1)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ def test_api(ref_pdf_rel, actual_pdf_rel, should_pass):
assert result == should_pass


def test_skip_compare_text():
ref_pdf = TEST_ASSETS_DIR / "fail/1-letter-diff-A.pdf"
actual_pdf = TEST_ASSETS_DIR / "fail/1-letter-diff-B.pdf"

# Without skip: fails at text stage
assert diffpdf(ref_pdf, actual_pdf) is False
# With skip: text stage bypassed, fails at visual stage (letter difference is visible)
assert diffpdf(ref_pdf, actual_pdf, skip_compare_text=True) is False


def test_skip_compare_text_multiplatform():
ref_pdf = TEST_ASSETS_DIR / "pass/multiplatform-diff-A.pdf"
actual_pdf = TEST_ASSETS_DIR / "pass/multiplatform-diff-B.pdf"

# Multiplatform PDFs already pass with text comparison
assert diffpdf(ref_pdf, actual_pdf, skip_compare_text=True) is True


def test_text_diff_output(tmp_path):
ref_pdf = TEST_ASSETS_DIR / "fail/1-letter-diff-A.pdf"
actual_pdf = TEST_ASSETS_DIR / "fail/1-letter-diff-B.pdf"
Expand Down
13 changes: 13 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ def test_cli_with_output_dir():
assert Path("./diff").exists()


def test_skip_compare_text_flag():
runner = CliRunner()
result = runner.invoke(
cli,
[
str(TEST_ASSETS_DIR / "pass/multiplatform-diff-A.pdf"),
str(TEST_ASSETS_DIR / "pass/multiplatform-diff-B.pdf"),
"--skip-compare-text",
],
)
assert result.exit_code == 0


def test_verbose_flag():
runner = CliRunner()
result = runner.invoke(
Expand Down