Image-to-PDF conversion

更新时间:
复制 MD 格式

Intelligent Media Management (IMM) allows you to convert multiple images into a single PDF file, which facilitates image searches. This topic describes how to use the feature of image conversion to PDF.

Scenarios

  • Image sharing: Image-to-PDF conversion makes it easier to share multiple images.

  • Print preparation: Image-to-PDF conversion can ensure that the printed documents have the original formatting and layout settings.

  • Cross-platform compatibility: PDF is a cross-platform file format that provides consistent content displays across different operating systems, such as Windows, macOS, or Linux.

Prerequisites

  • An AccessKey pair is created and obtained. For more information, see Create an AccessKey pair.

  • OSS is activated, a bucket is created, and objects are uploaded to the bucket. For more information, see Upload objects.

  • IMM is activated. For more information, see Activate IMM.

  • A project is created in the IMM console. For more information about how to create a project by using the IMM console, see Create a project.

    Note
    • You can also call the CreateProject operation to create a project. For more information, see CreateProject.

    • You can call the ListProjects operation to query existing projects in a specific region. For more information, see ListProjects.

Usage

Call the CreateImageToPDFTask operation to create a task that converts multiple images into a single PDF file.

Important

The task information is retained for seven days after the task starts. Task information cannot be obtained after the seven-day window ends. You can use one of the following methods to query task information:

Transform information

  • IMM project: test-project

  • Image location: oss://test-bucket/test-object.jpg

  • PDF location: oss://test-bucket/test-target-object.pdf

Sample request

{
    "ProjectName": "test-project",
    "Sources": "[{\"URI\": \"oss://test-bucket/test-object.jpg\"}]",
    "TargetURI": "oss://test-bucket/test-target-object.pdf",
}

Sample response

{
    "TaskId": "ImageToPDF-1c73b551-381d-46cc-86ed-16905*****",
    "RequestId": "657DE82D-5960-0309-810C-639637*****",
    "EventId": "124-1S7JgMQdZ3kWQbmUqXxI3******"
}

Sample code

The following sample code provides an example on how to use IMM SDK for Python to convert images into a PDF file:

# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys
import os
from typing import List

from alibabacloud_imm20200930.client import Client as imm20200930Client
from alibabacloud_tea_openapi import models as open_api_models
from alibabacloud_imm20200930 import models as imm_20200930_models
from alibabacloud_tea_util import models as util_models
from alibabacloud_tea_util.client import Client as UtilClient


class Sample:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
        access_key_id: str,
        access_key_secret: str,
    ) -> imm20200930Client:
        """
        Use your AccessKey ID and AccessKey secret to initialize the client. 
        @param access_key_id:
        @param access_key_secret:
        @return: Client
        @throws Exception
        """
        config = open_api_models.Config(
            access_key_id=access_key_id,
            access_key_secret=access_key_secret
        )
        # Specify the IMM endpoint. 
        config.endpoint = f'imm.cn-zhangjiakou.aliyuncs.com'
        return imm20200930Client(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. 
        # For security reasons, we recommend that you do not embed your AccessKey pair in your project code. 
        # In this example, the AccessKey pair is obtained from the environment variables to implement identity verification for API access. For information about how to configure environment variables, visit https://www.alibabacloud.com/help/en/imm/developer-reference/configure-environment-variables. 
        imm_access_key_id = os.getenv("AccessKeyId")
        imm_access_key_secret = os.getenv("AccessKeySecret")
        client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
        sources_0 = imm_20200930_models.CreateImageToPDFTaskRequestSources(
            uri='oss://test-bucket/test-object.jpg'
        )
        create_image_to_pdftask_request = imm_20200930_models.CreateImageToPDFTaskRequest(
            project_name='test-project',
            target_uri='oss://test-bucket/test-target-object.pdf',
            sources=[
                sources_0
            ]
        )
        runtime = util_models.RuntimeOptions()
        try:
            # Write your code to print the response of the API operation if necessary. 
            client.create_image_to_pdftask_with_options(create_image_to_pdftask_request, runtime)
        except Exception as error:
            # Print the error message if necessary. 
            UtilClient.assert_as_string(error.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        # The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. 
        # For security reasons, we recommend that you do not embed your AccessKey pair in your project code. 
        # In this example, the AccessKey pair is obtained from the environment variables to implement identity verification for API access. For information about how to configure environment variables, visit https://www.alibabacloud.com/help/en/imm/developer-reference/configure-environment-variables. 
        imm_access_key_id = os.getenv("AccessKeyId")
        imm_access_key_secret = os.getenv("AccessKeySecret")
        client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
        sources_0 = imm_20200930_models.CreateImageToPDFTaskRequestSources(
            uri='oss://test-bucket/test-object.jpg'
        )
        create_image_to_pdftask_request = imm_20200930_models.CreateImageToPDFTaskRequest(
            project_name='test-project',
            target_uri='oss://test-bucket/test-target-object.pdf',
            sources=[
                sources_0
            ]
        )
        runtime = util_models.RuntimeOptions()
        try:
            # Write your code to print the response of the API operation if necessary. 
            await client.create_image_to_pdftask_with_options_async(create_image_to_pdftask_request, runtime)
        except Exception as error:
            # Print the error message if necessary. 
            UtilClient.assert_as_string(error.message)


if __name__ == '__main__':
    Sample.main(sys.argv[1:])

Billing

During the image-to-PDF conversion process, the following billable items are generated on both OSS and IMM sides:

  • OSS side: For detailed pricing information, see OSS Pricing

    API

    Billable item

    Description

    GetObject

    GET requests

    You are charged request fees based on the number of successful requests.

    Data retrieval of IA objects

    If IA objects are retrieved, you are charged IA data retrieval fees based on the size of retrieved IA data.

    Data Retrieval Capacity from Archive Direct Read

    If Archive objects in a bucket for which real-time access is enabled are retrieved, You are charged Archive data retrieval fees based on the size of retrieved Archive objects.

    Transfer acceleration

    If you enable transfer acceleration and use an acceleration endpoint to access your bucket, you are charged transfer acceleration fees based on the data size.

    PutObject

    PUT requests

    You are charged request fees based on the number of successful requests.

    Storage fees

    You are charged storage fees based on the storage class, size, and storage duration of the object.

    HeadObject

    GET requests

    You are charged request fees based on the number of successful requests.

  • IMM side: For detailed pricing information, see IMM billable items.

    Important

    Starting from 11:00 on July 28, 2025 (UTC+8), the IMM image-to-PDF service will be upgraded from a free model to a paid model. For more information, see IMM billing adjustment announcement.

    API

    Billable item

    Description

    CreateImageToPDFTask

    ImageToPDF

    You are charged image-to-PDF conversion fees based on the number of converted images.