簡潔的寫作需要勇氣。讓事物變小是一種深思熟慮的、困難的和有價值的行為。大多數(shù)書籍本應(yīng)是一篇博客文章。大多數(shù)博客文章本應(yīng)是一條微博。大多數(shù)微博本應(yīng)不寫。
概述
aws lambda
AWS Lambda 是一項(xiàng)無服務(wù)器事件驅(qū)動型計算服務(wù),該服務(wù)使您可以運(yùn)行幾乎任何類型的應(yīng)用程序或后端服務(wù)的代碼,而無需預(yù)置或管理服務(wù)器??梢詮?200 多個 AWS 服務(wù)和軟件即服務(wù) (SaaS) 應(yīng)用程序中觸發(fā) Lambda,且只需按您的使用量付費(fèi)。
為什么選擇使用 aws lambda
文件格式轉(zhuǎn)換是一種非常消耗內(nèi)存和 CPU 的計算,并且存在很多不確定性。如果使用傳統(tǒng)的方式去實(shí)現(xiàn),比如 api + 多線程的方式運(yùn)行,如果在大批量轉(zhuǎn)換的時候,很多失敗的任務(wù)沒有進(jìn)行有效的資源釋放,這很容易引起系統(tǒng)的內(nèi)存泄漏或 CPU 過載。并且如果任務(wù)過多,服務(wù)的橫向擴(kuò)展會變得比較復(fù)雜(實(shí)現(xiàn)橫向擴(kuò)展遠(yuǎn)比轉(zhuǎn)換服務(wù)本身復(fù)雜)。
aws lambda 引入能有效的解決上述產(chǎn)生的問題,通過 lambda + sqs 的方式可以很方便的實(shí)現(xiàn)一個高可用的格式轉(zhuǎn)換服務(wù)。
- aws lambda 支持 docker 的方式調(diào)用,不管運(yùn)行運(yùn)行環(huán)境多復(fù)雜,也能有效的進(jìn)行環(huán)境打包。
- 每一個 lambda 函數(shù)的執(zhí)行是獨(dú)立的,失敗任務(wù)不會影響到其他任務(wù)的執(zhí)行。
- sqs 消息隊列的方式觸發(fā) lambda,可以對任務(wù)過多時進(jìn)行有效的“削峰”處理。
構(gòu)建轉(zhuǎn)換環(huán)境
由于 aws lambda 支持 docker 的方式運(yùn)行,因此我們可以構(gòu)建好基礎(chǔ)的轉(zhuǎn)換服務(wù)運(yùn)行環(huán)境,這樣只需要關(guān)注具體的業(yè)務(wù)邏輯編寫即可。
- 對于 html 的轉(zhuǎn)換方案,采用 wkhtmltopdf
- 對于 office(word/excel/powerpoint) 則采用開源的 libreoffice
由于我的業(yè)務(wù)環(huán)境使用 python 居多,因此這里選擇的基礎(chǔ)鏡像為 python:3.7
,這同時也是一個基于 Debain 的系統(tǒng)。
html 轉(zhuǎn)換方案
wkhtmltopdf 是一個使用 Qt WebKit 引擎做渲染的,能夠把 html 文檔轉(zhuǎn)換成 pdf 文檔 或 圖片(image) 的命令行工具。支持多個平臺,可在 win,linux,os x 等系統(tǒng)下運(yùn)行。
優(yōu)點(diǎn):
- 生成 PDF 時會自動根據(jù) HTML 頁面中 H 標(biāo)簽生成樹形目錄結(jié)構(gòu);
- 小巧方便,轉(zhuǎn)換速度快;
- 跨平臺,可以在 Liunx 下用,也可以在 win 使用;
下面是 Dockerfile 的關(guān)于安裝 wkhtmltopdf 的命令,其實(shí)過程主要是從 wkhtmltopdf 官網(wǎng)下載對應(yīng)的版本,只有通過 dpkg 進(jìn)行安裝。
# Install wkhtmltopdf
RUN wget -O wkhtmltox_0.12.6.1-2.bullseye_amd64.deb https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6.1-2/wkhtmltox_0.12.6.1-2.bullseye_amd64.deb && \
apt-get -y update && apt-get -y install xfonts-encodings xfonts-utils xfonts-base xfonts-75dpi && \
dpkg -i wkhtmltox_0.12.6.1-2.bullseye_amd64.deb && \
apt-get -y autoremove && apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
wkhtmltopdf --version
安裝完之后可以通過 python 的 os 調(diào)用系統(tǒng)命令進(jìn)行 html 文件的轉(zhuǎn)換,詳細(xì)的參數(shù)配置選擇可以參考官網(wǎng)。
wkhtmltopdf --page-size A4 --orientation Portrait --dpi 300 --image-dpi 600 --image-quality 94 <input name> <output name>
office 轉(zhuǎn)換方案
對于轉(zhuǎn)換 office 到 pdf,最好的方案是調(diào)用 office 的底層服務(wù),但這會緊緊局限在 windows 平臺,如果要進(jìn)行跨平臺使用,LibreOffice 是一個不錯的選擇,它能最大程度的兼容 word/excel/powerpoint,最終轉(zhuǎn)換效果也是很不錯的。
LibreOffice 在 linux 的安裝會比較復(fù)雜一些,同時需要安裝一些相應(yīng)的字體庫等。其安裝過程可以分為下面三步:
- 增加 LibreOffice “fresh” PPA
- 安裝語言包
- 安裝額外必須的包
- 安裝 LibreOffice
可以參考這篇文章獲得詳細(xì)的解釋,下面是 Dockerfile 的關(guān)于安裝 LibreOffice 的命令
# Install LibreOffice
RUN apt-get -y update && apt-get -y dist-upgrade &&\
apt-get -y install software-properties-common && \
add-apt-repository -y ppa:libreoffice/ppa && \
apt-get -y install locales libreoffice libreoffice-writer libreoffice-impress libreoffice-common && \
apt-get -y install fonts-opensymbol && \
apt-get -y install hunspell-en-us hyphen-en-us mythes-en-us libreoffice-help-en-us && \
apt-get -y install libreoffice-l10n-de hunspell-de-de-frami hyphen-de mythes-de libreoffice-help-de && \
apt-get -y install libreoffice-l10n-fr hunspell-fr-comprehensive hyphen-fr mythes-fr libreoffice-help-fr && \
apt-get -y install libreoffice-l10n-es hunspell-es hyphen-es mythes-es libreoffice-help-es && \
apt-get -y install fonts-dejavu fonts-dejavu-core fonts-dejavu-extra fonts-droid-fallback fonts-dustin fonts-f500 fonts-fanwood && \
apt-get -y install fonts-freefont-ttf fonts-liberation fonts-lmodern fonts-lyx fonts-sil-gentium fonts-texgyre fonts-tlwg-purisa && \
apt-get -y autoremove && apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8 && \
libreoffice --version
安裝完之后可以通過 python 的 os 調(diào)用系統(tǒng)命令進(jìn)行 html 文件的轉(zhuǎn)換。
libreoffice --headless --convert-to pdf <input name>
注意,如果在 lambda 上運(yùn)行的時候,一定要重新設(shè)置 HOME 路徑到 /tmp
下,否則會因?yàn)闊o權(quán)限創(chuàng)建緩存文件而報錯。
export HOME=/tmp && libreoffice --headless --convert-to pdf <input name>
txt 轉(zhuǎn)換方案
將 txt 轉(zhuǎn)換成 pdf 可以直接借用 libreoffce,命令也是一樣的,如下:文章來源:http://www.zghlxwxcb.cn/news/detail-405939.html
libreoffice --headless --convert-to pdf <input name>
參考文檔
[1] HTML 轉(zhuǎn) PDF 之 wkhtmltopdf 工具簡介 https://www.jianshu.com/p/559c594678b6/
[2] wkhtmltopdf https://wkhtmltopdf.org/downloads.html
[3] Ubuntu18.04中安裝wkhtmltopdf http://events.jianshu.io/p/07c8e7837c7e
[4] How to install LibreOffice 7.4 on Linux Mint, Ubuntu, MX Linux, Debian… https://libre-software.net/linux/ how-to-install-libreoffice-on-ubuntu-linux-mint/
[5] convert-document https://github.com/occrp-attic/convert-document/blob/master/Dockerfile
[6] Converting Office Docs to PDF with AWS Lambda https://gist.github.com/madhavpalshikar/96e72889c534443caefd89000b2e69b5文章來源地址http://www.zghlxwxcb.cn/news/detail-405939.html
到了這里,關(guān)于aws lambda 轉(zhuǎn)換 office/txt/html 為 pdf的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!