日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

开发函数计算的正确姿势——借助 Ghostscript 将 PDF 转换成 JPG

發布時間:2024/8/23 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 开发函数计算的正确姿势——借助 Ghostscript 将 PDF 转换成 JPG 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

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

函數計算(Function Compute):函數計算是一個事件驅動的服務,通過函數計算,用戶無需管理服務器等運行情況,只需編寫代碼并上傳。函數計算準備計算資源,并以彈性伸縮的方式運行用戶代碼,而用戶只需根據實際代碼運行所消耗的資源進行付費。
Fun:Fun 是一個用于支持 Serverless 應用部署的工具,能幫助您便捷地管理函數計算、API 網關、日志服務等資源。它通過一個資源配置文件(template.yml),協助您進行開發、構建、部署操作。
Ghostscript:Ghostscript 是一套建基于Adobe、PostScript及可移植文檔格式(PDF)的頁面描述語言等而編譯成的自由軟件。

備注: 本文介紹的技巧需要 Fun 版本大于等于 3.0.0-beta.7 。

依賴工具

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

  • Docker
  • Fun

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

對于 MacOS 用戶可以使用?homebrew?進行安裝:

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

Windows 和 Linux 用戶安裝請參考:

  • https://github.com/aliyun/fun/blob/master/docs/usage/installation.md
  • 安裝好后,記得先執行?fun config?初始化一下配置。

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

    $ fun --version 3.0.0-beta.7

    初始化

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

    fun init vangie/ghostscript-example

    安裝依賴

    $ fun installInstalling recursively on fun.ymlskip pulling image aliyunfc/runtime-python3.6:build-1.6.1... Task => workaround for update-gsfontmap=> bash -c 'mkdir -p /code/.fun/root/etc/ghostscript/cidfmap.d/ && mkdir -p /code/.fun/root/etc/ghostscript/fontmap.d/ && mkdir -p /etc/ghostscript/ && mkdir -p /var/lib/ghostscript/ && mkdir -p /code/.fun/root/var/lib/ghostscript/fonts && ln -s /code/.fun/root/etc/ghostscript/cidfmap.d /etc/ghostscript/ && ln -s /code/.fun/root/etc/ghostscript/fontmap.d /etc/ghostscript/ && ln -s /code/.fun/root/var/lib/ghostscript/fonts /var/lib/ghostscript/' Task => [UNNAMED]=> apt-get update (if need)=> apt-get install -y -d -o=dir::cache=/code/.fun/tmp/install ghostscript --reinstall=> bash -c for f in $(ls /code/.fun/tmp/install/archives/*.deb); dodpkg -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;Creating config file /etc/papersize with new version=> bash -c 'rm -rf /code/.fun/tmp/install/archives'

    本地調用

    $ fun local invoke pdf2jpg using template: template.yml skip pulling image aliyunfc/runtime-nodejs10:1.6.1... FC Invoke Start RequestId: 21d9c646-1db4-403c-b018-cd4246e193d3 load code for handler:index.handler 2019-09-18T09:45:38.400Z 21d9c646-1db4-403c-b018-cd4246e193d3 [verbose] stdout =================== START 2019-09-18T09:45:38.400Z 21d9c646-1db4-403c-b018-cd4246e193d3 [verbose] GPL Ghostscript 9.26 (2018-11-20) Copyright (C) 2018 Artifex Software, Inc. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. Processing pages 1 through 1. Page 12019-09-18T09:45:38.401Z 21d9c646-1db4-403c-b018-cd4246e193d3 [verbose] stdout =================== END FC Invoke End RequestId: 21d9c646-1db4-403c-b018-cd4246e193d3 convert success. JPG file save to /tmp/test.jpg 2019-09-18T09:45:38.416Z 21d9c646-1db4-403c-b018-cd4246e193d3 [error](node:21) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.RequestId: 21d9c646-1db4-403c-b018-cd4246e193d3 Billed Duration: 2132 ms Memory Size: 1998 MB Max Memory Used: 78 MB

    可以查看文件 .fun/tmp/invoke/ghostscript/pdf2jpg/test.jpg ,預留轉換后的效果。

    部署

    $ fun deploy using template: template.yml using region: cn-shanghai using accountId: ***********4733 using accessKeyId: ***********EUz3 using timeout: 60Waiting for service ghostscript to be deployed...Waiting for function pdf2jpg to be deployed...Waiting for packaging function pdf2jpg code...The function pdf2jpg has been packaged. A total of 1054 files files were compressed and the final size was 23.44 MBfunction pdf2jpg deploy success service ghostscript deploy success

    執行

    $ fun invoke pdf2jpg fun invoke pdf2jpg using template: template.yml ========= FC invoke Logs begin ========= FC Invoke Start RequestId: 1411066b-1ad0-4750-922d-2350652ca5a6 load code for handler:index.handler 2019-09-18T09:52:39.802Z 1411066b-1ad0-4750-922d-2350652ca5a6 [verbose] stdout =================== START 2019-09-18T09:52:39.802Z 1411066b-1ad0-4750-922d-2350652ca5a6 [verbose] GPL Ghostscript 9.26 (2018-11-20) Copyright (C) 2018 Artifex Software, Inc. All rights reserved. This software comes with NO WARRANTY: see the file PUBLIC for details. Processing pages 1 through 1. Page 12019-09-18T09:52:39.802Z 1411066b-1ad0-4750-922d-2350652ca5a6 [verbose] stdout =================== END FC Invoke End RequestId: 1411066b-1ad0-4750-922d-2350652ca5a6Duration: 513.34 ms, Billed Duration: 600 ms, Memory Size: 128 MB, Max Memory Used: 56.49 MB ========= FC invoke Logs end =========FC Invoke Result: convert success. JPG file save to /tmp/test.jpg


    原文鏈接
    本文為云棲社區原創內容,未經允許不得轉載。

    總結

    以上是生活随笔為你收集整理的开发函数计算的正确姿势——借助 Ghostscript 将 PDF 转换成 JPG的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。