1. 程式人生 > >開發函式計算的正確姿勢——藉助LibreOffice將Word轉換成PDF

開發函式計算的正確姿勢——藉助LibreOffice將Word轉換成PDF

前言

首先介紹下在本文出現的幾個比較重要的概念:

函式計算(Function Compute):函式計算是一個事件驅動的服務,通過函式計算,使用者無需管理伺服器等執行情況,只需編寫程式碼並上傳。函式計算準備計算資源,並以彈性伸縮的方式執行使用者程式碼,而使用者只需根據實際程式碼執行所消耗的資源進行付費。函式計算更多資訊參考。

Funcraft:Funcraft 是一個用於支援 Serverless 應用部署的工具,能幫助您便捷地管理函式計算、API 閘道器、日誌服務等資源。它通過一個資源配置檔案(template.yml),協助您進行開發、構建、部署操作。Funcraft 的更多文件參考。

LibreOffice:LibreOffice 是由文件基金會開發的自由及開放原始碼的辦公室套件。LibreOffice套件包含文書處理器、電子表格、簡報程式、向量圖形編輯器和圖表工具、資料庫管理程式及建立和編輯數學公式的應用程式。參見維基百科詞條

備註: 本文介紹的技巧需要 Funcraft 版本大於等於 3.0.0-beta.7 。

依賴工具

本專案是在 MacOS 下開發的,涉及到的工具是平臺無關的,對於 Linux 和 Windows 桌面系統應該也同樣適用。在開始本例之前請確保如下工具已經正確的安裝,更新到最新版本,並進行正確的配置。

  • Docker
  • Funcraft

Fun 工具依賴於 docker 來模擬本地環境。

對於 MacOS 使用者可以使用 homebrew 進行安裝:

 
brew cask install docker 
brew tap vangie/formula 
brew install fun 

Windows 和 Linux 使用者安裝請參考:

  1. https://github.com/aliyun/fun/blob/master/docs/usage/installation.md

安裝好後,記得先執行 fun config 初始化一下配置。

注意, 如果你已經安裝過了 funcraft,確保 funcraft 的版本在 3.0.0-beta.7 以上。

 
$ fun --version 3.0.0-beta.7 

初始化

使用 fun init 命令可以快捷地將本模板專案初始化到本地。

 
$ fun init vangie/libreoffice-example 

安裝依賴

 
$ fun install Installing recursively on fun.yml skip pulling image aliyunfc/runtime-nodejs8:build-1.6.1... Task => [UNNAMED] => apt-get update (if need) => apt-get install -y -d -o=dir::cache=/code/.fun/tmp/install libnss3 --reinstall => bash -c for f in $(ls /code/.fun/tmp/install/archives/*.deb); do dpkg -x $f /code/.fun/root; mkdir -p /code/.fun/tmp/install/deb-control/${f%.*}; dpkg -e $f /code/.fun/tmp/install/deb-control/${f%.*}; if [ -f "/code/.fun/tmp/install/deb-control/${f%.*}/postinst" ]; then FUN_INSTALL_LOCAL=true /code/.fun/tmp/install/deb-control/${f%.*}/postinst configure; fi; done; => bash -c 'rm -rf /code/.fun/tmp/install/archives' Task => [UNNAMED] => bash -c 'npm install --production && mkdir -p .fun/nas/auto-default/libreoffice/ && mv ./node_modules/fc-libreoffice/bin/lo.tar.br .fun/nas/auto-default/libreoffice/' info looking for cached prebuild @ /root/.npm/_prebuilds/95f620-iltorb-v2.4.3-node-v57-linux-x64.tar.gz http request GET https://github.com/MayhemYDG/iltorb/releases/download/v2.4.3/iltorb-v2.4.3-node-v57-linux-x64.tar.gz http 200 https://github.com/MayhemYDG/iltorb/releases/download/v2.4.3/iltorb-v2.4.3-node-v57-linux-x64.tar.gz info downloading to @ /root/.npm/_prebuilds/95f620-iltorb-v2.4.3-node-v57-linux-x64.tar.gz.152-11c561798275d.tmp info renaming to @ /root/.npm/_prebuilds/95f620-iltorb-v2.4.3-node-v57-linux-x64.tar.gz info unpacking @ /root/.npm/_prebuilds/95f620-iltorb-v2.4.3-node-v57-linux-x64.tar.gz info unpack resolved to /code/node_modules/iltorb/build/bindings/iltorb.node info unpack required /code/node_modules/iltorb/build/bindings/iltorb.node successfully info install Successfully installed iltorb binary! npm WARN [email protected] No description npm WARN [email protected] No repository field. 

部署

 
$ fun deploy using template: template.yml using region: cn-shanghai using accountId: ***********4733 using accessKeyId: ***********EUz3 using timeout: 60 Waiting for service libreoffice to be deployed... make sure role 'aliyunfcgeneratedrole-cn-shanghai-libreoffice' is exist role 'aliyunfcgeneratedrole-cn-shanghai-libreoffice' is already exist attaching police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-shanghai-libreoffice attached police 'AliyunECSNetworkInterfaceManagementAccess' to role: aliyunfcgeneratedrole-cn-shanghai-libreoffice using 'VpcConfig: Auto', Fun will try to generate related vpc resources automatically vpc already generated, vpcId is: vpc-uf6jphcirs45lekbtw59k vswitch already generated, vswitchId is: vsw-uf6gmxg7cyl3dlohlck9p security group already generated, security group is: sg-uf6hkd156qntmzxr51h3 generated auto VpcConfig done: {"vpcId":"vpc-uf6jphcirs45lekbtw59k","vswitchIds":["vsw-uf6gmxg7cyl3dlohlck9p"],"securityGroupId":"sg-uf6hkd156qntmzxr51h3"} using 'NasConfig: Auto', Fun will try to generate related nas file system automatically nas file system already generated, fileSystemId is: 3f95748ba7 nas file system mount target is already created, mountTargetDomain is: 3f95748ba7-ips53.cn-shanghai.nas.aliyuncs.com generated auto NasConfig done: {"UserId":10003,"GroupId":10003,"MountPoints":[{"ServerAddr":"3f95748ba7-ips53.cn-shanghai.nas.aliyuncs.com:/libreoffice","MountDir":"/mnt/auto"}]} Checking if nas directories /libreoffice exists, if not, it will be created automatically Checking nas directories done ["/libreoffice"] Waiting for function word2pdf to be deployed... Waiting for packaging function word2pdf code... The function word2pdf has been packaged. A total of 1019 files files were compressed and the final size was 6.36 MB function word2pdf deploy success service libreoffice deploy success 

執行

 
$ fun invoke word2pdf using template: template.yml ========= FC invoke Logs begin ========= FC Invoke Start RequestId: 20e7c0e4-d674-4be0-854f-bd876dc00043 load code for handler:index.handler Error: source file could not be loaded 2019-09-17T13:57:35.716Z 20e7c0e4-d674-4be0-854f-bd876dc00043 [verbose] convert success. FC Invoke End RequestId: 20e7c0e4-d674-4be0-854f-bd876dc00043 Duration: 4741.22 ms, Billed Duration: 4800 ms, Memory Size: 640 MB, Max Memory Used: 578.54 MB ========= FC invoke Logs end ========= FC Invoke Result: pdf save to /tmp/example.pdf 

PDF 檔案會寫入到 /tmp 目錄,如果希望能看到結果,可以把檔案上傳到 OSS,然後從 OSS 下載下來。

參考閱讀

  1. 五分鐘上線——函式計算 Word 轉 PDF 雲服務
  2. https://github.com/awesome-fc/fc-libreoffice
  3. 模板專案

原文連結

本文為雲棲社群原創內容,未經