Add Attachments

IronPDF for Python can embed file attachments within a PDF document and remove them programmatically, using the Attachments collection on any PdfDocument object.

Getting Started

Load or render a PdfDocument, then use Attachments.AddAttachment to embed binary data as a named attachment. Use Attachments.RemoveAttachment to remove an attachment by reference.

Understanding the Code

  • pdf.Attachments.AddAttachment(name, data): Embeds a file attachment in the PDF with the given name. The second argument accepts a byte[] (binary data). In this example, pdf.BinaryData provides the raw bytes of a second loaded PdfDocument.
    • Returns an attachment reference object that can be used later with RemoveAttachment.
  • my_pdf.Attachments.RemoveAttachment(attachment): Removes a previously added attachment from the document using the reference returned by AddAttachment.
  • SaveAs(path): Saves the final document, with all attachments embedded or removed as configured.

Use Cases

Attachments are useful for including source data alongside a report PDF — for example, embedding the raw spreadsheet, XML data file, or original HTML template that was used to generate the document. Embedded attachments are portable and travel with the PDF file.

Ready to Get Started?
Version: 2026.6 just released
Still Scrolling Icon

Still Scrolling?

Want proof fast?
run a sample watch your HTML become a PDF.