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.
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.
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
# 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 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.
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.
Connect Function Compute and CDN with the CDN Trigger and Fun
99 posts | 7 followers
FollowAlibaba Clouder - January 18, 2021
Alibaba Cloud Serverless - March 19, 2019
Alibaba Clouder - February 11, 2019
H Ohara - May 8, 2024
Alibaba Clouder - August 12, 2019
PM - C2C_Yuan - May 23, 2024
99 posts | 7 followers
FollowAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreProvides scalable, distributed, and high-performance block storage and object storage services in a software-defined manner.
Learn MoreAn encrypted and secure cloud storage service which stores, processes and accesses massive amounts of data from anywhere in the world
Learn MorePlan and optimize your storage budget with flexible storage services
Learn MoreMore Posts by Alibaba Cloud Serverless
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"