Skip to main content

API Reference

LabraDocs comes with two API endpoints for converting docx documents to PDF/UA. The first endpoint should be used if you prefer specifying the input data in separate files, the second endpoint should be used if you prefer to specify the input data in a single JSON file.

/docx2pdfua and /docx2docx

/docx2docx only available from v1.6.0

Request

  • Conversion with separate input files
  • Method: POST
  • Input:
    • template - the docx file to be converted (required)
    • data - a JSON file containing the data to be inserted into the template (optional)
    • images - an optional array of images to be inserted into the template (optional)
  • Response: application/pdf
  • Return codes:
    • 200 - conversion successful
    • 400 - bad request
    • 500 - internal server error
curl --location --request POST 'http://localhost:8000/docx2pdfua' \
--form 'template=@"/home/pathfindr/example/generic.docx"' \
--form 'data=@"/home/pathfindr/example/generic.json"' \
--form 'images=@"/home/pathfindr/example/logo.png"' \
--form 'images=@"/home/pathfindr/example/another-image.png"'
tip

Adding /extract_mergefields to the end of the endpoints will return a list of all the mergefields in the template

note

You may download the demo files here: generic.docx, generic.json and the logo.png files here.

JSON file format

In the JSON file you have to specify the data and image mappings. For the data you may use nesting such as arrays and objects. To reference data structures in the docx file, you have to use "." to separate the different nesting levels.

See the following example for easier understanding

{
"CUSTOMERNAME": "Jens Jensen", // can be referenced as «CUSTOMERNAME»
"CUSTOMERADDRESS": "...", // can be referenced as «CUSTOMERADDRESS»
"CUSTOMER": {
"NAME": "Jens Jensen", // can be referenced as «CUSTOMER.NAME»
"ADDRESS": "..." // can be referenced as «CUSTOMER.ADDRESS»
},
"LOGO": { // the name of the IMAGE, which is used in the docx document to reference the image
"src": "logo.png", // the name of the file submitted in the images array
"title": "Logo", // title for screenreaders
"alt": "Logo" // alt text for screenreaders
},
"ANOTHERIMAGE": {
"src": "another-image.png",
"title": "Another image",
"alt": "Another image"
}
}
tip

All names are case sensitivie, so make sure to use the same case in the docx document and in the JSON file.

/docx2pdfua_packed and /docx2docx_packed

/docx2docx_packed only available from v1.6.0

Request

  • Conversion with a single input file
  • Method: POST
  • Input: all files packed together into one JSON file
  • Response: application/pdf
  • Return codes:
    • 200 - conversion successful

    • 400 - bad request

    • 500 - internal server error

curl --location --request POST 'http://localhost:8000/docx2pdfua_packed' \
--header 'Content-Type: application/json' \
--data-binary '@/home/pathfindr/dev/docx2pdfua/example/generic.packed.json'
tip

Adding /extract_mergefields to the end of endpoints will return a list of all the mergefields in the template

note

You may download the demo file here: generic.packed.json

JSON file format

When using a single file, the JSON file should contain all the images templates and data that has to be merged together before converting it to a PDF/UA file.

The JSON file contains the following fields:

  • template: json object with the property "templateBase64": containing the base64 encoded word document
  • data: json object with arrays, objects, strings, numbers and booleans
  • images: array of json objects containing the "name" (string) to be replaced, the base 64 encoded image in the "base64" property, the "title" and the "alt" text for the image itself
note

In this setup all three fields are required.

See the example below

{
"template": { templateBase64: "base64 encoded word document" },
"data": {
"CUSTOMERNAME": "Jens Jensen", // can be referenced as «CUSTOMERNAME»
"CUSTOMERADDRESS": "...", // can be referenced as «CUSTOMERADDRESS»
"CUSTOMER": {
"NAME": "Jens Jensen", // can be referenced as «CUSTOMER.NAME»
"ADDRESS": "..." // can be referenced as «CUSTOMER.ADDRESS»
},
"ITEMS: [ // can be referenced in loops
{
"NAME": "Item 1",
"PRICE": 10
},
{
"NAME": "Item 2",
"PRICE": 20
}
]

},
"images": [
{
"name": "LOGO", // the name of the IMAGE, which is used in the docx document to reference the image
"base64": "base64 encoded image", // the base64 encoded image
"title": "Logo", // title for screenreaders
"alt": "Logo" // alt text for screenreaders
},
{
"name": "ANOTHERIMAGE",
"base64": "base64 encoded image",
"title": "Another image",
"alt": "Another image"
}
]
}
tip

You may download the demo file here: generic.packed.json

If you want to learn more about how to use mergefields, please continue start out at the beginning.

/pdf_fill

available from v1.7.0

Request

  • Filling in PDF forms with separate input files
  • Method: POST
  • Input:
    • template - the docx file to be converted (required)
    • data - a JSON file containing the data to be inserted into the template (optional)
  • Response: application/pdf
  • Return codes:
    • 200 - conversion successful
    • 400 - bad request
    • 500 - internal server error
curl --location --request POST 'http://localhost:8000/pdf_fill' \
--form 'template=@"/home/pathfindr/example/generic.pdf"' \
--form 'data=@"/home/pathfindr/example/generic.json"' \

JSON file format

In the JSON file you have to specify the data mappings. For the data you may use nesting such as arrays and objects. See the following example for easier understanding

{
"CUSTOMER NAME": "Jens Jensen", // can be referenced as «CUSTOMER NAME»
"CUSTOMER ADDRESS": "...", // can be referenced as «CUSTOMER ADDRESS»

}
tip

Adding /extract_mergefields to the end of endpoints will return a list of all the mergefields in the template

note

Data fields support spaces, but no nested data is allowed. Keys like "person.cpr" or "person.address" will only work if the data key actually contains a "." character and will not work if it's an object, so this is invalid:

{"person": {"address": "some address", "cpr": "123456-1234"}}.

tip

All names are case sensitivie, so make sure to use the same case in the docx document and in the JSON file.

/pdf_fill_packed

available from v1.7.0

Request

  • Filling in PDF forms with a single input file
  • Method: POST
  • Input: all files packed together into one JSON file
  • Response: application/pdf
  • Return codes:
    • 200 - conversion successful

    • 400 - bad request

    • 500 - internal server error

curl --location --request POST 'http://localhost:8000/pdf_fill_packed' \
--header 'Content-Type: application/json' \
--data-binary '@/home/pathfindr/dev/docx2pdfua/example/generic.packed.json'
tip

Adding /extract_mergefields to the end of endpoints will return a list of all the mergefields in the template

JSON file format

When using a single file, the JSON file should contain all the images templates and data that has to be merged together before converting it to a PDF/UA file.

The JSON file contains the following fields:

  • template: json object with the property "templateBase64": containing the base64 encoded word document
  • data: json object with arrays, objects, strings, numbers and booleans
note

In this setup both fields are required.

See the example below

{
"template": { templateBase64: "base64 encoded word document" },
"data": {
"CUSTOMER NAME": "Jens Jensen", // can be referenced as «CUSTOMER NAME»
"CUSTOMER ADDRESS": "...", // can be referenced as «CUSTOMER ADDRESS»
}
}
note

Data fields support spaces, but no nested data is allowed. Keys like "person.cpr" or "person.address" will only work if the data key actually contains a "." character and will not work if it's an object, so this is invalid:

{"person": {"address": "some address", "cpr": "123456-1234"}}.

tip

All names are case sensitivie, so make sure to use the same case in the docx document and in the JSON file.

If you want to learn more about how to use mergefields, please continue start out at the beginning.