Templates

Templates are a cornerstone of FastPDF — they serve as the blueprint for generating consistent and well-structured documents in various formats. On this page, we’ll explore the various template-related endpoints that you can utilize to manage your templates programmatically. We'll look at how to create new templates, add and delete resources such as CSS stylesheets and images, retrieve templates and related files, and delete templates as necessary. Dive in to discover how you can use templates to streamline your document generation process.

Template

The Template model contains all the information about your templates, such as their name, format, associated style files, and more.

When adding a template, the following properties are mandatory: name, format.

template_file cannot be retreived with regular template getter calls, but with a separate template file getter.

Properties

  • Name
    id
    Type
    string
    Description

    Unique identifier for the template.

  • Name
    name
    Type
    string
    Description

    The name of the template. It is displayed on your template dashboard, and it is used to name the rendered file.

  • Name
    format
    Type
    string
    Description

    The format of the template. Supported formats include docx, html, odt, ods, odp, pptx, xlsx.

  • Name
    description
    Type
    string
    Description

    A description of the template. Use it at your own convenience.

  • Name
    template_file
    Type
    TemplateFile
    Description

    The file data of the template file.

  • Name
    header_file
    Type
    TemplateFile
    Description

    The header data. HTML or plaintext. Script tags inside header/footer templates are not evaluated. Globally added page styles are not visible inside header/footer templates. Inline styles are visible.

  • Name
    footer_file
    Type
    TemplateFile
    Description

    The footer data. HTML or plaintext. Script tags inside header/footer templates are not evaluated. Globally added page styles are not visible inside header/footer templates. Inline styles are visible.

  • Name
    style_files
    Type
    array of StyleFile
    Description

    The array of style files associated with the template.

  • Name
    image_files
    Type
    array of ImageFile
    Description

    The array of image files associated with the template.

  • Name
    paper_format
    Type
    string
    Description

    The paper format for PDF rendering. Supported formats include Letter, Legal, Tabloid, Ledger, A0, A1, A2, A3, A4, A5, A6. (Default: A4).

  • Name
    landscape
    Type
    boolean
    Description

    A boolean value indicating if the PDF rendering should be in landscape orientation (true) or portrait (false). (Default: false).

  • Name
    margin_top
    Type
    integer
    Description

    The top margin for PDF rendering in cm. (Default: 2).

  • Name
    margin_bottom
    Type
    integer
    Description

    The bottom margin for PDF rendering in cm. (Default: 2).

  • Name
    margin_left
    Type
    integer
    Description

    The left margin for PDF rendering in cm. (Default: 1).

  • Name
    margin_right
    Type
    integer
    Description

    The right margin for PDF rendering in cm. (Default: 1).

  • Name
    scale
    Type
    float
    Description

    The scale for PDF rendering. Allowed value range from 0.1 to 2.0.

  • Name
    print_background
    Type
    boolean
    Description

    A boolean value indicating if background should be printed in PDF rendering. Mostly affects CSS property such as background-color, background-image, etc. (Default: true).

  • Name
    page_range
    Type
    string
    Description

    The page range for PDF rendering.

  • Name
    page_number_footer_enabled
    Type
    boolean
    Description

    A boolean value indicating whether or not page numbers should be included in the footer during PDF rendering. (Default: true).

  • Name
    date_header_enabled
    Type
    boolean
    Description

    A boolean value indicating whether or not today's date should be included in the header during PDF rendering. (Default: true).

  • Name
    date_locale
    Type
    string
    Description

    A string indicating the locale for the header date. Available locales: en_US, en_GB, zh_CN, ja_JP, fr_FR, de_DE, es_ES, pt_BR, ru_RU, ar_EG, hi_IN, it_IT (Default: en_US).

  • Name
    title_header_enabled
    Type
    boolean
    Description

    A boolean value indicating whether or not title should be included in the header during PDF rendering. (Default: true).

Example Template

{
  "id": "a056V7R7NmNRjl70",
  "name": "Invoice Template",
  "format": "html",
  "timestamp": "2023-07-10T08:30:00Z",
  "description": "A standard invoice template",
  "paper_format": "A4",
  "landscape": false,
  "margin_top": 2,
  "margin_bottom": 2,
  "margin_left": 1,
  "margin_right": 1,
  "scale": 1.0,
  "print_background": true,
  "page_number_footer_enabled": true,
  "date_header_enabled": true,
  "title_header_enabled": true
}

Special Keywords

Special variable and function keywords are available for use in your templates, headers and footers. They allow to include dates and images easily. Check them out

TemplateFile

The TemplateFile model contains the template file, including file data and its template id.

Properties

  • Name
    id
    Type
    string
    Description

    Unique identifier for the template file.

  • Name
    template_id
    Type
    string
    Description

    The id of the template associated with the file.

  • Name
    file_data
    Type
    binary
    Description

    The file data in binary format.

Example TemplateFile

{
  "id": "a056V7R7NmNRjl70",
  "template_id": "b03456R7213123",
}

StyleFile

The StyleFile model represents a style file, including the id, number, format, template id, and file data.

When adding a StyleFile, the following properties are mandatory: format.

Properties

  • Name
    id
    Type
    string
    Description

    Unique identifier for the style file.

  • Name
    format
    Type
    string
    Description

    The format of the style file. (Default: css).

  • Name
    template_id
    Type
    string
    Description

    The id of the template associated with the style file.

  • Name
    file_data
    Type
    binary
    Description

    The file data in binary format.

  • Name
    filename
    Type
    string
    Description

    The name of the file.

  • Name
    description
    Type
    string
    Description

    A description of the style file. Use it at your own convenience.

Example StyleFile

{
  "id": "ab12cd34ef56gh78",
  "format": "css",
  "template_id": "a1b2c3d4e5f6g7h8",
  "filename": "style.css",
  "description": "This style file provides the main styling for the homepage."
}

ImageFile

The ImageFile model contains all the information about an image file, including the id, filename, description, format, timestamp, template id, file data, and uri.

When adding a ImageFile, the following properties are mandatory: format.

Properties

  • Name
    id
    Type
    string
    Description

    Unique identifier for the image file.

  • Name
    format
    Type
    string
    Description

    The format of the image file.

  • Name
    template_id
    Type
    string
    Description

    The id of the template associated with the image file.

  • Name
    filename
    Type
    string
    Description

    The name of the image file.

  • Name
    description
    Type
    string
    Description

    A description of the image file. Use it at your own convenience.

  • Name
    file_data
    Type
    binary
    Description

    The file data in binary format.

  • Name
    uri
    Type
    string
    Description

    The URI of the image file. This attribute is used to include images in templates, as the src property of an image tag: <image src="{{my-image-uri}}">

Example ImageFile

{
  "id": "jkl345mno678pqr",
  "format": "jpg",
  "template_id": "a1b2c3d4e5f6g7h8",
  "filename": "image.jpg",
  "description": "This image file is used for the main banner of the homepage.",
  "uri": "my-image"
}

GET/v1/template

Get all templates

This endpoint allows you to retrieve a paginated list of all your templates. By default, all templates are returned.

Optional attributes

  • Name
    limit
    Type
    integer
    Description

    Limit the number of templates returned.

Request

GET
/v1/template
curl -G https://data.fastpdfservice.com/v1/template \
  -H "Authorization: {token}" \
  -d active=true \
  -d limit=10

Response

[
  {
    "id": "a056V7R7NmNRjl70",
    "name": "Invoice Template",
    "format": "html",
    "timestamp": "2023-07-10T08:30:00Z",
    "description": "A standard invoice template",
    "paper_format": "A4",
    "landscape": false,
    "margin_top": 2,
    "margin_bottom": 2,
    "margin_left": 1,
    "margin_right": 1,
    "scale": 1.0,
    "print_background": true,
    "page_number_footer_enabled": true,
    "date_header_enabled": true,
    "title_header_enabled": true,
    "template_file": {
      "id": "6148eb22-35a3-4f17-ae8c-e3827221be60"
    },
      "timestamp": "Wed, 12 Jul 2023 11:11:32 GMT"
    }
]

GET/v1/template/{id}

Get template

This endpoint allows you to retrieve a specific template by its id.

Path parameters

  • Name
    id
    Type
    string
    Description

    ID of the template to retrieve.

Request

GET
/v1/template/{id}
curl -G https://data.fastpdfservice.com/v1/template/{id} \
  -H "Authorization: {token}"

Response

{
  "id": "a056V7R7NmNRjl70",
  "name": "Invoice Template",
  "format": "html",
  "timestamp": "2023-07-10T08:30:00Z",
  "description": "A standard invoice template",
  "paper_format": "A4",
  "landscape": false,
  "margin_top": 2,
  "margin_bottom": 2,
  "margin_left": 1,
  "margin_right": 1,
  "scale": 1.0,
  "print_background": true,
  "page_number_footer_enabled": true,
  "date_header_enabled": true,
  "title_header_enabled": true,
  "template_file": {
    "id": "6148eb22-35a3-4f17-ae8c-e3827221be60"
  },
  "timestamp": "Wed, 12 Jul 2023 11:11:32 GMT"
}

POST/v1/template

Add template

This endpoint allows you to add a new template. Refer to the Template model at the top of this page to see which properties are included with Template objects.

Request parameters

  • Name
    file_data
    Type
    File
    Description

    The path to the template file, or the template in bytes.

    Available format includes: html, docx.

  • Name
    header_data
    Type
    File
    Description

    The facultative header template file or bytes.

  • Name
    footer_data
    Type
    File
    Description

    The facultative footer template file or bytes.

  • Name
    template_data
    Type
    Template
    Description

    Template object containing the template properties (See Template). The following properties are mandatory: name, format.

Request

POST
/v1/template
curl -X POST https://data.fastpdfservice.com/v1/template \
  -H "Authorization: {token}" \
  -F "file_data=@/path/to/file" \
  -F "header_data=@/path/to/file" \
  -F "footer_data=@/path/to/file" \
  -F "template_data={template_data}"

Response

{
  "id": "a056V7R7NmNRjl70",
  "name": "Invoice Template",
  "format": "html",
  "timestamp": "2023-07-10T08:30:00Z",
  "description": "A standard invoice template",
  "paper_format": "A4",
  "landscape": false,
  "margin_top": 2,
  "margin_bottom": 2,
  "margin_left": 1,
  "margin_right": 1,
  "scale": 1.0,
  "print_background": true,
  "page_number_footer_enabled": true,
  "date_header_enabled": true,
  "title_header_enabled": true,
  "template_file": {
    "id": "6148eb22-35a3-4f17-ae8c-e3827221be60"
  },
  "timestamp": "Wed, 12 Jul 2023 11:11:32 GMT"
}

DELETE/v1/template/{template_id}

Delete template

This endpoint allows you to delete a specific template by its id.

Path parameters

  • Name
    template_id
    Type
    string
    Description

    id of the template to delete.

Request

DELETE
/v1/template/{template_id}
curl -X DELETE https://data.fastpdfservice.com/v1/template/{template_id} \
  -H "Authorization: {token}"

POST/v1/template/css/{template_id}

Add stylesheet

This endpoint allows you to add a new CSS stylesheet to a specific template. Once a stylesheet as been added to a Template, it will always be included during rendering.

Path parameters

  • Name
    template_id
    Type
    string
    Description

    The ID of the template to add the stylesheet to.

Request parameters

  • Name
    file_data
    Type
    File
    Description

    The path to the CSS file to upload, or the CSS file in bytes.

  • Name
    template_data
    Type
    StyleFile
    Description

    StyleFile object containing the stylesheet properties (See StyleFile). The following properties are mandatory: format.

Request

POST
/v1/template/css/{template_id}
curl -X POST https://data.fastpdfservice.com/v1/template/css/{template_id} \
  -H "Authorization: {token}" \
  -F "file_data=@stylesheet.css" \
  -F "template_data={template_data}"

Response

{
  "id": "6b810e5f-a0b9-4f9e-a4fc-a475b0e6a775",
  "template_id": "{template_id}",
  "timestamp": "{timestamp}",
  "format": "css"
}

DELETE/v1/template/css/{stylesheet_id}

Delete stylesheet

This endpoint allows you to delete stylesheet by its id.

Path parameters

  • Name
    stylesheet_id
    Type
    string
    Description

    id of the stylesheet to delete.

Request

DELETE
/v1/template/css/{stylesheet_id}
curl -X DELETE https://data.fastpdfservice.com/v1/template/css/{stylesheet_id} \
  -H "Authorization: {token}"

POST/v1/template/img/{template_id}

Add image

This endpoint allows you to add a new image to a specific template.

Path parameters

  • Name
    template_id
    Type
    string
    Description

    The ID of the template to add the image to.

Request parameters

  • Name
    file_data
    Type
    File
    Description

    The path to the image file to upload, or the image bytes.

  • Name
    template_data
    Type
    ImageFile
    Description

    ImageFile object containing information about the image (See ImageFile). The following properties are mandatory: format.

Request

POST
/v1/template/img/{template_id}
curl -X POST https://data.fastpdfservice.com/v1/template/img/{template_id} \
  -H "Authorization: {token}" \
  -F "file_data=@image.png" \
  -F "template_data={template_data}"

Response

{
  "id": "16fb3ad6-e7b0-4d3d-a021-5e26a90a94e1",
  "template_id": "{template_id}",
  "format": "png",
  "timestamp": "{timestamp}"
}

DELETE/v1/template/img/{image_id}

Delete image

This endpoint allows you to delete a specific image by its id.

Path parameters

  • Name
    image_id
    Type
    string
    Description

    id of the image to delete.

Request

DELETE
/v1/template/img/{image_id}
curl -X DELETE https://data.fastpdfservice.com/v1/template/img/{image_id} \
  -H "Authorization: {token}"

GET/v1/template/file/{id}

Get template file

This endpoint allows you to download a specific template file by its id.

Path parameters

  • Name
    id
    Type
    string
    Description

    id of the template file to retrieve.

Request

GET
/v1/template/file/{id}
curl -G https://data.fastpdfservice.com/v1/template/file/{id} \
  -H "Authorization: {token}"

GET/v1/template/css/file/{id}

Get stylesheet file

This endpoint allows you to download a specific stylesheet file by its id.

Path parameters

  • Name
    id
    Type
    string
    Description

    id of the stylesheet file to retrieve.

Request

GET
/v1/template/css/file/{id}
curl -G https://data.fastpdfservice.com/v1/css/template/file/{id} \
  -H "Authorization: {token}"

GET/v1/template/img/file/{id}

Get image file

This endpoint allows you to download a specific image file by its id.

Path parameters

  • Name
    id
    Type
    string
    Description

    id of the image file to retrieve.

Request

GET
/v1/template/img/file/{id}
curl -G https://data.fastpdfservice.com/v1/template/img/file/{id} \
  -H "Authorization: {token}"