1. Products
  2.   HTML
  3.   GO
  4.   HTML2Text
 
  

Free Go Library to Convert HTML Documents to Text

HTML2Text is a Very Simple Open Source Go Library That Can Transform HTML Markup into Human-Readable Plain text inside Go Apps.

In today's digital landscape, content needs to be accessible across multiple formats and platforms. HTML2Text is a powerful Go library created by Jay Taylor that solves a fundamental challenge: converting HTML content into readable, well-formatted plain text. Originally developed to ensure HTML emails remain readable by all recipients—including those with HTML-disabled clients or accessibility needs—this library has evolved into a versatile tool for developers working with content transformation.

The library transforms complex HTML structures, including tables, lists, and links, into clean, markdown-flavored plain text while preserving the logical structure and readability. There are several important features part of the library, such as Intelligent HTML-to-Text conversion, table rendering with pretty formatting, customizable conversion options, command line Interface, and so on. HTML2Text is written in pure Go with no external dependencies, making it fast for typical HTML documents, memory-efficient with streaming-friendly design and thread-safe for concurrent use in web servers. With its simple API, flexible options, and proven track record in thousands of projects, HTML2Text deserves a place in your Go toolkit.

Previous Next

Getting Started with HTML2Text

The recommended and easiest way to install HTML2Text is using GitHub. Please use the following command a smooth installation.

Install HTML2Text Library via GitHub

go get jaytaylor.com/html2text

The library requires Go 1.x or newer and has zero external dependencies, keeping your project lightweight. You can also install it manually; download the latest release files directly from GitHub repository.

Basic HTML to Text Conversion via Go

The open source HTML2Text library makes it easy for software developers to load and convert HTML file to text format inside their Go applications. HTML2Text doesn't just strip tags—it intelligently interprets HTML structure to produce human-readable output with Semantic understanding of headings, paragraphs, and lists, link preservation with URLs displayed in parentheses after link text, whitespace normalization that maintains readability without excessive line breaks, character encoding support including UTF-8 with BOM (Byte Order Mark). Here’s a full example of using the library that allows to load and convert HTML to text format inside Go applications.

How to Convert Simple HTML Content into Text Format via Go Library?

package main

import (
    "fmt"
    "jaytaylor.com/html2text"
)

func main() {
    html := `
        

Welcome to Our Service

Check out our features page for more information about what we offer.

  • 24/7 Support
  • Free Trial
  • Enterprise Plans
` text, err := html2text.FromString(html, html2text.Options{}) if err != nil { panic(err) } fmt.Println(text) }

Table Rendering with Pretty Formatting

One of the standout features of HTML2Text is its ability to render HTML tables as ASCII-based text tables. Tables in plain text typically become jumbled data soup. The PrettyTables feature preserves tabular structure using ASCII art, making numerical data, pricing tables, or comparison charts readable in any context. These are perfect for automated reports sent via email, command-line tool output, or SMS notifications where data structure matters. The following code example shows, how software developers can convert HTML table to formatted ASCII table using Go commands.

How to Convert HTML Table to Formatted ASCII Table via Go?

func sendMonthlyReport(userEmail string, metricsHTML string) {
    // Convert HTML table to formatted ASCII table
    textReport, _ := html2text.FromString(metricsHTML, html2text.Options{
        PrettyTables: true, // Preserves table structure visually
    })
    
    // Even in plain text, the data remains organized:
    // +----------------+-----------+
    // | METRIC         | VALUE     |
    // +----------------+-----------+
    // | Page Views     | 24,589    |
    // | Conversion Rate| 2.4%      |
    // +----------------+-----------+
    
    sendEmail(userEmail, "Monthly Report", textReport)
}

Email Fallback Generation via Go Library

One of the most common use cases for HTML2Text is converting HTML email templates into their plain text equivalents. Email clients vary widely in their HTML rendering capabilities. Some security-conscious users disable HTML entirely, while older clients or screen readers for visually impaired users often work better with plain text. Providing a text alternative isn't just good practice—it's essential for accessibility and deliverability. By automatically generating synchronized plain text versions, you ensure every recipient can read your content, reduce spam filter triggers, and comply with accessibility standards like WCAG.

How to Generate Plain Text Fallback of an Email Message via Go Library?

func sendTransactionalEmail(userEmail, htmlContent string) error {
    // Generate plain text fallback
    textContent, err := html2text.FromString(htmlContent, html2text.Options{})
    if err != nil {
        return fmt.Errorf("failed to create text alternative: %v", err)
    }
    
    // Send multipart email (both HTML and text)
    email := &Email{
        To: userEmail,
        HTMLBody: htmlContent,
        TextBody: textContent, // Critical for users with HTML disabled
    }
    
    return emailService.Send(email)
}

Working with Links During HTML to Text

The HTML2Text library has included complete support for handling links inside HTML to text conversion. Links are particularly important in HTML content, and html2text ensures that both the link text and the URL are preserved. The link text is displayed normally, while the actual URL is appended in parentheses, ensuring that readers of the plain text version can still access the linked resources. The following example demonstrates how to achieve this.

How to Manage Links While Converting HTML to Text inside Go Apps?

package main

import (
    "fmt"
    "log"
    
    "jaytaylor.com/html2text"
)

func main() {
    htmlContent := `
    
        
            

Check out our product catalog or visit our support page.

` text, err := html2text.FromString(htmlContent) if err != nil { log.Fatal(err) } fmt.Println(text) }