Render text documents as HTML, PDF, and image files

GroupDocs.Viewer for Python allows you to convert text documents to HTML, PDF, PNG, and JPEG formats so you can view document content in a web browser, PDF or image viewer application.

To start with the GroupDocs.Viewer API, create a Viewer class instance. Pass a text document you want to view to the class constructor. You can load the document from a file or stream. Call one of the Viewer.view method overloads to convert the image to HTML, PDF, PNG, or JPEG format. These methods allow you to render the entire document or specific pages.

View text files online View demos and examples on GitHub

Supported text file formats

GroupDocs.Viewer supports the following text and programming file formats:

When you load a text document from a file, you should explicitly specify their format. To do this, create a LoadOptions class instance and use the FileType method. Then pass this instance to the Viewer class constructor.

from groupdocs.viewer import Viewer, FileType
from groupdocs.viewer.options import LoadOptions, PdfViewOptions

def render_text_with_load_options():
    # Specify the file encoding. 
    load_options = LoadOptions(FileType.MD)
    # Convert the document to PDF.
    with Viewer("terms_of_service.txt", load_options) as viewer:
        viewOptions = PdfViewOptions("render_text_with_load_options/text_with_load_options.pdf")
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_text_with_load_options()

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

Render text files as HTML

Create an HtmlViewOptions.for_embedded_resources class instance and pass it to the Viewer.view method to convert a text file 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 HTML files with embedded resources

To save all elements of an HTML page (including text, graphics, and stylesheets) into a single file, 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_text_to_html():
    # Load text file
    with Viewer("terms_of_service.txt") as viewer:
        # Convert the text file to HTML.
        # {0} is replaced with the current page number in the output file names.
        viewOptions = HtmlViewOptions.for_embedded_resources("render_text_to_html/pdf_page_{0}.html")
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_text_to_html()

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

The following image demonstrates the result:

Render a text file to HTML

Create HTML files with external resources

If you want to store output HTML files and additional resource files (such as fonts, images, and style sheets) separately, 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_text_to_html_external():
    # Load text file
    with Viewer("terms_of_service.txt") as viewer:
        # Create an HTML file for each page.
        # Specify the HTML file names and location of external resources.
        # {0} and {1} are replaced with the current page number and resource name, respectively.
        viewOptions = HtmlViewOptions.for_external_resources("render_text_to_html_external/pdf_page_{0}.html", "render_text_to_html_external/pdf_page_{0}/resource_{0}_{1}", "render_text_to_html_external/pdf_page_{0}/resource_{0}_{1}")
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_text_to_html_external()

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

The image below demonstrates the result. External resources are placed in a separate folder.

Place HTML resources in a separate folder

Create a single HTML page

If you need to display the entire document content on a single HTML page, use the HtmlViewOptions.setRenderToSinglePage method, as shown below:

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

def render_text_to_single_html():
    # Load text file
    with Viewer("terms_of_service.txt") as viewer:
        # Create an HTML file.
        viewOptions = HtmlViewOptions.for_embedded_resources("render_text_to_single_html/text_to_single_html.html")
        # Render the file to a single page. 
        viewOptions.render_to_single_page = True
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_text_to_single_html()

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

Render text files as PDF

Create a PdfViewOptions class instance and pass it to the Viewer.view method to convert a text file 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_text_to_pdf():
    # Load text file
    with Viewer("terms_of_service.txt") as viewer:
        # Convert the text file to PDF.
        viewOptions = PdfViewOptions("render_text_to_pdf/text_document.pdf")
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_text_to_pdf()

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

The following image demonstrates the result:

Render a text file to PDF

Render text files as PNG

Create a PngViewOptions class instance and pass it to the Viewer.view method to convert a text file 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_text_to_png():
    # Load text file
    with Viewer("terms_of_service.txt") as viewer:
        # Convert the text file to PNG.
        # {0} is replaced with the current page number in the output image names.
        viewOptions = PngViewOptions("render_text_to_png/text_page_0_{0}.png")
        # Set width and height.
        viewOptions.width = 950
        viewOptions.height = 550
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_text_to_png()

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

The following image demonstrates the result:

Render a text file to PNG

Render text files as JPEG

Create a JpgViewOptions class instance and pass it to the Viewer.view method to convert a text file 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_text_to_jpg():
    # Load text file
    with Viewer("terms_of_service.txt") as viewer:
        # Convert the text file to JPEG.
        # {0} is replaced with the current page number in the output image names.
        viewOptions = JpgViewOptions("render_text_to_jpg/text_to_jpg_{0}.jpg")
        # Set width and height.
        viewOptions.width = 950
        viewOptions.height = 550
        viewer.view(viewOptions)

if __name__ == "__main__":
    render_text_to_jpg()

terms_of_service.txt 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.