Render archives as HTML, PDF, and image files

GroupDocs.Viewer for Python via .Net allows you to view the contents of archive files in HTML, PDF, PNG, and JPEG formats. You do not need to use third-party file archiver and compression software to display archive file contents within your .NET application (web or desktop).

To start using the GroupDocs.Viewer API, create a Viewer class instance. Pass an archive file you want to view to the class constructor. You can load the archive from a file or stream. Call one of the Viewer.view method overloads to convert the archive file to HTML, PDF, or image format.

View archive files online View demos and examples on GitHub

Supported archive and compressed file formats

GroupDocs.Viewer supports the following archive file formats:

GroupDocs.Viewer can detect the archive file format automatically based on information in the file header.

Render archive files as HTML

To convert an archive file to HTML, call the HtmlViewOptions.for_embedded_resources method to create an HtmlViewOptions class instance and pass this instance to the Viewer.view method.

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

def render_archive_to_html():
    # Load archive file
    with Viewer("documents.zip") as viewer:
        # Create an HTML file for the top folder and each subfolder in the archive.
        # {0} is replaced with the current page number in the output file name.
        viewOptions = HtmlViewOptions.for_embedded_resources("render_archive_to_html/pdf_page_{0}.html")
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_archive_to_html()

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

The following image demonstrates the result:

Render an archive file to HTML

Specify the number of items to render

GroupDocs.Viewer supports the HtmlViewOptions.archive_options.items_per_page option that allows you to specify the number of archive items to display on each HTML page. The default property value is 16.

The following example demonstrates how to set this option in code:

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

def render_archive_with_items_per_page():
    # Load archive file
    with Viewer("documents.zip") as viewer:
        # Create an HTML file for the top folder and each subfolder in the archive.
        # {0} is replaced with the current page number in the output file name.
        viewOptions = HtmlViewOptions.for_embedded_resources("render_archive_with_items_per_page/archive_with_items_per_page_{0}.html")
        # Specify the number of items to display on each HTML page.
        viewOptions.archive_options.items_per_page = 10
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_archive_with_items_per_page()

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

Create a single HTML page

If you need to display the contents of an archive file on a single HTML page, enable the HtmlViewOptions.render_to_single_page option, as shown below:

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

def render_archive_to_single_html():
    # Load archive file
    with Viewer("documents.zip") as viewer:
        # Create an HTML file.
        viewOptions = HtmlViewOptions.for_embedded_resources("render_archive_to_single_html/archive_to_single_html.html")
        # Render the archive file to a single page.
        viewOptions.render_to_single_page = True
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_archive_to_single_html()

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

The animation below demonstrates the result. You can navigate between the archive folders. Click on a particular folder to see its contents. To go backward, click the required folder name in the navigation bar at the top of the web page.

Navigate between folders in the archive

Render archive files as PDF

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

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

def render_archive_to_pdf():
    # Load archive file
    with Viewer("documents.zip") as viewer:
        # Create a PDF file.
        viewOptions = PdfViewOptions("render_archive_to_pdf/archive_content.pdf")
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_archive_to_pdf()

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

The following image demonstrates the result:

Render an archive file to PDF

Render archive files as PNG

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

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

def render_archive_to_png():
    # Load archive file
    with Viewer("documents.zip") as viewer:
        # Create a PNG image for the top folder and each subfolder in the archive.
        # {0} is replaced with the current page number in the image name.
        viewOptions = PngViewOptions("render_archive_to_png/archive_page_0_{0}.png")
        # Set width and height.
        viewOptions.width = 800
        viewOptions.height = 1000
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_archive_to_png()

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

The following image demonstrates the result:

Render an archive file to PNG

Render archive files as JPEG

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

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

def render_archive_to_jpg():
    # Load archive file
    with Viewer("documents.zip") as viewer:
        # Create a JPEG image for the top folder and each subfolder in the archive.
        # {0} is replaced with the current page number in the image name.
        viewOptions = JpgViewOptions("render_archive_to_jpg/archive_to_jpg_{0}.jpg")
        # Set width and height.
        viewOptions.width = 800
        viewOptions.height = 1000
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_archive_to_jpg()

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

Render a specific folder

When you convert an archive file to HTML, PDF, or image format, GroupDocs.Viewer renders items from all folders contained in the archive. If you need to render items from a specific folder, specify the ArchiveOptions.folder property for one of the following classes (depending on the output file format):

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

def render_specific_archive_folder():

    # Load archive file
    with Viewer("documents.zip") as viewer:
        # Create an HTML file.
        viewOptions = HtmlViewOptions.for_embedded_resources("render_specific_archive_folder/specific_archive_folder.html")
        viewOptions.archive_options.folder = "first_folder"
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_specific_archive_folder()

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

Specify the archive file name

When rendering an archive file, GroupDocs.Viewer displays the archive file name in the header of each page. If you need to change or hide this name, define the archive_options.file_name property for a target view. You can set this option to one of the following values:

  • FileName.SOURCE— Returns the name of the source file (this name is used by default).
  • FileName.EMPTY—Specifies an empty name. Use this value to hide the archive file name in the output file.
  • A FileName instance with a custom name you want to display in the output file.

The following code snippet demonstrates how to use a custom name when rendering an archive file to HTML:

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

def render_archive_with_custom_filename():
    # Load archive file
    with Viewer("documents.zip") as viewer:
        # Create an HTML file for the top folder and each subfolder in the archive.
        # {0} is replaced with the current page number in the output file name.
        viewOptions = HtmlViewOptions.for_embedded_resources("render_archive_with_custom_filename/archive_with_custom_filename_{0}.html")
        # Specify a custom filename
        viewOptions.archive_options.file_name = FileName("Sample Files")
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_archive_with_custom_filename()

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

The image below demonstrates the result.

Set a custom archive name

Close
Loading

Analyzing your prompt, please hold on...

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