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

本文涉及的产品
简介: 前言首先介绍下在本文出现的几个比较重要的概念:函数计算(Function Compute):函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。

前言

首先介绍下在本文出现的几个比较重要的概念:

函数计算(Function Compute):函数计算是一个事件驱动的服务,通过函数计算,用户无需管理服务器等运行情况,只需编写代码并上传。函数计算准备计算资源,并以弹性伸缩的方式运行用户代码,而用户只需根据实际代码运行所消耗的资源进行付费。函数计算更多信息参考
Fun:Fun 是一个用于支持 Serverless 应用部署的工具,能帮助您便捷地管理函数计算、API 网关、日志服务等资源。它通过一个资源配置文件(template.yml),协助您进行开发、构建、部署操作。Fun 的更多文档参考
Ghostscript:Ghostscript 是一套建基于Adobe、PostScript及可移植文档格式(PDF)的页面描述语言等而编译成的自由软件。参见维基百科词条

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

依赖工具

本项目是在 MacOS 下开发的,涉及到的工具是平台无关的,对于 Linux 和 Windows 桌面系统应该也同样适用。在开始本例之前请确保如下工具已经正确的安装,更新到最新版本,并进行正确的配置。

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 初始化一下配置。

注意, 如果你已经安装过了 fun,确保 fun 的版本在 3.0.0-beta.7 以上。

$ fun --version
3.0.0-beta.7

初始化

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

fun init vangie/ghostscript-example

安装依赖

$ fun install

Installing recursively on fun.yml

skip 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); 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;

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 1

2019-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: 60

Waiting 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 MB
        function 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 1

2019-09-18T09:52:39.802Z 1411066b-1ad0-4750-922d-2350652ca5a6 [verbose] stdout =================== END
FC Invoke End RequestId: 1411066b-1ad0-4750-922d-2350652ca5a6

Duration: 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

参考阅读

  1. 手把手教您将 Ghostscript 移植到函数计算平台
相关实践学习
基于函数计算一键部署掌上游戏机
本场景介绍如何使用阿里云计算服务命令快速搭建一个掌上游戏机。
建立 Serverless 思维
本课程包括: Serverless 应用引擎的概念, 为开发者带来的实际价值, 以及让您了解常见的 Serverless 架构模式
目录
相关文章
|
5月前
|
运维 Serverless 云计算
云上开发新范式:Serverless 的必然与应然 | 云栖深度对话
云上开发新范式:Serverless 的必然与应然 | 云栖深度对话
|
2月前
|
开发框架 前端开发 .NET
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
为了便于大家查找,特将之前开发的.Net Core相关的五大案例整理成文,共计440页,32w字,免费提供给大家,文章底部有PDF下载链接。
33 1
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
|
2月前
|
人工智能 Serverless API
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
|
2月前
|
小程序 IDE Serverless
【经验分享】支付宝小程序serverless云开发拓荒
【经验分享】支付宝小程序serverless云开发拓荒
80 0
|
3月前
|
人工智能 Serverless API
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
7710 132
|
3月前
|
人工智能 Serverless API
AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案
为了帮助用户高效率、低成本应对企业级复杂场景,函数计算团队正式推出 Stable Diffusion API Serverless 版解决方案,通过使用该方案,用户可以充分利用 Stable Diffusion +Serverless 技术优势快速开发上线 AI 绘画应用,期待为广大开发者 AI 绘画创业及变现提供思路。
87315 4
|
3月前
|
安全 NoSQL Java
从安卓转到Java开发,我吃透了这份pdf,终于4面拿下美团offer
先说说个人情况吧,坐标广州,16年从一所普通二本大学毕业,毕业后在一家小公司干android开发,年薪在15w左右。转Java的契机是认识到了一个朋友,做Java后台的,经常跟他聊相关的内容,经过慎重考虑及个人的发展规划,所以就决定转型了。
|
4月前
|
XML Java Android开发
Android App开发手机阅读中PDF文件渲染器的讲解及使用(附源码 简单易懂)
Android App开发手机阅读中PDF文件渲染器的讲解及使用(附源码 简单易懂)
82 0
|
4月前
|
运维 Serverless API
Serverless云开发入门
Serverless云开发入门
|
5月前
|
运维 Serverless 云计算
飞天技术观|云上开发新范式:Serverless的必然与应然
在2022年云栖大会上宣布核心产品全面Serverless化之前,阿里云在Serverless领域布局已经有六七年积累了,一直在打磨产品,夯实基础;从去年开始,阿里云进入到了Serverless的第二阶段,一路突飞猛进,相对应地,阿里云Serverless产品数量和丰富度都在快速增长,业务规模也在快速增长。
474 1

相关产品

  • 函数计算