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

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_ACCESS_KEY_ID=
ALIBABA_CLOUD_ACCESS_KEY_SECRET=
# Alibaba Cloud account ID. You can retrieve the account ID from Alibaba Cloud Account Center.
ALIBABA_CLOUD_ACCOUNT_ID=
# Deployed region
ALIBABA_CLOUD_DEFAULT_REGION=
# This indicates the OSS bucket used to upload the compressed LibreOffice package. Log on to the OSS console to create one first.
OSS_BUCKET=

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
bin/invoke.sh
+ 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":"100.118.102.36","remotePort":80,"socketHandledRequests":3,"socketHandledResponses":3}}

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

$ make download
bin/download.sh
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.

Image for post

Conclusion

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.

Reference:https://www.alibabacloud.com/blog/launching-a-word-to-pdf-cloud-service-on-function-compute_594593?spm=a2c41.12692415.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store