docconv

repository·master·Indexed 23 days ago

https://github.com/sajari/docconv

A Go wrapper library and toolset for converting various document formats (PDF, DOC, DOCX, XML, HTML, RTF, ODT, Pages) and images into plain text. It includes the docconv Go package for local conversion, a client for network-based conversion, and the docd tool which can operate as an HTTP service, a Docker container, or a command-line utility. Supports OCR for images via the ocr build tag and provides HTML readability extraction options.

Tokens
6.2K
Snippets
12
Records
54
Agent score
83%

What's inside docconv

  1. Enable OCR and image support in docconv

    master

    To add image support (OCR) to the docconv library, follow these steps:

    1. Install and build gosseract.
    2. If on macOS, install tesseract via brew: brew install tesseract.
    3. Fetch/build docconv using the ocr build tag:
    $ go get -tags ocr code.sajari.com/docconv/v2/...
    $ go get -tags ocr code.sajari.com/docconv/v2/...
  2. Use the docd tool

    master

    The docd tool can be used in three different modes:

    1. As an HTTP service: Runs on port 8888 by default. You can send documents via multipart POST requests to receive plain text and metadata as a JSON object.
    2. As a Docker container: Official images are available at https://hub.docker.com/r/sajari/docd. You can build it locally:
      $ cd docd
      $ docker build -t docd .
    3. Via Command Line: Pass a document as an argument:
      $ docd -input document.pdf
    $ docd -input document.pdf
  3. Install system dependencies for docconv

    master

    The docconv library requires several system-level dependencies to handle different file formats. Install them based on your operating system:

    Debian-based Linux

    $ sudo apt-get install poppler-utils wv unrtf tidy
    $ go get github.com/JalfResi/justext

    macOS

    $ brew install poppler-qt5 wv unrtf tidy-html5
    $ go get github.com/JalfResi/justext
    $ sudo apt-get install poppler-utils wv unrtf tidy
    $ go get github.com/JalfResi/justext
  4. Install the docd tool

    master

    To install the docd executable, use the go install command. Ensure that the resulting executable's location is included in your PATH environment variable.

    $ go install code.sajari.com/docconv/v2/docd@latest
    $ go install code.sajari.com/docconv/v2/docd@latest
  5. Run the docd conversion service

    master

    By default, docd starts an HTTP server that listens for conversion requests. The service exposes a /convert endpoint.

    To start the server on a specific address, use the -addr flag:

    docd -addr :8080

    Once running, you can request a conversion via HTTP (e.g., using curl):

    curl http://localhost:8080/convert?url=https://example.com
  6. Install the modified Snappy-Go library from source

    master

    To install this specific version of the Snappy library for Go, use the go get command.

    WARNING: This version is a modified 'hack' specifically designed to handle Apple files that fail to set CRC checks and stream identifiers. If you are working on any other project that does not require this specific Apple-file compatibility, DO NOT USE THIS VERSION. Use the standard code.google.com/p/snappy-go/snappy instead.

    $ go get code.google.com/p/snappy-go/snappy
  7. Convert a file locally using the docconv Go library

    master

    If you have the system dependencies installed, you can use the docconv package directly in your Go code to convert a file path to text.

    package main
    
    import (
    	"fmt"
    
    	"code.sajari.com/docconv/v2"
    )
    
    func main() {
    	res, err := docconv.ConvertPath("your-file.pdf")
    	if err != nil {
    		// TODO: handle
    	}
    	fmt.Println(res)
    }
  8. Convert a file over the network using the docconv client

    master

    You can use the client package to interact with a running docd service (defaulting to localhost:8888).

    package main
    
    import (
    	"fmt"
    
    	"code.sajari.com/docconv/v2/client"
    )
    
    func main() {
    	// Create a new client, using the default endpoint (localhost:8888)
    	c := client.New()
    
    	res, err := client.ConvertPath(c, "your-file.pdf")
    	if err != nil {
    		// TODO: handle
    	}
    	fmt.Println(res)
    }

    Alternatively, you can use curl to send a multipart POST request:

    $ curl -s -F input=@your-file.pdf http://localhost:8888/convert
  9. Configure HTML Readability options in docd

    master

    The docd service allows fine-tuning of the HTML readability extraction logic via CLI flags. These flags map to docconv.HTMLReadabilityOptions:

    • readability-length-low / readability-length-high: Controls the length constraints for readability.
    • readability-stopwords-low / readability-stopwords-high: Controls the stopwords density thresholds.
    • readability-max-link-density: Sets the maximum allowed link density.
    • readability-max-heading-distance: Sets the maximum distance between headings.
    • readability-use-classes: A comma-separated list of CSS classes used to identify readable content (e.g., "good,neargood").
  10. Configure HTML readability options

    master

    The HTMLReadability function is configured via the global variable HTMLReadabilityOptionsValues of type HTMLReadabilityOptions.

    Note: These settings are global and affect all subsequent calls to HTMLReadability or ConvertHTML (when readability is true).

    type HTMLReadabilityOptions struct {
    	LengthLow             int
    	LengthHigh            int
    	StopwordsLow          float64
    	StopwordsHigh         float64
    	MaxLinkDensity        float64
    	MaxHeadingDistance    int
    	ReadabilityUseClasses string
    }
    
    // Set global options
    docconv.HTMLReadabilityOptionsValues = docconv.HTMLReadabilityOptions{
    	LengthLow:      20,
    	LengthHigh:     100,
    	MaxLinkDensity: 0.5,
    }
  11. Reference: docd CLI flags

    master

    The following flags are available for the docd tool:

    FlagDescription
    addrThe bind address for the HTTP server (default: ":8888")
    readability-length-lowSets the readability length low if the ?readability=1 parameter is set
    readability-length-highSets the readability length high if the ?readability=1 parameter is set
    readability-stopwords-lowSets the readability stopwords low if the ?readability=1 parameter is set
    readability-stopwords-highSets the readability stopwords high if the ?readability=1 parameter is set
    readability-max-link-densitySets the readability max link density if the ?readability=1 parameter is set
    readability-max-heading-distanceSets the readability max heading distance if the ?readability=1 parameter is set
    readability-use-classesComma separated list of readability classes to use if the ?readability=1 parameter is set