Render images as HTML, PDF, PNG, and JPEG files

GroupDocs.Viewer for Python via .NET allows you to load images in various formats and convert them to HTML, PDF, PNG, and JPEG. Incorporate this library into your Python application (web or desktop) to build your own image viewer.

To start with the GroupDocs.Viewer API, create a Viewer class instance. Pass an image you want to view to the class constructor. You can load the image from a file or stream. Call one of the Viewer.view method overloads to convert the image to HTML, PDF, PNG, or JPEG format. For multipage images (such as TIFF, CDR, DICOM, WebP, and so on), you can specify the pages to render.

View image files online View demos and examples on GitHub

Supported image file formats

GroupDocs.Viewer supports the following image file formats:

Render images as HTML

Create an HtmlViewOptions class instance and pass it to the Viewer.view method to convert an image to HTML. The HtmlViewOptions class properties allow you to control the conversion process. For instance, you can embed all external resources in the generated HTML file, minify the output file, and optimize it for printing. Refer to the following documentation section for details: Rendering to HTML.

Create an HTML file with embedded resources

To embed an image in an HTML page, call the HtmlViewOptions.for_embedded_resources method and specify the output file name.

from groupdocs.viewer import Viewer
from groupdocs.viewer.options import HtmlViewOptions

def render_image_to_html():
    # Load image
    with Viewer("vector-image.svg") as viewer:
        # Specify the HTML file name.
        viewOptions = HtmlViewOptions.for_embedded_resources("render_image_to_html/pdf_page_{0}.html")
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_image_to_html()

vector-image.svg is the sample file used in this example. Click here to download it.

The following image demonstrates the result:

Render an image to HTML

Create an HTML file with external resources

To save an image to a separate folder, call the HtmlViewOptions.for_external_resources method and pass the following parameters:

  • The output file path format
  • The path format for the folder with external resources
  • The resource URL format
from groupdocs.viewer import Viewer
from groupdocs.viewer.options import HtmlViewOptions

def render_image_to_html_external():
    # Load image
    with Viewer("vector-image.svg") as viewer:
        # Specify the HTML file name and location of external resources.
        # {0} is replaced with the resource name in the output file name.
        viewOptions = HtmlViewOptions.for_external_resources("render_image_to_html_external/pdf_page_{0}.html", "render_image_to_html_external/pdf_page_{0}/resource_{0}_{1}", "render_image_to_html_external/pdf_page_{0}/resource_{0}_{1}")
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_image_to_html_external()

vector-image.svg is the sample file used in this example. Click here to download it.

The result is shown below. The image is placed in a separate folder.

Place HTML resources in a separate folder

Render images as PDF

Create a PdfViewOptions class instance and pass it to the Viewer.view method to convert an image to PDF. The PdfViewOptions class properties allow you to control the conversion process. For instance, you can protect the output PDF file, reorder its pages, and specify the quality of document images. Refer to the following documentation section for details: Rendering to PDF.

from groupdocs.viewer import Viewer
from groupdocs.viewer.options import PdfViewOptions

def render_image_to_pdf():
    # Load image
    with Viewer("vector-image.svg") as viewer:
        # Create a PDF file.
        viewOptions = PdfViewOptions("render_image_to_pdf/pdf_document.pdf")
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_image_to_pdf()

vector-image.svg is the sample file used in this example. Click here to download it.

The following image demonstrates the result:

Render an image to PDF

Convert images to PNG

Create a PngViewOptions class instance and pass it to the Viewer.view method to convert an image to PNG. Use the PngViewOptions.height and PngViewOptions.width methods to specify the output image size in pixels.

from groupdocs.viewer import Viewer
from groupdocs.viewer.options import PngViewOptions

def render_image_to_png():
    # Load image
    with Viewer("vector-image.svg") as viewer:
        # Create a PNG image.
        viewOptions = PngViewOptions("render_image_to_png/image.png")
        # Set width and height.
        viewOptions.width = 950
        viewOptions.height = 550
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_image_to_png()

vector-image.svg is the sample file used in this example. Click here to download it.

The following image demonstrates the result:

Render an image to PNG

Convert images to JPEG

Create a JpgViewOptions class instance and pass it to the Viewer.view method to convert an image to JPEG. Use the JpgViewOptions.height and JpgViewOptions.width methods to specify the output image size in pixels.

from groupdocs.viewer import Viewer
from groupdocs.viewer.options import JpgViewOptions

def render_image_to_jpg():
    # Load image
    with Viewer("vector-image.svg") as viewer:
        # Create a JPG image.
        viewOptions = JpgViewOptions("render_image_to_jpg/image_to_jpg.jpg")
        # Set width and height.
        viewOptions.width = 950
        viewOptions.height = 550
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_image_to_jpg()

vector-image.svg is the sample file used in this example. Click here to download it.

Render a PSD file with custom fonts

When you render a PSD file with custom fonts, you can specify a folder that contains necessary fonts to prevent font substitution during rendering. To do this, follow the steps below:

  1. Create a FolderFontSource class instance and specify a path to the folder that stores custom fonts. Pass a SearchOption enumeration member to the class constructor to define the search scope. The following options are available:

    • TOP_FOLDER_ONLY—Searches for the fonts only in the current folder.
    • ALL_FOLDERS—Searches for the fonts in the current folder and its subfolders.
  2. Call the FontSettings.set_font_sources static method and pass the specified font source to this method as a parameter. This method allows you to specify multiple font sources.

You can also use the ViewOptions.default_font_name method to specify the default font that should be used when a particular font is not found.

import os
from groupdocs.viewer import Viewer
from groupdocs.viewer.fonts import FolderFontSource, SearchOption, FontSettings
from groupdocs.viewer.options import JpgViewOptions

def render_psd_with_custom_fonts():
    # Create font sources.
    os.makedirs("./custom_fonts_folder", exist_ok=True)
    os.makedirs("./custom_additional_fonts_folder", exist_ok=True)

    # Add custom fonts folder to look for fonts recursively. (look into subfolders too).
    folderFontSource = [FolderFontSource("./custom_fonts_folder", SearchOption.ALL_FOLDERS)]
    # Add custom fonts folder to look for fonts only in this folder (without subfolders).
    additionalFontSource = [FolderFontSource("./custom_additional_fonts_folder", SearchOption.TOP_FOLDER_ONLY)]
    # Call SetFontSources method and supply font sources as arguments.
    FontSettings.set_font_sources(folderFontSource, additionalFontSource)

    # Load PSD file
    with Viewer("sample.psd") as viewer:
        # Create a JPG image.
        viewOptions = JpgViewOptions("render_psd_with_custom_fonts/psd_with_custom_fonts.jpg")
        viewOptions.default_font_name = "Arial"
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_psd_with_custom_fonts()

sample.psd is the sample file used in this example. Click here to download it.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.