×
Community Blog How to Develop Function Compute - Using Ghostscript to Convert PDF Files to JPG Files

How to Develop Function Compute - Using Ghostscript to Convert PDF Files to JPG Files

This tutorial is a part of the 'How to Develop Function Compute' series. It explains how to use Ghostscript for converting PDF files to JPG files.

By Du Wan (Yixian)

Preface

First, let's take a quick look at several key concepts mentioned in this article:

  • Function Compute: It's an event-driven service that allows users to focus on writing and uploading code without the need to manage infrastructure such as servers. Function Compute provides compute resources, allowing users to run code more elastically by just paying for the resources consumed while running the code. For more information about Function Compute, see here.
  • Fun: Also known as Funcraft, is a developer tool for serverless applications that helps in managing resources such as Function Compute, API Gateway, and Log Service. Use Fun to develop, build, and deploy resources by defining specified resources in the template.yml file. For more information about Fun, see here.
  • Ghostscript: It's a suite of software based on an interpreter for Adobe Systems' PostScript and Portable Document Format (PDF) page description languages. For more information, see the Wikipedia entry.

Note: The operations in this article are applicable to Fun version 3.0.0-beta.7 and other later versions.

Dependent Tools

This project development is based on MacOS. However, the tools involved are platform-independent and also applicable to the Linux and Windows operating systems. Before proceeding with the example, make sure that the following tools are correctly installed, updated to the latest version, and properly configured.

Fun is based on Docker to simulate the local environment.

MacOS users may use homebrew to install these tools:

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

Windows and Linux users may refer this article to install these tools. After the installation, first run the fun config command to initialize the configuration.

Note: Make sure to use Fun version 3.0.0-beta.7 or any later version.

$ fun --version
3.0.0-beta.7

Initialize a Project in the Local System

Run the fun init command to locally initialize a project according to the specified template.

fun init vangie/ghostscript-example

Install Dependencies

Execute the commands shown in the following snippet to install dependencies.

$ 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'

Call the Function Locally

Run the following commands to call the function locally.

$ 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

View the converted fun/tmp/invoke/ghostscript/%2jpg/test.jpg file.

Deploy the Function

Execute the commands below to deploy the function.

$ 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

Call the Function

Finally, run the commands shown below to call the function.

$ 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
0 0 0
Share on

Alibaba Cloud Serverless

97 posts | 7 followers

You may also like

Comments

Alibaba Cloud Serverless

97 posts | 7 followers

Related Products

  • Function Compute

    Alibaba 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 More
  • Serverless Workflow

    Visualization, O&M-free orchestration, and Coordination of Stateful Application Scenarios

    Learn More
  • Serverless Application Engine

    Serverless Application Engine (SAE) is the world's first application-oriented serverless PaaS, providing a cost-effective and highly efficient one-stop application hosting solution.

    Learn More
  • Super App Solution for Telcos

    Alibaba Cloud (in partnership with Whale Cloud) helps telcos build an all-in-one telecommunication and digital lifestyle platform based on DingTalk.

    Learn More