Attaching files to a PDF

New in version 3.0.

You can attach (or if you prefer, embed) any file to a PDF, including other PDFs. As a quick example, let’s attach pikepdf’s README.md file to one of its test files.

In [1]: from pikepdf import Pdf, AttachedFileSpec, Name, Dictionary, Array

In [2]: from pathlib import Path

In [3]: pdf = Pdf.open('../tests/resources/fourpages.pdf')

In [4]: filespec = AttachedFileSpec.from_filepath(pdf, Path('../README.md'))

In [5]: pdf.attachments['README.md'] = filespec

In [6]: pdf.attachments
Out[6]: <pikepdf._qpdf.Attachments with 1 attached files>

This creates an attached file named README.md, which holds the data in filespec. Now we can retrieve the data.

In [7]: pdf.attachments['README.md']
Out[7]: <pikepdf._qpdf.AttachedFileSpec for 'README.md', description ''>

In [8]: file = pdf.attachments['README.md'].get_file()

In [9]: file.read_bytes()[:50]
Out[9]: b'<!-- SPDX-FileCopyrightText: 2022 James R. Barlow '

If the data used to create an attachment is in memory:

In [10]: memfilespec = AttachedFileSpec(pdf, b'Some text', mime_type='text/plain')

In [11]: pdf.attachments['plain.txt'] = memfilespec

General notes on attached files

  • If the main PDF is encrypted, any embedded files will be encrypted with the same encryption settings.

  • PDF viewers tend to display attachment filenames in alphabetical order. Use prefixes if you want to control the display order.

  • The AttachedFileSpec will capture all of the data when created, so the file object used to create the data can be closed.

  • Each attachment is a pikepdf.AttachedFileSpec. An attachment usually contains only one pikepdf.AttachedFile, but might contain multiple objects of this type. Usually, multiple versions are used to provide different versions of the same file for alternate platforms, such as Windows and macOS versions of a file. Newer PDFs rarely provide multiple versions.

How to find attachments in a PDF viewer

Your PDF viewer should have an attachments panel that shows available attachments.

Screenshot of attachments panel in Acrobat DC on Windows

Attachments in Adobe Acrobat DC.

Attachments added to Pdf.attachments will be shown here.

You may find it useful to set pdf.root.PageMode = Name.UseAttachments. This tells the PDF viewer to open a pane that lists all attachments in the PDF. Note that it is up to the PDF viewer to implement and honor this request.

Creating attachment annotations

You can also create PDF Annotations and Actions that contain attached files.

Here is an example of an annotation that displays an icon. Clicking the icon prompt the user to view the attached document.

In [12]: pdf = Pdf.open('../tests/resources/fourpages.pdf')

In [13]: filespec = AttachedFileSpec.from_filepath(pdf, Path('../README.md'))

In [14]: pushpin = Dictionary(
   ....:     Type=Name.Annot,
   ....:     Subtype=Name.FileAttachment,
   ....:     Name=Name.GraphPushPin,
   ....:     FS=filespec.obj,
   ....:     Rect=[2*72, 9*72, 3*72, 10*72],
   ....: )
   ....: 

In [15]: pdf.pages[0].Annots = pdf.make_indirect(Array([
   ....:     pushpin
   ....: ]))
   ....: 

Files that are referenced as Annotations and Actions do not need to be added to Pdf.attachments. If they are, the file will be attached twice.