Today, I needed to expose these three lines of code as an HTTP web service capable of handling GET requests from the HTML page https://www.scroogexhtml.com/index.html.

ScroogeXHTML s = new ScroogeXHTML();
s.setAddOuterHTML(true);
String html = s.convert("{\\rtf1 \"Hello world!\"}");

The HTML page contains an iframe, which can display the conversion result by simply setting its src attribute to the URL of the web service:

    <iframe src="https://example.com/myconversionservice" 
             style="width:100%;height:100%;border:0" />

Deploying a full-fledged web application on Apache Tomcat or some Jakarta EE container would require provisioning and maintaining a dedicated internet-facing server, resulting in unnecessary overhead in terms of resources, time, and cost.

The web page doesn’t get much traffic, so I went with a serverless Google Cloud Run function. With Cloud Run, you don’t have to worry about servers—Google takes care of everything, and you only pay for what you use when the function runs.

(Note: Other options like Amazon Web Services or Microsoft Azure might be a better fit if you already have an account with them.)

Skip the Headaches: Deploy RTF-to-HTML Conversion Java Code to the Web with Minimal Effort

After a bit of research, it turned out to be pretty easy. Here’s what I did with Google Cloud to get it working.

In the Maven pom.xml, one additional dependency was required to include the ScroogeXHTML RTF Converter:

    ....
    <dependencies>
        <!-- Required for Function primitives -->
        <dependency>
            <groupId>com.google.cloud.functions</groupId>
            <artifactId>functions-framework-api</artifactId>
            <version>1.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- Required for ScroogeXHTML RTF Converter -->
        <dependency>
            <groupId>com.scroogexhtml</groupId>
            <artifactId>ScroogeXHTML</artifactId>
            <version>11.0.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    ...

The full source code of the Cloud Run function Java class is shown below:

package func;

import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import java.io.BufferedWriter;
import java.io.IOException;
import com.scroogexhtml.ScroogeXHTML;

public class HelloWorld implements HttpFunction {
    // Simple function to return "Hello World"
    @Override
    public void service(HttpRequest request, HttpResponse response) throws IOException {
        ScroogeXHTML s = new ScroogeXHTML();
        s.setAddOuterHTML(true);
        String html = s.convert("{\\rtf1 \"Hello world!\"}");
        BufferedWriter writer = response.getWriter();
        writer.write(html);
    }
}

Local Test

A local test can be done by issuing
> mvn function:run
in the project directory. When the server is ready, the conversion can be tested by navigating to the URL http://localhost:8080/

[INFO] --- function:0.11.0:run (default-cli) @ ScroogeXHTML-Cloud ---
...
[INFO] Started ServerConnector@2a04ab05{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
[INFO] Started @4254ms
Juli 06, 2025 10:53:24 AM com.google.cloud.functions.invoker.runner.Invoker logServerInfo
INFORMATION: Serving function...
Juli 06, 2025 10:53:24 AM com.google.cloud.functions.invoker.runner.Invoker logServerInfo
INFORMATION: Function: func.HelloWorld
Juli 06, 2025 10:53:24 AM com.google.cloud.functions.invoker.runner.Invoker logServerInfo
INFORMATION: URL: http://localhost:8080/

Deploy to the Cloud

Deployment is done on the console using the gcloud functions deploy command:

gcloud functions deploy java-http-function 
  --gen2
  --entry-point=func.HelloWorld 
  --runtime=java21 
  --region=europe-west10 
  --source=./target 
  --trigger-http 
  --allow-unauthenticated

When the deployment succeeds, the log shows the web service endpoint URL which has been created to accept the HTTP requests. This is the endpoint URL which must be used as the src attribute of the iframe tag.

The endpoint URL is also shown in the Cloud Run console on the “Networking” tab:

That’s all for today.


Discover more from Habarisoft Blog

Subscribe to get the latest posts sent to your email.