# Render text documents as HTML, PDF, and image files Convert text files to HTML, PDF, PNG, or JPEG using the GroupDocs.Viewer Python API. Learn how to render various file formats. [GroupDocs.Viewer for Python](https://products.groupdocs.com/viewer/python-net) 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](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer/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](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer/viewer/#methods) 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. {{< button style="primary" link="https://products.groupdocs.app/viewer/total" >}} {{< icon "gdoc_person" >}} View text files online {{< /button >}} {{< button style="primary" link="https://github.com/groupdocs-viewer/GroupDocs.Viewer-for-Python-via-.NET" >}} {{< icon "gdoc_github" >}} View demos and examples on GitHub {{< /button >}} ## Supported text file formats GroupDocs.Viewer supports the following text and programming file formats: * [.AS / .AS3 (ActionScript File)](https://docs.fileformat.com/programming/as/) * [.ASM (Assembly Language Source Code File)](https://docs.fileformat.com/programming/asm/) * [.BAT (DOS Batch File)](https://docs.fileformat.com/executable/bat/) * [.C (C/C++ Source Code File)](https://docs.fileformat.com/programming/c/) * [.CC (C++ Source Code File)](https://docs.fileformat.com/programming/c/) * [.CMAKE (CMake File)](https://docs.fileformat.com/programming/cmake/) * [.CPP (C++ Source Code File)](https://docs.fileformat.com/programming/cpp/) * [.CS (C# Source Code File)](https://docs.fileformat.com/specification/programming/cs/) * [.CSS (Cascading Style Sheet)](https://docs.fileformat.com/web/css/) * [.CXX (C++ Source Code File)](https://docs.fileformat.com/programming/cxx/) * [.DIFF (Patch File)](https://docs.fileformat.com/programming/diff/) * [.ERB (Ruby ERB Script)](https://docs.fileformat.com/programming/erb/) * [.GROOVY (Groovy Source Code File)](https://docs.fileformat.com/programming/groovy/) * [.H (C/C++/Objective-C Header File)](https://docs.fileformat.com/programming/h/) * [.HAML (Haml Source Code File)](https://docs.fileformat.com/programming/haml/) * [.HH (C++ Header File)](https://docs.fileformat.com/programming/hh/) * [.JAVA (Java Source Code File)](https://docs.fileformat.com/programming/python-net/) * [.JS (JavaScript File)](https://docs.fileformat.com/web/js/) * [.JSON (JavaScript Object Notation File)](https://docs.fileformat.com/web/json/) * [.LESS (LESS Style Sheet)](https://docs.fileformat.com/web/less/) * [.LOG (Log File)](https://docs.fileformat.com/database/log/) * [.M (Objective-C Implementation File)](https://docs.fileformat.com/programming/m/) * [.MAKE (Xcode Makefile Script)](https://docs.fileformat.com/programming/make/) * [.MD (Markdown Documentation File)](https://docs.fileformat.com/word-processing/md/) * [.ML (ML Source Code File)](https://docs.fileformat.com/programming/ml/) * [.MM (Objective-C++ Source File)](https://docs.fileformat.com/programming/mm/) * [.PHP (PHP Source Code File)](https://docs.fileformat.com/programming/php/) * [.PL (Perl Script)](https://docs.fileformat.com/programming/pl/) * [.PROPERTIES (Java Properties File)](https://en.wikipedia.org/wiki/.properties) * [.PY (Python Script)](https://docs.fileformat.com/programming/py/) * [.RB (Ruby Source Code)](https://docs.fileformat.com/ebook/rb/) * [.RST (reStructuredText File)](https://docs.fileformat.com/programming/rst/) * [.SASS (Syntactically Awesome StyleSheets File)](https://docs.fileformat.com/web/sass/) * [.SCALA (Scala Source Code File)](https://docs.fileformat.com/programming/scala/) * [.SCM (Scheme Source Code File)](https://docs.fileformat.com/programming/scm/) * [.SCRIPT (Generic Script File)](https://docs.fileformat.com/programming/script/) * [.SH (Bash Shell Script)](https://docs.fileformat.com/programming/sh/) * [.SML (Standard ML Source Code File)](https://en.wikipedia.org/wiki/Standard_ML) * [.SQL (Structured Query Language Data File)](https://docs.fileformat.com/database/sql/) * [.TXT (Plain Text File)](https://docs.fileformat.com/word-processing/txt/) * [.VB (Visual Basic Source Code File)](https://docs.fileformat.com/specification/programming/vb/) * [.VIM (Vim Settings File)](https://docs.fileformat.com/misc/vim/) * [.XML (XML File)](https://docs.fileformat.com/web/xml/) * [.YAML (YAML Document)](https://docs.fileformat.com/programming/yaml/) When you load a text document from a file, you should explicitly specify their format. To do this, create a [LoadOptions](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer.options/loadoptions/) class instance and use the [FileType](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer/filetype/) method. Then pass this instance to the [Viewer](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer/viewer/) class constructor. {{< tabs "example1">}} {{< tab "Python" >}} ```python 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() ``` {{< /tab >}} {{< tab "terms_of_service.txt" >}} {{< tab-text >}} `terms_of_service.txt` is the sample file used in this example. Click [here](https://docs.groupdocs.com/viewer/python-net/_sample_files/rendering-basics/render-text-files/terms_of_service.txt) to download it. {{< /tab-text >}} {{< /tab >}} {{< tab "text_with_load_options.pdf" >}} ```text Binary file (PDF, 85 KB) ``` [Download full output](https://docs.groupdocs.com/viewer/python-net/_output_files/rendering-basics/render-text-files/render_text_with_load_options/text_with_load_options.pdf) {{< /tab >}} {{< /tabs >}} ## Render text files as HTML Create an [HtmlViewOptions.for_embedded_resources](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer.options/htmlviewoptions/#methods) class instance and pass it to the [Viewer.view](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer/viewer/#methods) 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]({{< ref "viewer/python-net/developer-guide/rendering-documents/rendering-to-html/_index.md" >}}). ### 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](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer.options/htmlviewoptions/#methods) method and specify the output file name. {{< tabs "example3">}} {{< tab "Python" >}} ```python 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() ``` {{< /tab >}} {{< tab "terms_of_service.txt" >}} {{< tab-text >}} `terms_of_service.txt` is the sample file used in this example. Click [here](https://docs.groupdocs.com/viewer/python-net/_sample_files/rendering-basics/render-text-files/terms_of_service.txt) to download it. {{< /tab-text >}} {{< /tab >}} {{< tab "render-text-to-html-outputs.zip" >}} ```text render_text_to_html/pdf_page_1.html (2 KB) render_text_to_html/pdf_page_2.html (3 KB) render_text_to_html/pdf_page_3.html (2 KB) render_text_to_html/pdf_page_4.html (1 KB) ``` [Download full output](https://docs.groupdocs.com/viewer/python-net/_output_files/rendering-basics/render-text-files/render_text_to_html/render-text-to-html-outputs.zip) {{< /tab >}} {{< /tabs >}} ### 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](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer.options/htmlviewoptions/#methods) method and pass the following parameters: * The output file path format * The path format for the folder with external resources * The resource URL format {{< tabs "example4">}} {{< tab "Python" >}} ```python 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. # The resource URL is relative to the HTML file. 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}", "pdf_page_{0}/resource_{0}_{1}") viewer.view(viewOptions) if __name__ == "__main__": render_text_to_html_external() ``` {{< /tab >}} {{< tab "terms_of_service.txt" >}} {{< tab-text >}} `terms_of_service.txt` is the sample file used in this example. Click [here](https://docs.groupdocs.com/viewer/python-net/_sample_files/rendering-basics/render-text-files/terms_of_service.txt) to download it. {{< /tab-text >}} {{< /tab >}} {{< tab "render-text-to-html-external-outputs.zip" >}} ```text render_text_to_html_external/pdf_page_1.html (2 KB) render_text_to_html_external/pdf_page_1/resource_1_s.css (109 bytes) render_text_to_html_external/pdf_page_2.html (2 KB) render_text_to_html_external/pdf_page_2/resource_2_s.css (109 bytes) render_text_to_html_external/pdf_page_3.html (2 KB) render_text_to_html_external/pdf_page_3/resource_3_s.css (109 bytes) render_text_to_html_external/pdf_page_4.html (1 KB) render_text_to_html_external/pdf_page_4/resource_4_s.css (109 bytes) ``` [Download full output](https://docs.groupdocs.com/viewer/python-net/_output_files/rendering-basics/render-text-files/render_text_to_html_external/render-text-to-html-external-outputs.zip) {{< /tab >}} {{< /tabs >}} External resources are placed 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: {{< tabs "example5">}} {{< tab "Python" >}} ```python 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() ``` {{< /tab >}} {{< tab "terms_of_service.txt" >}} {{< tab-text >}} `terms_of_service.txt` is the sample file used in this example. Click [here](https://docs.groupdocs.com/viewer/python-net/_sample_files/rendering-basics/render-text-files/terms_of_service.txt) to download it. {{< /tab-text >}} {{< /tab >}} {{< tab "text_to_single_html.html" >}} ```text HTML file (7 KB) ``` [Download full output](https://docs.groupdocs.com/viewer/python-net/_output_files/rendering-basics/render-text-files/render_text_to_single_html/text_to_single_html.html) {{< /tab >}} {{< /tabs >}} ## Render text files as PDF Create a [PdfViewOptions](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer.options/pdfviewoptions) class instance and pass it to the [Viewer.view](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer/viewer/#methods) 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]({{< ref "viewer/python-net/developer-guide/rendering-documents/rendering-to-pdf/_index.md" >}}). {{< tabs "example6">}} {{< tab "Python" >}} ```python 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() ``` {{< /tab >}} {{< tab "terms_of_service.txt" >}} {{< tab-text >}} `terms_of_service.txt` is the sample file used in this example. Click [here](https://docs.groupdocs.com/viewer/python-net/_sample_files/rendering-basics/render-text-files/terms_of_service.txt) to download it. {{< /tab-text >}} {{< /tab >}} {{< tab "text_document.pdf" >}} ```text Binary file (PDF, 109 KB) ``` [Download full output](https://docs.groupdocs.com/viewer/python-net/_output_files/rendering-basics/render-text-files/render_text_to_pdf/text_document.pdf) {{< /tab >}} {{< /tabs >}} ## Render text files as PNG Create a [PngViewOptions](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer.options/pngviewoptions) class instance and pass it to the [Viewer.view](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer/viewer/#methods) method to convert a text file to PNG. Use the [PngViewOptions.height](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer.options/pngviewoptions/#properties) and [PngViewOptions.width](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer.options/pngviewoptions/#properties) methods to specify the output image size in pixels. {{< tabs "example7">}} {{< tab "Python" >}} ```python 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() ``` {{< /tab >}} {{< tab "terms_of_service.txt" >}} {{< tab-text >}} `terms_of_service.txt` is the sample file used in this example. Click [here](https://docs.groupdocs.com/viewer/python-net/_sample_files/rendering-basics/render-text-files/terms_of_service.txt) to download it. {{< /tab-text >}} {{< /tab >}} {{< tab "render-text-to-png-outputs.zip" >}} ```text render_text_to_png/text_page_0_1.png (99 KB) render_text_to_png/text_page_0_2.png (111 KB) render_text_to_png/text_page_0_3.png (99 KB) render_text_to_png/text_page_0_4.png (52 KB) ``` [Download full output](https://docs.groupdocs.com/viewer/python-net/_output_files/rendering-basics/render-text-files/render_text_to_png/render-text-to-png-outputs.zip) {{< /tab >}} {{< /tabs >}} ## Render text files as JPEG Create a [JpgViewOptions](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer.options/jpgviewoptions) class instance and pass it to the [Viewer.view](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer/viewer/#methods) method to convert a text file to JPEG. Use the [JpgViewOptions.height](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer.options/jpgviewoptions/#properties) and [JpgViewOptions.width](https://reference.groupdocs.com/viewer/python-net/groupdocs.viewer.options/jpgviewoptions/#properties) methods to specify the output image size in pixels. {{< tabs "example8">}} {{< tab "Python" >}} ```python 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() ``` {{< /tab >}} {{< tab "terms_of_service.txt" >}} {{< tab-text >}} `terms_of_service.txt` is the sample file used in this example. Click [here](https://docs.groupdocs.com/viewer/python-net/_sample_files/rendering-basics/render-text-files/terms_of_service.txt) to download it. {{< /tab-text >}} {{< /tab >}} {{< tab "render-text-to-jpg-outputs.zip" >}} ```text render_text_to_jpg/text_to_jpg_1.jpg (97 KB) render_text_to_jpg/text_to_jpg_2.jpg (112 KB) render_text_to_jpg/text_to_jpg_3.jpg (97 KB) render_text_to_jpg/text_to_jpg_4.jpg (53 KB) ``` [Download full output](https://docs.groupdocs.com/viewer/python-net/_output_files/rendering-basics/render-text-files/render_text_to_jpg/render-text-to-jpg-outputs.zip) {{< /tab >}} {{< /tabs >}}