Environment Configuration – Java
Having different configurations based on the application’s environment can be highly beneficial. For example, environment configuration allows you to customize script policies, override document styles with a user-defined stylesheet, or handle web requests. To facilitate this, Aspose.HTML offers the Configuration class specifically designed to fulfill these requirements. By utilizing the Configuration object, you can effortlessly tailor the behavior of your Aspose.HTML application according to your specific needs.
Sandboxing
A sandboxing flag set is a set of zero or more flags that limit the capabilities of potentially untrusted resources. A sandbox provides a secure and controlled execution environment by isolating potentially malicious or untrusted code from the underlying system and sensitive data.
The following Java example demonstrates how to convert an HTML document to PDF format and apply sandboxing restrictions – how to mark ‘Scripts’ as an untrusted resource. As a result, “Scripts” will be disabled during the application execution.
1// Prepare HTML code and save it to a file
2String code = "<span>Hello World!!</span>\n" +
3 "<script>document.write('Have a nice day!');</script>\n";
4
5try (java.io.FileWriter fileWriter = new java.io.FileWriter(Resources.output("sandboxing.html"))) {
6 fileWriter.write(code);
7}
8
9// Create an instance of the Configuration class
10com.aspose.html.Configuration configuration = new com.aspose.html.Configuration();
11try {
12 // Mark 'scripts' as an untrusted resource
13 configuration.setSecurity(com.aspose.html.Sandbox.Scripts);
14
15 // Initialize an HTML document with specified configuration
16 com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(Resources.output("sandboxing.html"), configuration);
17 try {
18 // Convert HTML to PDF
19 com.aspose.html.converters.Converter.convertHTML(
20 document,
21 new com.aspose.html.saving.PdfSaveOptions(),
22 Resources.output("sandboxing_out.pdf")
23 );
24 } finally {
25 if (document != null) {
26 document.dispose();
27 }
28 }
29} finally {
30 if (configuration != null) {
31 configuration.dispose();
32 }
33}
In the example, a new instance of Configuration class is created to configure the sandbox. The setSecurity()
method is called on the configuration
object, passing com.aspose.html.Sandbox.Scripts
as an argument. This flag enables sandboxing and restricts the execution of scripts within the HTML document.
Services
All important functional is grouped into separated services for usability purpose and located into the Package com.aspose.html.services.
User Agent Service
In the context of environment configuration, the User Agent Service allows you to specify a custom user stylesheet, a primary character set for the document, language and fonts settings.
User Style Sheet
The user is able to specify a custom style information for a particular document. This information applies to the document according to the
cascading rules and may affect the presentation of the document. The following Java code snippet showcases the usage of the User Agent Service (IUserAgentService
) for applying a custom user stylesheet to an HTML document that is then converted to PDF.
Let’s just look at the necessary steps to specify a custom style sheet in an HTML document:
- In the example, we use the
Configuration()
constructor to create an instance of the Configuration class. - Then we call the
getService() method on the Configuration class and pass the
IUserAgentService.class
to it as a parameter. So, we get an instance of the User Agent Service. - To set a custom user stylesheet to be applied to the HTML document, we use the
setUserStyleSheet()
method of the User Agent Service, providing a CSS code as an argument. In this case, the CSS rulespan { color: green; }
is used to make all<span>
elements display text in green color.
1// Prepare HTML code and save it to a file
2String code = "<span>Hello World!!!</span>";
3
4try (java.io.FileWriter fileWriter = new java.io.FileWriter("user-agent-stylesheet.html")) {
5 fileWriter.write(code);
6}
7
8// Create an instance of the Configuration class
9com.aspose.html.Configuration configuration = new com.aspose.html.Configuration();
10try {
11 // Get the IUserAgentService
12 com.aspose.html.services.IUserAgentService userAgent = configuration.getService(com.aspose.html.services.IUserAgentService.class);
13
14 // Set the custom color to the <span> element
15 userAgent.setUserStyleSheet("span { color: green; }");
16
17 // Initialize an HTML document with specified configuration
18 com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(Resources.output("user-agent-stylesheet.html"), configuration);
19 try {
20 // Convert HTML to PDF
21 com.aspose.html.converters.Converter.convertHTML(
22 document,
23 new com.aspose.html.saving.PdfSaveOptions(),
24 Resources.output("user-agent-stylesheet_out.pdf")
25 );
26 } finally {
27 if (document != null) {
28 document.dispose();
29 }
30 }
31} finally {
32 if (configuration != null) {
33 configuration.dispose();
34 }
35}
Character Set
In order to parse and display an HTML document correctly, the application must know what character set (encoding) is used for the document. If the character encoding is not directly specified in the document’s header, Aspose.HTML uses UTF-8, which is defined as the default for HTML5 specification. However, if you are sure that your HTML document is written differently from UTF-8 encoding, you can specify it manually. Let’s consider the necessary steps to specify a character set (encoding) in the HTML document:
- Create an instance of Configuration class.
- Use the
getService()
method to get an instance of the User Agent Service. The method takes theIUserAgentService.class
as a parameter. - Call the
setCharSet()
method of the User Agent Service and provide the desired character set (encoding). In this example,ISO-8859-1
is set as the character set.
1// Prepare HTML code and save it to a file
2String code = "<h1>Character Set</h1>\r\n" +
3 "<p>The <b>CharSet</b> property sets the primary character-set for a document.</p>\r\n";
4
5try (java.io.FileWriter fileWriter = new java.io.FileWriter(Resources.output("user-agent-charset.html"))) {
6 fileWriter.write(code);
7}
8
9// Create an instance of the Configuration class
10com.aspose.html.Configuration configuration = new com.aspose.html.Configuration();
11try {
12 // Get the IUserAgentService
13 com.aspose.html.services.IUserAgentService userAgent = configuration.getService(com.aspose.html.services.IUserAgentService.class);
14
15 // Set ISO-8859-1 encoding to parse the document
16 userAgent.setCharSet("ISO-8859-1");
17
18 // Initialize an HTML document with specified configuration
19 com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(Resources.output("user-agent-charset.html"), configuration);
20 try {
21 // Convert HTML to PDF
22 com.aspose.html.converters.Converter.convertHTML(
23 document,
24 new com.aspose.html.saving.PdfSaveOptions(),
25 Resources.output("user-agent-charset_out.pdf")
26 );
27 } finally {
28 if (document != null) {
29 document.dispose();
30 }
31 }
32} finally {
33 if (configuration != null) {
34 configuration.dispose();
35 }
36}
By setting the character set using the setCharSet()
method, the application informs the Aspose.HTML for .NET parsing and rendering engine about the specific encoding used in the HTML document. This is very important because different character sets can represent characters differently, and without the correct encoding information, the document may not be accurately parsed or displayed.
Set Path to the Font Folder
One of the key features of Aspose.HTML is its ability to work with custom fonts, allowing developers to add their own fonts to the rendering process. For a situation when you need to use the custom fonts instead of the fonts installed on OS, you can set the path to your custom folder, as it follows:
1// Prepare HTML code and save it to a file
2String code = "<h1>FontsSettings property</h1>\r\n" +
3 "<p>The FontsSettings property is used for configuration of fonts handling.</p>\r\n";
4
5try (java.io.FileWriter fileWriter = new java.io.FileWriter(Resources.output("user-agent-fontsetting.html"))) {
6 fileWriter.write(code);
7}
8
9// Initialize an instance of the Configuration class
10com.aspose.html.Configuration configuration = new com.aspose.html.Configuration();
11try {
12 // Get the IUserAgentService
13 com.aspose.html.services.IUserAgentService userAgent = configuration.getService(com.aspose.html.services.IUserAgentService.class);
14
15 // Set custom font folder path
16 userAgent.getFontsSettings().setFontsLookupFolder("fonts");
17
18 // Initialize an HTML document with specified configuration
19 com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(Resources.output("user-agent-fontsetting.html"), configuration);
20 try {
21 // Convert HTML to PDF
22 com.aspose.html.converters.Converter.convertHTML(
23 document,
24 new com.aspose.html.saving.PdfSaveOptions(),
25 Resources.output("user-agent-fontsetting_out.pdf")
26 );
27 } finally {
28 if (document != null) {
29 document.dispose();
30 }
31 }
32} finally {
33 if (configuration != null) {
34 configuration.dispose();
35 }
36}
To set font folder using Aspose.HTML for Java library, we use the getFontsSettings()
method to access the FontsSettings object. The setFontsLookupFolder()
method is called on the FontsSettings object, specifying the folder path where the fonts are located.
Runtime Service
The Runtime Service gives you control over the lifetime of the internal processes. For instance, using IRuntimeService you can specify timeouts for JavaScripts. It is important to have such a timeout in case if a script contains an endless loop. The following code snippet demonstrates how to use the Runtime Service to limit JavaScript execution time and convert an HTML document to an image format:
- In the example, we create an HTML document from scratch. Prepared HTML code includes an infinite loop within a
<script>
element. We use the FileWriter() to write the HTML code to a file. - Create an instance of Configuration class.
- Call the getService() method to get an instance of the Runtime Service.
- Use the setJavaScriptTimeout() method of the Runtime Service to specify the maximum allowed JavaScript code execution time. The example is set to 5 seconds.
- Create an HTMLDocument object using the HTMLDocument(address, configuration) constructor. It takes the path to the previously created HTML file and the configuration object.
- Convert HTML to PNG using convertHTML(document, options, outputPath) method.
In the example, if the timeout of 5 seconds is exceeded during JavaScript execution, Aspose.HTML will interrupt JavaScript code execution and continue with the remaining HTML to PNG conversion process.
1// Prepare HTML code and save it to a file
2String code = "<h1>Runtime Service</h1>\r\n" +
3 "<script> while(true) {} </script>\r\n" +
4 "<p>The Runtime Service optimizes your system by helping it start apps and programs faster.</p>\r\n";
5
6try (java.io.FileWriter fileWriter = new java.io.FileWriter(Resources.output("runtime-service.html"))) {
7 fileWriter.write(code);
8}
9
10// Create an instance of the Configuration class
11com.aspose.html.Configuration configuration = new com.aspose.html.Configuration();
12try {
13 // Limit JS execution time to 5 seconds
14 com.aspose.html.services.IRuntimeService runtimeService = configuration.getService(com.aspose.html.services.IRuntimeService.class);
15 runtimeService.setJavaScriptTimeout(TimeSpan.fromSeconds(5));
16
17 // Initialize an HTML document with specified configuration
18 com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument(Resources.output("runtime-service.html"), configuration);
19 try {
20 // Convert HTML to PNG
21 com.aspose.html.converters.Converter.convertHTML(
22 document,
23 new ImageSaveOptions(),
24 Resources.output("runtime-service_out.png")
25 );
26 } finally {
27 if (document != null) {
28 document.dispose();
29 }
30 }
31} finally {
32 if (configuration != null) {
33 configuration.dispose();
34 }
35}
Network Service
INetworkService allows you to control all incoming/outcoming traffic and implement your custom message handlers. It can be used for different purposes, such as: create custom caching mechanism, trace/logging request messages, etc.
Message Handlers
Using the
MessageHandler class and overriding the invoke()
method, you can define custom logic that will be executed during network operations. The following example demonstrates how to use message handlers to log information about unreachable resources. In the example, the logic checks the response status code and handles the case where the file is not found:
1// Create a MessageHandler. This message handler logs all failed requests to the console
2MessageHandler handler = new MessageHandler() {
3 @Override
4 public void invoke(INetworkOperationContext context) {
5 if (context.getResponse().getStatusCode() != HttpStatusCode.OK)
6 {
7 System.out.println(String.format("File '%s' Not Found", context.getRequest().getRequestUri().toString()));
8 }
9
10 // Invoke the next message handler in the chain
11 next(context);
12 }
13};
First of all, you need to create a custom message handler and use it, as it follows:
1// Prepare HTML code with missing image file
2String code = "<img src='missing.jpg'>";
3
4try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
5 fileWriter.write(code);
6}
7
8// Create an instance of Configuration
9com.aspose.html.Configuration configuration = new com.aspose.html.Configuration();
10try {
11 // Add ErrorMessageHandler to the chain of existing message handlers
12 com.aspose.html.services.INetworkService network = configuration.getService(com.aspose.html.services.INetworkService.class);
13 network.getMessageHandlers().addItem(handler);
14
15 // Initialize an HTML document with specified configuration
16 // During the document loading, the application will try to load the image and we will see the result of this operation in the console
17 com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("document.html", configuration);
18 try {
19 // Convert HTML to PNG
20 com.aspose.html.converters.Converter.convertHTML(
21 document,
22 new com.aspose.html.saving.ImageSaveOptions(),
23 "output.png"
24 );
25 } finally {
26 if (document != null) {
27 document.dispose();
28 }
29 }
30} finally {
31 if (configuration != null) {
32 configuration.dispose();
33 }
34}
You can download the complete examples and data files from GitHub.