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

Today, I needed to expose these lines of code as an HTTP web service capable of handling GET requests from the ScroogeXHTML home page.

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.)

First steps and online resources

After a bit of research, it turned out to be pretty easy. Here’s what I did to get the RTF to HTML conversion working.

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

    ....
    <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>
    ...

Logging API Dependency

For logging, ScroogeXHTML uses the SLF4J API. For very simple projects, an implementation dependency may be omitted. You may add a simple implementation with these lines:

    <!-- SLF4J for logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>2.0.16</version>
    </dependency>

Cloud Function Source Code

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 server for the function can be launched by issuing mvn function:run in the project directory.

[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/

When the server is ready, the conversion can be tested by navigating to the 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 console 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.

Full Source Code on GitHub

Visit https://github.com/michaelJustin/cloud-function-rtf-to-html for the source code of the follow-up project, which allows uploading RTF files. Currently the file size is limited to 64 KB, but this limit may be increased later.

Features

  • Complete Source Code and Maven project.
  • Deployment script included.
  • Demo HTML page included.
  • Serverless document conversion.

Related

More posts on RTF to HTML conversion with ScroogeXHTML