JSReport
We are actively working to improve this documentation. The content you see here may be incomplete, subject to change, or may not fully reflect the current state of the feature. We appreciate your understanding as we continue to enhance our docs.
Brightspot seamlessly integrates with JSReport, enhancing document generation capabilities. In response to limitations observed with traditional HTML → PDF tools, this collaboration presents a practical solution for creating dynamic documents. The integration allows users to generate customized PDFs, Word documents, Excel sheets, and PowerPoint presentations directly from Brightspot, utilizing JSReport's template-based approach for a robust document generation process.
What is JSReport?
JSReport is a server-based service that utilizes Handlebars templates to produce various document types. These templates, along with associated assets, are stored on the JSReport server, and documents are generated by making REST calls with the required data in JSON format.
Integration overview
Brightspot's integration with JSReport offers a straightforward yet powerful set of features:
- Document Generation API—APIs for generating a document for a provided object. Both a generic API and one leveraging Brightspot's View System are provided.
- Config API—An API for providing configuration details for connecting to a JSReport server. A default implementation is provided for which configuration details are retrieved from environment settings.
- Preview Implementation—An implementation of Brightspot's Preview System for JSReport, allowing editors a live preview of generated documents in the CMS.
Configuration
There are two alternatives for configuring the JSReport integration within Brightspot, providing flexibility to accommodate various project requirements.
- Out-of-the-Box Configuration—This approach simplifies the initial configuration process and leverages environment variables to seamlessly provide essential details for the JSReport integration. The required configuration parameters include the JSReport server's URL, username, password, and optional timeout configurations. This straightforward method ensures a quick and efficient setup for users seeking a hassle-free configuration experience.
- Custom Configuration API—Recognizing the diverse needs of projects, Brightspot provides an API that allows users to implement bespoke ways of supplying JSReport configuration details. This empowers developers to choose the most suitable configuration approach that best fits the project's unique needs.
Out-of-the-Box Configuration
For the out-of-the-box configuration, the following environment variables need to be supplied:
| Key | Type | Description |
|---|---|---|
brightspot/jsreport/default | java.lang.String | Name of the default configuration to use. Swap in this value for \{configName\} for the other keys. |
brightspot/jsreport/config/\{configName\}/class | java.lang.String | Java class name of the JsReportServiceProvider to use. For this out-of-the-box solution the value should be com.psddev.jsreport.DefaultJsReportServiceProvider. |
brightspot/jsreport/config/\{configName\}/endpoint | java.lang.String | URL used to connect to the JSReport server. |
brightspot/jsreport/config/\{configName\}/username | java.lang.String | Username used to authenticate with JSReport. |
brightspot/jsreport/config/\{configName\}/password | java.lang.String | Password used to authenticate with JSReport. |
brightspot/jsreport/config/\{configName\}/callTimeout | java.lang.String | API Call Timeout in seconds. |
brightspot/jsreport/config/\{configName\}/connectTimeout | java.lang.String | API Connect Timeout in seconds. |
brightspot/jsreport/config/\{configName\}/writeTimeout | java.lang.String | API Write Timeout in seconds. |
brightspot/jsreport/config/\{configName\}/readTimeout | java.lang.String | API Read Timeout in seconds. |
For additional details on environment settings, see Configuring Dari.
Custom configuration API
To implement the Custom Configuration API for JSReport integration in Brightspot, you extend the provided JsReportServiceProvider abstract class. This class allows you to define your own method of supplying a JSReport configuration, offering flexibility to adapt to project-specific needs. Below is an example of implementing a JsReportServiceProvider in which the configuration comes from fields in Brightspot's Sites & Settings admin area.
1import com.psddev.cms.db.ToolUi;2import com.psddev.cms.tool.CmsTool;3import com.psddev.dari.db.Modification;4import com.psddev.dari.db.Recordable;56@Recordable.FieldInternalNamePrefix("jsreport.")7①public class JsReportGlobalSettings extends Modification<CmsTool> {89private static final String TAB = "Integrations";10private static final String CLUSTER = "JS Report";1112@ToolUi.Tab(TAB)13@ToolUi.Cluster(CLUSTER)14private String endpoint;1516@ToolUi.Tab(TAB)17@ToolUi.Cluster(CLUSTER)18private String username;1920@ToolUi.Tab(TAB)21@ToolUi.Cluster(CLUSTER)22@ToolUi.Secret23private String password;2425public String getEndpoint() {26return endpoint;27}2829public void setEndpoint(String endpoint) {30this.endpoint = endpoint;31}3233public String getUsername() {34return username;35}3637public void setUsername(String username) {38this.username = username;39}4041public String getPassword() {42return password;43}4445public void setPassword(String password) {46this.password = password;47}48}4950import java.time.Duration;5152import com.psddev.cms.tool.CmsTool;53import com.psddev.dari.db.Singleton;54import com.psddev.jsreport.JsReportServiceProvider;55import net.jsreport.java.service.JsReportService;56import net.jsreport.java.service.JsReportServiceImpl;57import net.jsreport.java.service.ServiceTimeout;5859②public class JsReportGlobalSettingsServiceProvider extends JsReportServiceProvider {6061@Override62public JsReportService getJsReportService() {63③JsReportGlobalSettings settings = Singleton.getInstance(CmsTool.class).as(JsReportGlobalSettings.class);64if (settings != null && settings.getEndpoint() != null && settings.getUsername() != null && settings.getPassword() != null) {65ServiceTimeout serviceTimeout = new ServiceTimeout();66serviceTimeout.setCallTimeout(Duration.ofMinutes(1));67serviceTimeout.setConnectTimeout(Duration.ofMinutes(1));68serviceTimeout.setWriteTimeout(Duration.ofMinutes(1));69serviceTimeout.setReadTimeout(Duration.ofMinutes(1));70④return new JsReportServiceImpl(71settings.getEndpoint(),72settings.getUsername(),73settings.getPassword(),74serviceTimeout);75}76return null;77}78}
- ①
A Modification on
CmsToolallows us to add new fields to the Sites & Settings admin area. The class contains fields for configuring:- Endpoint URL
- Username
- Password
- ②An extension of JsReportServiceProvider allows us to create a custom solution for providing configuration for JSReport.
- ③The JsReportGlobalSettings object is retrieved using the BSP Singleton API.
- ④A JsReportServiceImpl object is created with the configuration from the JsReportGlobalSettings object.
This custom configuration supplier can then be enabled via the following environment variables:
| Key | Type | Description |
|---|---|---|
brightspot/jsreport/default | java.lang.String | Name of the default configuration to use. Swap in this value for \{configName\} for the other keys. |
brightspot/jsreport/config/\{configName\}/class | java.lang.String | Java class name of the JsReportServiceProvider to use. For this custom solution the value should be com.yourcompany.JsReportGlobalSettingsServiceProvider. |
JSReport document generation API
The JsReportDocumentGenerator utility class in Brightspot simplifies the process of generating documents using JSReport. This utility class provides two distinct methods, catering to different use cases, allowing developers to seamlessly integrate JSReport document generation within their Brightspot projects. This topic describes each method and its use cases.
Raw data method
createDocument(String template, Object data)
Creates a Document for the specified JSReport template and input data.
-
Parameters:
template: The JSReport template to use, specified by either the template name or short ID.data: The data used to render the JSReport template, typically represented as ajava.util.Mapto convey JSON data.
-
Return:
- A
Documentobject containing the document produced by JSReport, including the document's content and content type.
- A
Usage example
1String templateName = "myTemplate";2Map<String, Object> jsonData = // ... prepare JSON data3Document generatedDocument = JsReportDocumentGenerator.createDocument(templateName, jsonData);
View system method
createDocument(String template, Class dateEntryView, Object content)
Creates a Document using the specified template, dateEntryView, and content, leveraging the Brightspot view system.
-
Parameters:
template: The entry view used to find an appropriate view model. If null, theDefaultJsReportTemplateannotation will be checked for a default template.dateEntryView: The entry view used to find an appropriate view model.content: The content providing data for document generation.
-
Return:
- A
Documentobject containing the document produced by JSReport, including the document's content and content type.
- A
Usage example
1Object content = Query.from(MyRecord.class).first(); // contrived example for getting cotent from BSP DB2Document generatedDocument = JsReportDocumentGenerator.createDocument(null, JsReportViewModel.class, content);34①@JsonView5②@ViewInterface6③@DefaultJsReportTemplate("myPdf")7④public class JsReportViewModel extends ViewModel<MyRecord> {89⑤public String getTitle() {10return model.getHeadline();11}1213⑥public String getCompany() {14return model.getCompany();15}16}
- ①Specifies that the ViewModel can be converted to JSON, which is the primary format JSReport accepts.
- ②Specifies that the ViewModel is a ViewInterface, meaning any public getter methods are included in data.
- ③Specifies that the default JSReport template to use for this viewmodel has the name "myPdf". This will be used in the API call from line #2 since a null value was passed for the template parameter.
- ④Marks this class as a ViewModel for the "MyRecord" class.
- ⑤This will result in a "title" key in the resulting JSON passed to JSReport.
- ⑥This will result in a "company" key in the resulting JSON passed to JSReport.
Enabling JSReport preview
The JSReport preview implementation in Brightspot offers editors the invaluable capability to visualize how JSReport-generated documents will look as they create assets. This functionality enhances the content creation experience by providing a real-time preview of the documents, ensuring they meet the desired format and appearance.
Key Details:
- Real-Time Document Preview—Editors can preview JSReport-generated documents directly within the Brightspot interface, gaining immediate insights into the document's appearance during the content creation process. This feature leverages the standard Brightspot preview system, so editors can view the preview within the content edit form in a separate browser tab.
- PDF and Microsoft Office suite support—Preview for both PDF and Microsoft Office documents (Word, Excel, PowerPoint) is supported. For Microsoft Office, the documents must be uploaded to a Microsoft endpoint for preview to function.
- View System-based document generation—The preview system requires using the Brightspot view system for converting Brightspot data models into the JSON that is sent to JSReport, as described below.
- Simple Configuration—Some simple configuration is required in Sites & Settings to enable the preview.
View system
Each content type providing a JSReport preview must have an associated ViewModel class that implements the interfaces JsReportMicrosoftOfficePreviewEntryView and/or JsReportPdfPreviewEntryView. Implementing these interfaces marks the ViewModel as the entry point for generating the JSON that will be sent to JSReport to generate the document.
Preview configuration
To enable JSReport preview:
- Click > Admin > Sites & Settings.
-
In the Sites widget, select the site for which you want to configure JSReport preview, or select Global to configure JSReport preview for all sites.
-
Expand CMS > Preview.
-
Under Preview Types, add a JS Report Microsoft Office or JS Report PDF preview type configuration. Under this object, add one or more configurations with the following fields set:
- Data View—This is the
ViewModelthat would be used. - Template—The JSReport template to use in the preview. This has a default value if the
ViewModelselected under Data View has a@DefaultJsReportTemplateannotation.
- Data View—This is the