Render text documents as HTML, PDF, and image files
Leave feedback
On this page
GroupDocs.Viewer for Node.js 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.
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.
// Specify the file encoding.
constloadOptions=newLoadOptions(FileType.MD)// Convert the document to PDF.
constviewer=newgroupdocs.viewer.Viewer("TermsOfService.txt",loadOptions)constviewOptions=groupdocs.viewer.PdfViewOptions("output.pdf")viewer.view(viewOptions)
Render text files as HTML
Create an HtmlViewOptions 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.forEmbeddedResources method and specify the output file name.
constviewer=newgroupdocs.viewer.Viewer("TermsOfService.txt")// Convert the text file to HTML.
// {0} is replaced with the current page number in the output file names.
constviewOptions=groupdocs.viewer.HtmlViewOptions.forEmbeddedResources("page_{0}.html")viewer.view(viewOptions)
The following image demonstrates the result:
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.forExternalResources method and pass the following parameters:
The output file path format
The path format for the folder with external resources
The resource URL format
constviewer=newgroupdocs.viewer.Viewer("TermsOfService.txt")// Create an HTML file for each PDF 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.
constviewOptions=groupdocs.viewer.HtmlViewOptions.forExternalResources("page_{0}.html","page_{0}/resource_{0}_{1}","page_{0}/resource_{0}_{1}")viewer.view(viewOptions)
The image below demonstrates the result. External resources are placed in a separate folder.
constviewer=newgroupdocs.viewer.Viewer("TermsOfService.txt")// Create an HTML file.
constviewOptions=groupdocs.viewer.HtmlViewOptions.forEmbeddedResources("output.html")// Render the file to a single page.
viewOptions.setRenderToSinglePage(true)viewer.view(viewOptions)
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.
constviewer=newgroupdocs.viewer.Viewer("TermsOfService.txt")// Convert the text file to PDF.
constviewOptions=groupdocs.viewer.PdfViewOptions("output.pdf")viewer.view(viewOptions)
constviewer=newgroupdocs.viewer.Viewer("TermsOfService.txt")// Convert the text file to PNG.
// {0} is replaced with the current page number in the output image names.
constviewOptions=groupdocs.viewer.PngViewOptions("output_{0}.png")// Set width and height.
viewOptions.setWidth(950)viewOptions.setHeight(550)viewer.view(viewOptions)
constviewer=newgroupdocs.viewer.Viewer("TermsOfService.txt")// Convert the text file to JPEG.
// {0} is replaced with the current page number in the output image names.
constviewOptions=groupdocs.viewer.JpgViewOptions("output_{0}.jpg")// Set width and height.
viewOptions.setWidth(950)viewOptions.setHeight(550)viewer.view(viewOptions)
Specify rendering options
GroupDocs.Viewer supports the TextOptions class that allows you to specify different options for rendering text files. To access these options, use the setTextOptions method for one of the following classes (depending on the output file format):
The TextOptions class supports the following methods:
setMaxRowsPerPage—Specifies the maximum number of rows per page. The default value is 55.
setMaxCharsPerRow—Specifies the maximum number of characters per row. The default value is 85.
The following example demonstrates how to change the number of rows displayed on each HTML page:
constviewer=newgroupdocs.viewer.Viewer("TermsOfService.txt")// Convert the text file to HTML.
// {0} is replaced with the current page number in the output file names.
constviewOptions=groupdocs.viewer.HtmlViewOptions.forEmbeddedResources("page_{0}.html")// Set the maximum number of rows per page.
viewOptions.getTextOptions().setMaxRowsPerPage(30)viewer.view(viewOptions)
The image below illustrates the result:
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.