Community Blog Launching a Word-to-PDF Cloud Service on Function Compute

Launching a Word-to-PDF Cloud Service on Function Compute

This article explains how to quickly deploy a Word-to-PDF cloud service with LibreOffice and Function Compute, based on the fc-libreoffice project.

By Du Wan

Charging by the number of calls and dynamic scaling features makes Function Compute an ideal solution for deploying online conversion tools, such as image clipping, webpage-to-image, and text-to-audio tools. These conversion tools are usually implemented with CPP and eventually compiled into binary command tools or DLLs. Because Function Compute is remotely called over HTTP in multi-language runtime environments, it is also suitable for the heterogeneous mode. This document explains how to quickly deploy a Word-to-PDF cloud service based on the fc-libreoffice project.

fc-libreoffice is a NPM with nodejs-based interface encapsulation. It incorporates the built-in and pre-compiled LibreOffice for running Function Compute. LibreOffice is tailored and compressed by using the Brotli algorithm and with a high compression ratio. However, the compression package is 84 MB, exceeding the upper limit (50 MB) of Function Compute. Therefore, in the example, we use Object Storage Service (OSS) to upload the LibreOffice file and download and compress it before running. The following primarily explains how to quickly deploy the LibreOffice cloud service by calling make commands encapsulated in scripts.

Dependent Tools

For MacOS systems, use the following installation method:

brew tap vangie/formula
brew install fun

For other platforms, install by using NPM.

npm install @alicloud/fun -g

The command line tool for OSS is ossutil. Download and store the tool in the directory that $PATH points to.

Clone a Project

Under the fc-libreoffice project, a sample project is provided to show how to quickly deploy a project through simple configuration.

# Clone a project
git clone https://github.com/awesome-fc/fc-libreoffice.git

# Enter the example project directory
cd fc-libreoffice/example

Modify the Configuration File

# Configure the .env file
cp env.example .env

Configure the environment variables in the .env file

# Alibaba Cloud access key. We recommend that you use the primary access key because role changing permissions are required for Fun deployment.

# Alibaba Cloud account ID. You can retrieve the account ID from Alibaba Cloud Account Center.

# Deployed region

# This indicates the OSS bucket used to upload the compressed LibreOffice package. Log on to the OSS console to create one first.

Deploy and Verify Functions

Deploy a function. In this step, install dependencies, upload the LibreOffice file to OSS, and deploy the function to Function Compute.

$ make deploy
.... (The output is omitted here.)

Waiting for service libre-svc to be deployed...
        Waiting for function libre-fun to be deployed...
                Waiting for packaging function libre-fun code...
                package function libre-fun code done
        function libre-fun deploy success
service libre-svc deploy success

Call the function. Convert the prepared example.docx file to the example.pdf file and upload it to OSS.

$ make invoke
+ fcli function invoke -s libre-svc -f libre-fun
{"name":"example.pdf","url":"http://vangie-test.oss-cn-shanghai-internal.aliyuncs.com/example.pdf","res":{"status":200,"statusCode":200,"headers":{"server":"AliyunOSS","date":"Sat, 01 Dec 2018 03:42:25 GMT","content-length":"0","connection":"keep-alive","x-oss-request-id":"5C02032129DDF1C134B9BC7C","x-oss-event-status":"eyJSZXN1bHQiOiAiT2sifQ==","etag":"\"94829EEB576E66A2B5760DD034EECB0B\"","x-oss-hash-crc64ecma":"5833133482735102142","content-md5":"lIKe61duZqK1dg3QNO7LCw==","x-oss-server-time":"60"},"size":0,"aborted":false,"rt":83,"keepAliveSocket":true,"data":{"type":"Buffer","data":[]},"requestUrls":["http://vangie-test.oss-cn-shanghai-internal.aliyuncs.com/example.pdf"],"timing":null,"remoteAddress":"","remotePort":80,"socketHandledRequests":3,"socketHandledResponses":3}}

Download the example.pdf file from OSS to the current directory. View the conversion result.

$ make download
Succeed: Total num: 1, size: 33,811. OK num: 1(download 1 objects).
0.192966(s) elapsed

The conversion result is as follows. The left figure shows the DOCX source file and the right figure shows the generated PDF file. Both Chinese characters and embedded images were correctly converted.



fc-libreoffice/example is only a sample project. For applications in actual production environments, there is still a lot of room for optimization, including integrity verification of downloaded files, streaming decompression, support for source file import and target file export over HTTP, and warm-up with a time trigger to reduce the cold startup costs. For the make commands in the document, we called some automation steps that are written with the bash script. These steps involve the commonly used methods and techniques for Function Compute. If you are interested in the LibreOffice migration process, check out the article How to Migrate LibreOffice to Function Compute or the source code of fc-libreoffice.

2 1 1
Share on

Alibaba Cloud Serverless

97 posts | 7 followers

You may also like


Raja_KT March 20, 2019 at 5:23 pm

Nice sharing . There is also...."https://www.alibabacloud.com/blog/building-a-serverless-pdf-text-recognition-using-function-compute-with-node-js-in-10-minutes_594429?spm=a2c5q.11423531.0.0.613933d94AWWLy""Building a Serverless PDF Text Recognition Using Function Compute with Node.js in 10 Minutes"

5649993212568625 August 28, 2023 at 9:21 am


Alibaba Cloud Serverless

97 posts | 7 followers

Related Products