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=
The attach
parameter also supports an array of files, allowing you to attach multiple files to a single PDF. You can mix URLs and Base64-encoded data URIs within the same array.
Here’s an example of attaching multiple files using an array of URLs:
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", "https://pdfobject.com/pdf/sample.pdf"]' \
-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 generated PDF document has two attachments, an XML file named note.xml and a PDF file named sample.pdf. You can download a copy of the generated PDF here.
You can also combine multiple URLs and data URIs in the same API call:
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 'attach[]=https://pdfobject.com/pdf/sample.pdf' \
-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 files will be attached to the PDF in the order they appear in the array. Each file follows the same naming conventions as single file attachments - URLs determine filenames automatically, while data URIs can specify custom names using the name
property.
If you are generating multiple documents by setting single_file
to false
, DocuGenerate will create a .zip
file containing all the generated documents. Each document within the .zip
file corresponds to a separate data item and will maintain the export format you have chosen.
In this case, the files specified by the attach
parameter will be attached to each generated document. This ensures that supplementary content is consistently included across all generated files.
With DocuGenerate’s API, adding attachments to PDFs is simple and flexible, whether using a single file or multiple files, and whether using direct URLs or Base64-encoded data URIs. This feature allows you to include additional files, such as supporting documents or data exports, directly within your PDFs.