Convert PNG to PDF

Convert PNG to PDF

Using Aspose.Imaging for Python via .NET, developers can convert PNG to PDF formats. This article shows how to export/convert PNG file to PDF formats with Aspose.Imaging. Aspose.Imaging for Python via .NET provides the Image class to load PNG files and same can be used to convert the PNG to PDF formats. The following code snippet shows you how to convert PNG to PDF image.

import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.fileformats.png import PngImage
from aspose.imaging.fileformats.pdf import PdfDocumentInfo
from aspose.imaging.imageoptions import PdfOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.png")), PngImage) as image:
export_options = PdfOptions()
export_options.pdf_document_info = PdfDocumentInfo()
image.save(os.path.join(data_dir, "result.pdf"), export_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.pdf"))