ENH: meta command - Add Permissions + fix version read on encrypted PDFs
Fixes #19
Description
What’s new
- pdfly meta now shows a compact, single-line Permissions field in the PDF Data table.
- Unencrypted PDFs show n/a (unencrypted).
- Encrypted PDFs show a comma-separated list of the allowed permissions (e.g., print, modify, annotate, fill-forms). If none are allowed, it shows none (all denied).
Why
- Addresses the feature request to display document permissions and follows the maintainer’s suggestion to keep it on a single line. Fixes #19.
How
- Uses PdfReader.user_access_permissions from pypdf.
- New helper _format_permissions(...)
- Also fixes header reading for encrypted PDFs by seeking to position 0 before reading the %PDF-… version.
Tests
- Manual verification included (screenshots in this PR)
For an encrypted pdf
For a normal file
Notes
- Output order of permissions is alphabetical for stability.
Checklist:
-
[x] A unit test is covering the code added / modified by this PR
-
[ ] In case of a new feature, docstrings have been added, with also some documentation in the
docs/folder -
[x] A mention of the change is present in
CHANGELOG.md -
[x] This PR is ready to be merged
By submitting this pull request, I confirm that my contribution is made under the terms of the BSD 3-Clause license.
I'm not sure if this 2nd table visible in your screenshot is needed, given that the same information is visible in the 1st table:
alr, removing the diff table and adding unittest and changelogs
Quick sanity check on the test fixture paths
I’m planning to use the following in the tests:
from pathlib import Path
rel = Path("sample-files/002-trivial-libre-office-writer/002-trivial-libre-office-writer.pdf") input_pdf = RESOURCES_ROOT / rel
Assuming RESOURCES_ROOT points to the repo root (as in tests/conftest.py), this resolves to:
…which is the expected location in the sample-files submodule. If RESOURCES_ROOT already points inside sample-files/, I’ll drop the leading sample-files/ from rel.
The test also pytest.skip(...)s if the file isn’t present, and the submodule can be initialized with:
git submodule update --init --recursive
Shout if you prefer me to reference it as:
rel = Path("002-trivial-libre-office-writer/002-trivial-libre-office-writer.pdf") input_pdf = RESOURCES_ROOT / "sample-files" / rel
Either style works; just want to match the project’s convention.
Quick sanity check on the test fixture paths [...] Either style works; just want to match the project’s convention.
Let's got for this:
SAMPLE_FILES = RESOURCES_ROOT / "sample-files"
input_pdf = SAMPLE_FILES / rel
I made a final feedback comment, but appart from that everything looks really good! 👍
I'll merge your PR once you have addressed that last point and the CI pipeline is passing.
Hey lucas, can u check now?
The unit tests are failing:
ERROR tests/test_metadata.py - ValueError: tests/test_metadata.py::test_meta_command_on_all_sample_pdfs: error raised while trying to determine id of parameter 'input_pdf' at position 0
Hi @iamrishu11 🙂 👋 We are close to the end of the month, I was just curious to know if you were willing to finish this PR as part of Hacktoberfest?