Skip to content

Commit

Permalink
Merge pull request #380 from chinapandaman/PPF-379
Browse files Browse the repository at this point in the history
PPF-379: implement get PDF version and change PDF version
  • Loading branch information
chinapandaman authored Sep 28, 2023
2 parents 0630935 + 1d25aab commit 5a097b9
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion PyPDFForm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

PyPDFForm = Wrapper

__version__ = "1.3.0"
__version__ = "1.3.1"
5 changes: 5 additions & 0 deletions PyPDFForm/middleware/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
GLOBAL_FONT_SIZE = 12
GLOBAL_FONT_COLOR = (0, 0, 0)

VERSION_IDENTIFIERS = [
b"%PDF-1.0", b"%PDF-1.1", b"%PDF-1.2", b"%PDF-1.3", b"%PDF-1.4", b"%PDF-1.5", b"%PDF-1.6", b"%PDF-1.7", b"%PDF-2.0",
]
VERSION_IDENTIFIER_PREFIX = b"%PDF-"

ELEMENT_TYPES = Union[Text, Checkbox, Radio, Dropdown]
21 changes: 21 additions & 0 deletions PyPDFForm/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ def read(self) -> bytes:

return self.stream

@property
def version(self) -> Union[str, None]:
"""Gets the version of the PDF."""

for each in constants.VERSION_IDENTIFIERS:
if self.stream.startswith(each):
return each.replace(constants.VERSION_IDENTIFIER_PREFIX, b"").decode()

return None

def change_version(self, version: str) -> Wrapper:
"""Changes the version of the PDF."""

self.stream = self.stream.replace(
constants.VERSION_IDENTIFIER_PREFIX + bytes(self.version, "utf-8"),
constants.VERSION_IDENTIFIER_PREFIX + bytes(version, "utf-8"),
1
)

return self

def __add__(self, other: Wrapper) -> Wrapper:
"""Overloaded addition operator to perform merging PDFs."""

Expand Down
25 changes: 25 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,28 @@ The above script will print the following JSON schema:
"type": "object"
}
```

## Change the version of a PDF

This example demos how to change the version of a PDF to 2.0.

```python
import os

from PyPDFForm import PyPDFForm

PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM = os.path.join(
os.path.expanduser("~/Downloads"), "sample_template.pdf"
) # Change this to where you downloaded the sample PDF form

PATH_TO_FILLED_PDF_FORM = os.path.join(
os.path.expanduser("~"), "output.pdf"
) # Change this to where you wish to put your filled PDF form

with open(PATH_TO_FILLED_PDF_FORM, "wb+") as output:
output.write(
PyPDFForm(PATH_TO_DOWNLOADED_SAMPLE_PDF_FORM)
.change_version("2.0")
.read()
)
```
Binary file added pdf_samples/versions/1.0.pdf
Binary file not shown.
Binary file added pdf_samples/versions/1.1.pdf
Binary file not shown.
Binary file added pdf_samples/versions/1.2.pdf
Binary file not shown.
Binary file added pdf_samples/versions/1.3.pdf
Binary file not shown.
Binary file added pdf_samples/versions/1.4.pdf
Binary file not shown.
Binary file added pdf_samples/versions/1.5.pdf
Binary file not shown.
Binary file added pdf_samples/versions/1.6.pdf
Binary file not shown.
Binary file added pdf_samples/versions/1.7.pdf
Binary file not shown.
Binary file added pdf_samples/versions/2.0.pdf
Binary file not shown.
Binary file added pdf_samples/versions/unknown.pdf
Binary file not shown.
12 changes: 12 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,15 @@ def test_fill_right_aligned(

for _, elements in template_core.get_elements_by_page(obj.read()).items():
assert not elements


def test_version(pdf_samples):
versions = ["1.0", "1.1", "1.2", "1.3", "1.4", "1.5", "1.6", "1.7", "2.0"]

for version in versions:
obj = PyPDFForm(os.path.join(pdf_samples, "versions", f"{version}.pdf"))
assert obj.version == version
assert obj.change_version("2.0").version == "2.0"

obj = PyPDFForm(os.path.join(pdf_samples, "versions", "unknown.pdf"))
assert obj.version is None

0 comments on commit 5a097b9

Please sign in to comment.