Save Generated Barcodes

The Aspose.BarCode Cloud SDK for Python by the GenerateApi provides methods to generate barcodes to the urllib3.response.HTTPResponse. Then you can work with result in order to obtain the desired outcomes. This API supports various barcode formats and configurations, enabling users to customize barcode generation to fit their needs.

Methods

  • generate: Generates a barcode via GET request.
  • generate_body: Generates a barcode via POST request with JSON or XML body.
  • generate_multipart: Generates a barcode via POST request with multipart form data. All of these methods return a urllib3.response.HTTPResponse.

Saving the Barcode to File

Below are examples demonstrating how to save the generated barcode stream to a file.

Example: Using generate

import os
from aspose_barcode_cloud import ApiClient, GenerateApi, EncodeBarcodeType, Configuration


def make_configuration():
    env_token = os.getenv("TEST_CONFIGURATION_ACCESS_TOKEN")
    if env_token:
        config = Configuration(access_token=env_token)
    else:
        config = Configuration(
            client_id="Client Id from https://dashboard.aspose.cloud/applications",
            client_secret="Client Secret from https://dashboard.aspose.cloud/applications",
        )
    return config


def main():
    configuration = make_configuration()
    api_client = ApiClient(configuration=configuration)
    api = GenerateApi(api_client=api_client)

    file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "testdata", "code128.jpeg"))

    response = api.generate(EncodeBarcodeType.CODE128, "Aspose.BarCode.Cloud")

    with open(file_name, "wb") as f:
        f.write(response.data)

    print(f"File '{file_name}' generated.")


if __name__ == "__main__":
    main()

Download Result Image

Example: Using generate_body

import os
from aspose_barcode_cloud import (
    ApiClient,
    EncodeBarcodeType,
    EncodeDataType,
    EncodeData,
    GenerateParams,
    BarcodeImageParams,
    BarcodeImageFormat,
    Configuration,
)
from aspose_barcode_cloud.api.generate_api import GenerateApi


def make_configuration():
    env_token = os.getenv("TEST_CONFIGURATION_ACCESS_TOKEN")
    if env_token:
        config = Configuration(access_token=env_token)
    else:
        config = Configuration(
            client_id="Client Id from https://dashboard.aspose.cloud/applications",
            client_secret="Client Secret from https://dashboard.aspose.cloud/applications",
        )
    return config


def main():
    file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "testdata", "pdf417.png"))

    configuration = make_configuration()
    api_client = ApiClient(configuration=configuration)
    generate_api = GenerateApi(api_client=api_client)

    generate_params = GenerateParams(
        barcode_type=EncodeBarcodeType.PDF417,
        encode_data=EncodeData(data_type=EncodeDataType.STRINGDATA, data="Aspose.BarCode.Cloud"),
        barcode_image_params=BarcodeImageParams(
            foreground_color="#FF5733",
            background_color="#FFFFFF",
            image_format=BarcodeImageFormat.JPEG,
        ),
    )

    response = generate_api.generate_body(generate_params)

    with open(file_name, "wb") as stream:
        stream.write(response.data)

    print(f"File '{file_name}' generated.")


if __name__ == "__main__":
    main()

Download Result Image

Example: Using generate_multipart

import os
from aspose_barcode_cloud import ApiClient, EncodeBarcodeType, Configuration
from aspose_barcode_cloud.api.generate_api import GenerateApi


def make_configuration():
    env_token = os.getenv("TEST_CONFIGURATION_ACCESS_TOKEN")
    if env_token:
        config = Configuration(access_token=env_token)
    else:
        config = Configuration(
            client_id="Client Id from https://dashboard.aspose.cloud/applications",
            client_secret="Client Secret from https://dashboard.aspose.cloud/applications",
        )
    return config


def main():
    """Main function to generate a barcode and save it as a file."""
    file_name = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "testdata", "pdf417.png"))

    configuration = make_configuration()
    api_client = ApiClient(configuration=configuration)
    generate_api = GenerateApi(api_client)

    response = generate_api.generate_multipart(EncodeBarcodeType.PDF417, "Aspose.BarCode.Cloud")

    # Save the generated barcode image
    with open(file_name, "wb") as stream:
        stream.write(response.data)

    print(f"File '{file_name}' generated.")


if __name__ == "__main__":
    main()

Download Result Image

Conclusion

The Aspose.BarCode Cloud SDK for Python offers a robust solution for generating barcodes in various formats, with flexible configuration options. By using the provided methods, developers can easily create and save barcode images to files, streamlining workflows that require barcode generation. Whether you’re working with simple barcodes like Code128 or more complex ones like QR Codes and DataMatrix, this API provides the tools needed to customize and produce high-quality barcode images.