Add hide_icon option to file attachment annotations - #1924
Conversation
PDF viewers render a default icon (a paperclip, a pushpin, ...) for a file attachment annotation. Add a hide_icon parameter to FPDF.file_attachment_annotation() that gives the annotation an empty normal appearance stream, so conforming viewers draw no icon while the file stays embedded and reachable through the annotation. Cf. issue py-pdf#561.
|
The Vera PDF check is failing: This happens because the current implementation of hide_icon creates a normal appearance that is not valid for PDF/A: For a FileAttachment annotation, the /N entry of the appearance dictionary must be an appearance stream. The representation currently used by hide_icon therefore isn't PDF/A compliant. To proceed with this PR, we need to:
|
| h: float = 1, | ||
| name: Optional[FileAttachmentAnnotationName | str] = None, | ||
| flags: tuple[AnnotationFlag | str, ...] = DEFAULT_ANNOT_FLAGS, | ||
| hide_icon: bool = False, |
There was a problem hiding this comment.
I believe appearance or something similar would be better here than hide_icon, since we can do another enhancement later to add custom appearances for the icon.
I believe we should add to enums.py:
class FileAttachmentAppearance(CoerciveEnum):
DEFAULT = "DEFAULT"
HIDDEN = "HIDDEN" # I'm not sure if HIDDEN or BLANK is the best term herethen the parameter becomes:
| hide_icon: bool = False, | |
| appearance: FileAttachmentAppearance | str = FileAttachmentAppearance.DEFAULT, |
Later we could expand appearance to accept a DrawingContext or an image (SVG, PNG, etc)
|
Thanks for digging into the VeraPDF failure. I pushed both changes: added 6.5.3-6 to scripts/verapdf-ignore.json, and hide_icon now raises PDFAComplianceError when the document enforces PDF/A, so the empty appearance stream only ever lands in regular PDFs. Added a test for the new guard too. |
Nice. The PDF validation is passing now - the only job failing is the Can you also check the other review comments I made? Thanks. |
|
Thanks. Ran black 26.3.1 on |
|
Reworked per your review:
|
|
@allcontributors please add @ChrisJr404 for code |
|
I've put up a pull request to add @ChrisJr404! 🎉 |
This adds a
hide_iconparameter toFPDF.file_attachment_annotation()so the default icon a viewer draws for an attachment (the paperclip, pushpin, etc.) can be suppressed while the file itself stays embedded and reachable, which is what was asked for in #561.The mechanism is the portable one from the PDF spec: when
hide_icon=True, the annotation gets an empty normal appearance stream (/AP << /N ... >>) pointing at a blank Form XObject. Conforming readers render the appearance stream instead of their built-in icon, and since it is empty, nothing is drawn. The/FSfile specification is untouched, so the attachment can still be opened or extracted from the annotation.The default is
False, so existing output is unchanged. I wired the appearance stream up at output time (once the XObject has an object id) next to where embedded files are added, and kept the mechanism generic on the annotation itself in case other annotation types want appearance streams later.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.