Attach Files to Your PDFs

This guide shows how to embed files inside PDFs with PDFCrowd's HTML to PDF API. A report can carry its source spreadsheet, an invoice its XML data, or a document its supporting files. Recipients can extract the files from the PDF using a reader that supports attachments.

Attach local files

Call addAttachment() once for each file, before conversion. The example converts report.html into a PDF with data.csv and notes.txt embedded as attachments. These names represent the HTML document and supporting files in your application; each attachment must be a nonempty local file.

The example uses Python. Attachments are available through all supported client libraries and the HTTP API.

To run it, install the Python client and set API credentials in the API_USERNAME and API_KEY environment variables. Both can be set to "demo" for testing.

import os

import pdfcrowd

client = pdfcrowd.HtmlToPdfClient(
    os.environ["API_USERNAME"],
    os.environ["API_KEY"],
)
client.setPageSize("A4")
client.addAttachment("data.csv")
client.addAttachment("notes.txt")

client.convertFileToFile("report.html", "report.pdf")

For a webpage input, the final conversion call would be:

client.convertUrlToFile("https://your-domain.example/report/", "report.pdf")

Attachment paths refer to files on the machine running the client. The client uploads them with the conversion request. Embedding another PDF keeps it as a separate attached file; its pages are not added to the main document.

Open and check the attachments

Open the resulting PDF in a reader with an attachments panel. Check the filenames, extract the files, and open them in the appropriate applications. A browser's PDF viewer may display the main document without exposing its attachments; download the PDF and use another reader if needed.

For automated checks, extract the embedded files and compare their contents with the source files. This verifies that the PDF carries the intended data, not just the expected filenames.

Include attachments in PDF/A-3

For an archival PDF containing arbitrary file types, use a PDF/A-3 conformance level. With a newly initialized client, this variation creates PDF/A-3b with one CSV attachment:

client.setConformance("PDF/A-3b")
client.addAttachment("data.csv")

client.convertFileToFile("report.html", "report-archive.pdf")

Choose the level required by the receiving system. The PDF/A and tagged PDFs guide explains the output settings and how to validate the result.

For an invoice, the attachment might be invoice-data.xml. Embedding XML in PDF/A-3 is only one part of an e-invoice format: its XML profile, metadata, attachment relationship, and validation rules also need to match the recipient's requirements.

Supported APIs and output settings

The attachment option is also available in the PDF to PDF API and Image to PDF API. Use their own conversion methods and call addAttachment() for each supporting file before conversion.

Regular PDFs can combine attachments with password protection. PDF/A output cannot be encrypted. The attachment option requires API client 6.7.0 or later and converter 24.04 or later; see the reference for availability and constraints.

In WordPress

In the Save as PDF plugin, open Expert Settings → PDF Format and use the Attachment option. It takes a path to a nonempty file on the WordPress server. For archival output, choose a compatible Conformance level in the same section.

Common PDF attachment use cases

  • Reports and audits: include the spreadsheet or CSV behind the figures.
  • Document packages: carry supporting terms, drawings, or datasheets with the main PDF.
  • Source and rendered versions: keep the original document alongside its PDF rendering.