HTML as a template
In addition to converting existing HTML documents with the "Converter" web service and converting URLs with the "URLConverter" web service, webPDF can also use HTML documents that define a template used as a basis for new PDF documents.
webPDF can use the FreeMarker template engine to convert an HTML document to a template that can then be used as a basis for new PDF documents. These documents can then be dynamically filled with content (variables) in order to generate a new PDF document. Within this context, both the template and the variables are passed dynamically when calling the web service / using the web service parameters.
You can use all HTML and CSS syntax (in addition to the template engine) when designing HTML documents. The HTML document (HTML template) you create will then be converted to a PDF document with the "Converter" web service and the Chromium bridge.
For more information on the FreeMarker template engine, please visit https://freemarker.apache.org/docs/index.html
When calling the "Converter" web service, you would pass the defined HTML template as the original file (the document being converted) with the parameters for the "Converter" web service and set the useAsTemplate attribute to true in the html section.
API {REST}: /converter
{
"converter": {
"html": {
"adjustFonts": false,
"baseURL": "",
"downloadImages": false,
"templateData": {
"source": "value",
"uri": "",
"value": ""
},
"useAsTemplate": false,
"useBackground": true
}
}
This setting enables the template engine and has the web service interpret the HTML document as a template. Before the actual conversion, the corresponding variables will be replaced, and the HTML document will then be handled as a FreeMarker template during conversion.
Variables need to be passed in <templateData>, in the parameters, as a "Base64-encoded" JSON object.
{
"page1": "Text for page 1",
"page2": "Text for page 2",
"page3": "Text for page 3",
"image": {
"name": "Warning!",
"data": "data:image/png;base64,iVBORw0KGgoAAAAN...SUhEUgAABJwAAAMRCAIAAAAmxTsD"
}
}
The JSON object will then be converted to FreeMarker variables during the conversion. These variables will have ${custom. ...} as a prefix, and can be used this way in the HTML document (highlighted in red).
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8"/>
<style type="text/css" media="print">
div.page {
page-break-after: always;
page-break-inside: avoid;
}
</style>
</head>
<body>
<div class="page">
<h1>This is Page 1</h1>
<i>${custom.page1?upper_case}</i>
<br>
${custom.image.name}<img src="${custom.image.data}" width="100" height="100"/>
</div>
<div class="page">
<h1>This is Page 2</h1>
<b>${custom.page2}</b>
</div>
<div class="page">
<h1>This is Page 3</h1>
<u>${custom.page3}</u>
</div>
</body>
</html>
You can use this method to define complex HTML documents with macros and variables and create new PDF documents that are generated dynamically during conversion.