PDF attachments refer to external files that can be included within a PDF document, supporting various formats such as images, XML files, Excel spreadsheets, and more. These embedded attachments enhance the main document by providing extra context or resources, allowing users to access all relevant files in a single location.
With DocuGenerate’s API, it is possible to add an attachment when calling the Generate Document endpoint by using the attach
parameter. The file can be specified as a URL or a Base64-encoded data URI. This parameter is only applicable if output_format
is .pdf
(or a PDF/A version such as .pdf/a-1b
, .pdf/a-2b
or .pdf/a-3b
).
To attach a file from a URL, specify the attach
parameter with the file’s URL, such as https://www.w3schools.com/xml/note.xml for example:
curl -X 'POST' \
'https://api.docugenerate.com/v1/document' \
-H 'accept: application/json' \
-H 'Authorization: 491c000c5fad32ed7787005b0723ad55' \
-H 'Content-Type: multipart/form-data' \
-F 'template_id=7VYocxLnIupLU3YT4iLr' \
-F 'attach=https://www.w3schools.com/xml/note.xml' \
-F 'data={ "Company Name": "Acme Corp", "Invoice No": "INV-123456", "Street Address": "123 Main St", "City": "Springfield", "State": "IL", "Zip Code": "62701", "Invoice Date": "2025-02-26", "Phone": "(555) 123-4567", "Description": "Consulting services for Q1 2025", "Quantity": "10", "Amount": "150.00", "Total": "1500.00" }' \
-F 'output_format=.pdf'
The filename of the attachment will be automatically determined from the URL, in this case note.xml. You can download a copy of the generated PDF here.
To attach a file using a Base64-encoded data URI, you can specify the filename using the name
property. Here’s an example with the Base64-encoded value of the note.xml file:
data:text/xml;name=note.xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPG5vdGU+CiAgPHRvPlRvdmU8L3RvPgogIDxmcm9tPkphbmk8L2Zyb20+CiAgPGhlYWRpbmc+UmVtaW5kZXI8L2hlYWRpbmc+CiAgPGJvZHk+RG9uJ3QgZm9yZ2V0IG1lIHRoaXMgd2Vla2VuZCE8L2JvZHk+Cjwvbm90ZT4=
This is how the corresponding API call would look, with the attach
parameter:
curl -X 'POST' \
'https://api.docugenerate.com/v1/document' \
-H 'accept: application/json' \
-H 'Authorization: 491c000c5fad32ed7787005b0723ad55' \
-H 'Content-Type: multipart/form-data' \
-F 'template_id=7VYocxLnIupLU3YT4iLr' \
-F 'attach=data:text/xml;name=note.xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPG5vdGU+CiAgPHRvPlRvdmU8L3RvPgogIDxmcm9tPkphbmk8L2Zyb20+CiAgPGhlYWRpbmc+UmVtaW5kZXI8L2hlYWRpbmc+CiAgPGJvZHk+RG9uJ3QgZm9yZ2V0IG1lIHRoaXMgd2Vla2VuZCE8L2JvZHk+Cjwvbm90ZT4=' \
-F 'data={ "Company Name": "Acme Corp", "Invoice No": "INV-123456", "Street Address": "123 Main St", "City": "Springfield", "State": "IL", "Zip Code": "62701", "Invoice Date": "2025-02-26", "Phone": "(555) 123-4567", "Description": "Consulting services for Q1 2025", "Quantity": "10", "Amount": "150.00", "Total": "1500.00" }' \
-F 'output_format=.pdf'
If no filename is provided in the data URI, the file will default to attachment with the appropriate extension based on the MIME type. For example, using the data URI below would result in naming the file attachment.xml:
data:text/xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiPz4KPG5vdGU+CiAgPHRvPlRvdmU8L3RvPgogIDxmcm9tPkphbmk8L2Zyb20+CiAgPGhlYWRpbmc+UmVtaW5kZXI8L2hlYWRpbmc+CiAgPGJvZHk+RG9uJ3QgZm9yZ2V0IG1lIHRoaXMgd2Vla2VuZCE8L2JvZHk+Cjwvbm90ZT4=
With DocuGenerate’s API, adding attachments to PDFs is simple and flexible, whether using a direct URL or a Base64-encoded data URI. This feature allows you to include additional files, such as supporting documents or data exports, directly within your PDFs.