Add text, images and barcodes to a PDF with a single API request
This guide will show you how to add text, images and barcodes to an existing PDF document. To do so, we will be utilising Docamatic's /write API endpoint.
Examples of where this may be useful:
- Populate a shipping label template with an address, logo and barcode
- Populating an invoice template
- Adding a student's name to a certificate
- Watermarking an eBook
In this guide we will create a simple 4x6 shipping label template in Microsoft Word and save it as a PDF. We will then use the Docamatic API to add an address, logo and barcode to the PDF. For demonstration purposes, we will be making the API requests with Postman. Please checkout our quick guide on making test API requests with Postman: https://docamatic.com/documentation#testing-with-postman
Prerequisites
-
You must have a Docamatic account. Docamatic free plan allows 30 documents per month. You can create an account here.
-
You will need access Microsoft Word (or similar) to create the PDF template.
-
To make the test API requests you will require access to Postman (or similar). You can download Postman for free here.
Create the PDF template, Step by Step
If you want to skip this step you can download the example PDF template here.
- Open Microsoft Word and create a blank document. We need to a custom document size for our 4x6" shipping label. From the Layout tab on the menu bar, click on the Size icon and choose More Paper Sizes from the drop down list.
- In the "Page Setup" modal, set the paper Width (10.16cm or 4 inches) and Height (15.24cm or 6 inches).
- Create a simple shipping label template. Add some borders around the document and add a default "Sender" address.
- Once you have created your template, export it as a PDF. To export as a PDF, click on the File menu and select Export. Then click on the Create PDF/XPS button.
That is a basic PDF template created! You can download the template here. Next, we will use the Docamatic API to add a logo, address and barcode.
Write to the PDF template, Step by Step
Now that we have created our PDF template we can make a simple API request to the /write endpoint. The endpoint expects you to provide a source PDF document (the template) either as a URL or base64 encoded PDF. In this example, we will provide a URL to the PDF template created previously.
To position each element on the PDF, you need to specify its XY coordinates. For text elements, it is possible to perform some basic styling (bold, text, underline) and font size. Please check out the write endpoint docs for further details.
Lets get started:
-
Open Postman to make a test API request. If you are not familiar with using Postman, please follow our quick guide on making an API request with postman.
-
Next we need to build our JSON request to post to the https://docamatic.com/api/v1/write endpoint. In this example we will add a logo, an address and a barcode to the PDF template. Here is the JSON that we will post:
{
"source": "https://docamatic.s3.eu-west-1.amazonaws.com/blog/write-to-pdf-api-template.pdf",
"images": [
{
"image_url": "https://docamatic.s3-eu-west-1.amazonaws.com/assets/360_logo.png",
"image_width": 28,
"image_height": 14,
"image_x": 62,
"image_y": 12
}
],
"text": [
{
"text_value": "Casey Williams",
"font_style": "B",
"font_size": 14.0,
"text_x": 12,
"text_y": 56
},
{
"text_value": "57 Parkway, 5th Floor",
"text_x": 12,
"text_y": 62
},
{
"text_value": "New York",
"text_x": 12,
"text_y": 68
},
{
"text_value": "NY 10013",
"text_x": 12,
"text_y": 74
},
{
"text_value": "USA",
"font_style": "B",
"text_x": 12,
"text_y": 80
}
],
"barcodes": [
{
"barcode_value": "90337210573",
"barcode_width": 64,
"barcode_height": 22,
"barcode_x": 18,
"barcode_y": 116
}
]
}
- Paste the JSON into the "Body" panel and click Send.
Once you click Send, the request will be sent to the Docamatic API. You can then download the resulting PDF from your dashboard. Here is the end result:
Return to blog