-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement uncompress functionality for PDF files (#75)
Co-authored-by: Lucas Cimon <[email protected]>
- Loading branch information
Showing
7 changed files
with
125 additions
and
8 deletions.
There are no files selected for viewing
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
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,52 @@ | ||
"""Module for uncompressing PDF content streams.""" | ||
|
||
import zlib | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
from pypdf import PdfReader, PdfWriter | ||
from pypdf.generic import IndirectObject, PdfObject | ||
|
||
|
||
def main(pdf: Path, output: Path) -> None: | ||
reader = PdfReader(pdf) | ||
writer = PdfWriter() | ||
|
||
for page in reader.pages: | ||
if "/Contents" in page: | ||
contents: Optional[PdfObject] = page["/Contents"] | ||
if isinstance(contents, IndirectObject): | ||
contents = contents.get_object() | ||
if contents is not None: | ||
if isinstance(contents, list): | ||
for content in contents: | ||
if isinstance(content, IndirectObject): | ||
decompress_content_stream(content) | ||
elif isinstance(contents, IndirectObject): | ||
decompress_content_stream(contents) | ||
writer.add_page(page) | ||
|
||
with open(output, "wb") as fp: | ||
writer.write(fp) | ||
|
||
orig_size = pdf.stat().st_size | ||
uncomp_size = output.stat().st_size | ||
|
||
print(f"Original Size : {orig_size:,}") | ||
print( | ||
f"Uncompressed Size: {uncomp_size:,} ({(uncomp_size / orig_size) * 100:.1f}% of original)" | ||
) | ||
|
||
|
||
def decompress_content_stream(content: IndirectObject) -> None: | ||
"""Decompress a content stream if it uses FlateDecode.""" | ||
if content.get("/Filter") == "/FlateDecode": | ||
try: | ||
compressed_data = content.get_data() | ||
uncompressed_data = zlib.decompress(compressed_data) | ||
content.set_data(uncompressed_data) | ||
del content["/Filter"] | ||
except zlib.error as error: | ||
print( | ||
f"Some content stream with /FlateDecode failed to be decompressed: {error}" | ||
) |
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
import pytest | ||
|
||
from .conftest import RESOURCES_ROOT, chdir, run_cli | ||
|
||
|
||
|
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,40 @@ | ||
"""Tests for the `uncompress` command.""" | ||
|
||
from pathlib import Path | ||
|
||
import pytest | ||
from pypdf import PdfReader | ||
from typer.testing import CliRunner | ||
|
||
from pdfly.cli import entry_point | ||
|
||
runner = CliRunner() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"input_pdf_filepath", Path("sample-files").glob("*.pdf") | ||
) | ||
def test_uncompress_all_sample_files( | ||
input_pdf_filepath: Path, tmp_path: Path | ||
) -> None: | ||
output_pdf_filepath = tmp_path / "uncompressed_output.pdf" | ||
|
||
result = runner.invoke( | ||
entry_point, | ||
["uncompress", str(input_pdf_filepath), str(output_pdf_filepath)], | ||
) | ||
|
||
assert ( | ||
result.exit_code == 0 | ||
), f"Error in uncompressing {input_pdf_filepath}: {result.output}" | ||
assert ( | ||
output_pdf_filepath.exists() | ||
), f"Output PDF {output_pdf_filepath} does not exist." | ||
|
||
reader = PdfReader(str(output_pdf_filepath)) | ||
for page in reader.pages: | ||
contents = page.get("/Contents") | ||
if contents: | ||
assert ( | ||
"/Filter" not in contents | ||
), "Content stream is still compressed" |
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