Add optional_content() context manager for screen-only / print-only content#1870
Open
eugen-goebel wants to merge 3 commits into
Open
Add optional_content() context manager for screen-only / print-only content#1870eugen-goebel wants to merge 3 commits into
eugen-goebel wants to merge 3 commits into
Conversation
Closes py-pdf#441. Adds an FPDF.optional_content() context manager that places the content drawn inside it in a PDF Optional Content Group (a "layer"), with independent on-screen and in-print visibility. This covers the screen-only and print-only images requested in the issue, and works for any content, not just images: with pdf.optional_content(on_print=False): pdf.image("background.png", x=0, y=0, w=pdf.epw) Implementation: - PDFOptionalContentGroup objects carrying a /Usage view/print state - /OCProperties in the document catalog, with an /AS usage-application array that toggles groups for the View and Print events - a /Properties entry in each page resource dictionary - an /OC marked-content sequence around the grouped content Includes a reference-PDF test, a CHANGELOG entry and a documentation page. Based on a recipe by @digidigital.
Optional Content Groups are intentionally not PDF/A-compliant, so the check-reference-pdf-files VeraPDF step flags test/optional_content.pdf. This is handled the same way as fpdf2's other non-PDF/A features.
andersonhc
requested changes
Jun 25, 2026
andersonhc
left a comment
Collaborator
There was a problem hiding this comment.
Thank you for the contribution.
Please see the comments.
| self.struct_tree_root: Optional[PDFObject] = None | ||
| self.a_f: Optional[str] = None | ||
| self.page_labels: Optional[str] = None | ||
| # snake_case "o_c_properties" serializes to "/OCProperties" (cf. a_f -> /AF): |
Collaborator
There was a problem hiding this comment.
I don't think this comment is necessary.
The serialization conversion happens for every field and is commented on the syntax.py classes.
| "6.1.10-1": "REASON: fpdf2 wants to support LZWDecode filter", | ||
| "6.1.11-1": "REASON: /EF is allowed in order for fpdf2 to be able to embed files", | ||
| "6.1.11-2": "REASON: /EmbeddedFiles is allowed in order for fpdf2 to be able to embed files", | ||
| "6.1.13-1": "REASON: fpdf2 supports Optional Content Groups (layers) via FPDF.optional_content(), which are not permitted in PDF/A", |
Collaborator
There was a problem hiding this comment.
That's interesting. PDF/A-1 prohibits optional contents - while PDF/A-2 and later ones allow it.
Can you make it raise PDFAComplianceError on optional_content() if the user selected PDFA/1?
Also add some tests in test/pdf-a.
Optional content is prohibited in PDF/A-1 but permitted from PDF/A-2 onwards, so optional_content() now raises PDFAComplianceError only when the document enforces PDF/A-1. Adds tests for the PDF/A-1 rejection and for PDF/A-2 still being allowed, and drops a redundant comment.
Author
|
Thanks for the review. Both points are addressed in the latest commit:
I kept the |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Link to issue
Closes #441.
Description of change
Adds an
FPDF.optional_content()context manager that places the content drawn inside it in a PDF Optional Content Group (a "layer"), with independent on-screen and in-print visibility. This covers the screen-only / print-only images requested in the issue, and works for any content, not just images.As discussed with @andersonhc in the issue, I went with the context-manager approach (more flexible than an
image()argument, and it keeps the implementation simple). Under the hood it adds Optional Content Group and Usage objects, a/Propertiesentry in the page resource dictionary,/OCPropertiesin the document catalog (with an/ASusage-application array for the View and Print events), and an/OCmarked-content sequence around the grouped content. The minimum PDF version is raised to 1.5.Demo
Testing strategy
Adds
test/test_optional_content.pywith a reference-PDF test (assert_pdf_equal) rendering screen-only, print-only and always-visible content in one document. Documents without optional content are byte-identical to before.Checklist
docs/folderCHANGELOG.mdBy submitting this pull request, I confirm that my contribution is made under the terms of the GNU LGPL 3.0 license.