Class PrintTemplateUtil
java.lang.Object
com.ssgllc.fish.service.util.published.PrintTemplateUtil
Generates documents from print templates in a Groovy script.
Called as the printTemplateUtil bean. Note that generation failures are folded into an
error document rather than thrown, so a non-null result does not by itself mean the document is
the one you asked for.
-
Constructor Summary
ConstructorsConstructorDescriptionFor scripts that make their own instance; the QR code methods need the Spring bean.PrintTemplateUtil(com.ssgllc.fish.service.QrCodeService qrCodeService) -
Method Summary
Modifier and TypeMethodDescriptionstatic com.ssgllc.fish.document.GeneratedDocgenerateDocument(String printTemplateName, String entityId, Map<String, Object> formData, String taskInstanceId, String type, Instant asOfDate) Generates a document based on the specified print template, entity data, and other parameters.static com.ssgllc.fish.document.GeneratedDocgenerateDocumentWithDataSource(String printTemplateName, String entityId, Map<String, Object> formData, String taskInstanceId, Instant asOfDate, List<?> dataSource) Generate a document from a Jasper template while passing it a specific "data source".static StringqrCodeDataUri(String text, int scale) Makes a QR code as adata:URI, for an<img src>in an HTML template or a page.
Example:
return "<img alt='Verify' src='" + printTemplateUtil.qrCodeDataUri(verifyUrl, 4) + "'>"
static StringqrCodePngBase64(String text, int scale) Makes a QR code as a Base64 PNG, for example a certificate's link to its public verification page.
-
Constructor Details
-
PrintTemplateUtil
public PrintTemplateUtil()For scripts that make their own instance; the QR code methods need the Spring bean. -
PrintTemplateUtil
@Autowired public PrintTemplateUtil(com.ssgllc.fish.service.QrCodeService qrCodeService)
-
-
Method Details
-
generateDocument
public static com.ssgllc.fish.document.GeneratedDoc generateDocument(String printTemplateName, String entityId, Map<String, Object> formData, String taskInstanceId, String type, Instant asOfDate) throws ExceptionGenerates a document based on the specified print template, entity data, and other parameters. Note that the entity and field permissions are not checked for the current user and the document is generated with super user permissions
Example:
def formData = [field1: "value1", field2: "value2"]
def doc = printTemplateUtil.generateDocument("PersonTemplate", "c0a83801-655d-13e2-8191-9067f826208c", [:], null, "docx", dateUtil.startOfToday())
return bpmUtil.createProcessDocFromGeneratedDoc(doc, execution.processInstanceId)
Returns:
A generated DOCX document based on the "PersonTemplate" for the entity with the specified id.- Parameters:
printTemplateName- The name of the print template to be used.entityId- The ID of the entity for which the document is generated.formData- A map of form data used as input for the print template.taskInstanceId- The ID of the task instance associated with the document generation.type- The type of document to generate ("pdf", "docx", or "report").asOfDate- The date for fetching entity data as of a particular moment in time, if applicable.- Returns:
- A
GeneratedDocobject. On failure this is an error document rather thannull, so testing the result for null does not tell you whether generation succeeded - inspect the returned document if that distinction matters. - Throws:
NoSuchMethodException- If a required method for document generation is not found.IOException- If an I/O error occurs during document generation.Exception
-
generateDocumentWithDataSource
public static com.ssgllc.fish.document.GeneratedDoc generateDocumentWithDataSource(String printTemplateName, String entityId, Map<String, Object> formData, String taskInstanceId, Instant asOfDate, List<?> dataSource) throws ExceptionGenerate a document from a Jasper template while passing it a specific "data source". Since only Jasper templates supportdataSource,typeis assumed to be "report". For general documentation, seegenerateDocument(java.lang.String, java.lang.String, java.util.Map<java.lang.String, java.lang.Object>, java.lang.String, java.lang.String, java.time.Instant).
dataSourcecan be in one of 3 formats:-
A list of maps, as in
[[firstName: "John", lastName: "Doe"], [firstName: "Jane", lastName: "Doe"]]. Each map is a "row" of data and their keys are "columns". Because of this, all maps must have the same keys. - A list of entities. Each entity must be the same type. Remember to define fields in the Jasper template corresponding to the entities' fields.
- A list of arrays. The list items must be arrays, not lists. The first array consists of strings that name the "columns" used by all the other arrays. The other arrays are the "rows", and can contain any type. All arrays must be the same length.
- Parameters:
printTemplateName- The name of the print template to be used.entityId- The ID of the entity for which the document is generated.formData- A map of form data used as input for the print template.taskInstanceId- The ID of the task instance associated with the document generation.asOfDate- The date for fetching entity data as of a particular moment in time, if applicable.dataSource- The primary data source given to Jasper when generating the document.- Returns:
- Throws:
Exception
-
A list of maps, as in
-
qrCodePngBase64
Makes a QR code as a Base64 PNG, for example a certificate's link to its public verification page. A Jasper image expression can decode it; an HTML template can useqrCodeDataUri(java.lang.String, int).
Example:
def png = printTemplateUtil.qrCodePngBase64("https://iis.example.gov/verify?code=AB12CD34", 4)
- Parameters:
text- The text to encode, usually a URL, at most 1000 bytes in UTF-8.scale- Pixels per QR module, 1 to 10. 4 gives a code about 2 cm wide at 300 dpi for a short URL.- Returns:
- The PNG image, Base64 encoded.
- Throws:
IllegalArgumentException- When the text is empty or too long, or the scale is out of range.
-
qrCodeDataUri
Makes a QR code as adata:URI, for an<img src>in an HTML template or a page.
Example:
return "<img alt='Verify' src='" + printTemplateUtil.qrCodeDataUri(verifyUrl, 4) + "'>"
- See Also:
-