Toggle menu

pdfRequest(name[, header, footer, html, template, data])

Description

This method that allows you to generate a PDF from HTML or Handlebars and store it in the filestore.

The full range of Swag Handlebars Helpers are available, which can be used when generating templates for the PDF content.

The full CSS 2.1 specification is supported, as well as elements from CSS 3, including named pages, margin boxes and running elements.

Parameters

NameTypeDescription
nameString, requiredThe name for the PDF. It should include the .pdf file extension
headerString, optionalThe header that will be inserted into the PDF
footerString, optionalThe footer that will be inserted into the PDF
htmlString, optionalThe static PDF content. Either html or template must be supplied
templateString, optionalThe Handlebars template. If a template is supplied you must provide a data object. Either html or template must be supplied
dataObject, optionalThe data to pass to the template. Required if template is used

Example

This simple (and rather poorly styled) example generates a PDF using a Handlebars template. A single parameter "name" is passed to it, which is uppercased using the uppercase Handlebars helper. A page break is inserted using style='page-break-after: always' in the first paragraph of the template.

Note the use of back ticks do define a multi-line string. This is a feature of ECMAScript 6, available since iCM 10.0.6.0.

function( params ) {
    let request = this.callWorkerMethod( 'formutils', 'pdfRequest', {
        "name":"mypdf.pdf",
        "header":`<style type='text/css'>
                   div.header {
                      position: running(header)
                    }
                    @page {
                       @top-left {
                            content: element(header);
                           border: solid green;
                       }
                       @bottom-right-corner {
                           content: counter(page);
                       }
                    }
                </style>`,
        "footer":"<div class='footer'>GOSS</div>",
        "template":"<p style='page-break-after: always'>Hello {{uppercase name}}</p><p>Here's a second page of content</p>",
        "data":{
            "name":params.name
        }
    });
    return request;
}

Response

{
    "jsonrpc": "2.0",
    "id": 133,
    "result": {
        "result": {
            "result": {
                "lastUpdated": "2017-03-03T15:31Z",
                "lastUpdatedBy": null,
                "filename": "mypdf.pdf",
                "size": 1614,
                "references": [],
                "createdBy": null,
                "created": "2017-03-03T15:31Z",
                "id": "60efbc41-a72e-4185-8926-679ed8abd93b",
                "type": "application/pdf",
                "hash": "7f022b21c780fe918b55decc79a00eaf"
            }
        },
        "id": "_267",
        "jsonrpc": "2.0"
    }
}

The response is that returned from the filestore worker when the request is made to it by formutils. The "id" can then be used to retrieve the file.

The example above generated this PDF:  (PDF) [1KB]

Further Reading

Paged media: https://www.w3.org/TR/css3-page

Running Elements: https://www.w3.org/TR/css-gcpm-3/

Margin Boxes: https://www.w3.org/TR/css3-page/#margin-boxes

Last modified on 1 August 2023

Share this page

Facebook icon Twitter icon email icon

Print

print icon