Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

github.com/cloudresty/pdf

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/cloudresty/pdf

  • v1.0.0
  • Source
  • Go
  • Socket score

Version published
Created
Source

PDF Reader

In a nutshell PDF Reader it is a simple Go library for reading PDF files which enables text exctraction being in the form of Plain Text or Formatted Text. The very first developer of this library it was https://github.com/rsc/pdf, being forked and improved by https://github.com/ledongthuc/pdf. Cloudresty has forked ledongthuc's library with the aim to maintain and improve it further.

  Features

  • Get plain text content (without format)
  • Get content (including all font and formatting information)

 

Install Read PDF Go library

go get -u github.com/cloudresty/pdf

 

Read PDF Go Library Examples

Go Read PDF - Plain Text
package main

import (
	"bytes"
	"fmt"

	"github.com/cloudresty/pdf"
)

//
// Main Function
//

func main() {

	pdf.DebugOn = true

	//
	// Read PDF File
	//

	pdfText, errReadPDF := readPDF("file.pdf")

	if errReadPDF != nil {
		panic(errReadPDF)
	}

	fmt.Println(pdfText)
	
	return

}

//
// Read PDF Function
//

func readPDF(path string) (string, error) {

	//
	// Open PDF File
	//

	f, r, errPDFOpen := pdf.Open(path)
	
    defer f.Close()

	if errPDFOpen != nil {
		return "", errPDFOpen
	}

	//
	// Extract Plain Text
	//

	var buf bytes.Buffer

    b, errGetPlainText := r.GetPlainText()

	if errGetPlainText != nil {
        return "", errGetPlainText
    }

	buf.ReadFrom(b)

	return buf.String(), nil

}
Go Read PDF - Formatted Text
//
// Read PDF Function (standalone)
//

func readPDF(path string) (string, error) {

	//
	// Open PDF File
	//

	f, r, errPDFOpen := pdf.Open(path)

	defer f.Close()

	if errPDFOpen != nil {
		return "", errPDFOpen
	}

	//
	// Page Count
	//
	
	totalPage := r.NumPage()

	//
	// Loop Through Pages
	//

	for pageIndex := 1; pageIndex <= totalPage; pageIndex++ {

		p := r.Page(pageIndex)

		if p.V.IsNull() {
			continue
		}

		//
		// Extract Formatted Text
		//

		var lastTextStyle pdf.Text
		texts := p.Content().Text

		for _, text := range texts {

			if isSameSentence(text, lastTextStyle) {

				lastTextStyle.S = lastTextStyle.S + text.S
		
			} else {

				fmt.Printf("Font: %s, Font-size: %f, x: %f, y: %f, content: %s \n", lastTextStyle.Font, lastTextStyle.FontSize, lastTextStyle.X, lastTextStyle.Y, lastTextStyle.S)
				lastTextStyle = text

			}

		}

	}

	return "", nil

}
Go Read PDF - Text Grouped by Rows
package main

import (
	"fmt"
	"os"

	"github.com/cloudresty/pdf"
)

//
// Main Function
//

func main() {

	//
	// Read Local PDF File
	//

	content, errReadPDF := readPDF(os.Args[1])

	if errReadPDF != nil {
		panic(errReadPDF)
	}

	fmt.Println(content)

	return

}

//
// Read PDF Function
//

func readPDF(path string) (string, error) {

	//
	// Open PDF File
	//

	f, r, errReadPDF := pdf.Open(path)

	defer func() {
		_ = f.Close()
	}()

	if errReadPDF != nil {
		return "", errReadPDF
	}

	//
	// Page Count
	//

	totalPage := r.NumPage()

	//
	// Loop Through Pages
	//

	for pageIndex := 1; pageIndex <= totalPage; pageIndex++ {

		p := r.Page(pageIndex)

		if p.V.IsNull() {
			continue
		}

		//
		// Get Rows
		//

		rows, _ := p.GetTextByRow()

		for _, row := range rows {

			println("Row: ", row.Position)

			for _, word := range row.Content {

				fmt.Println(word.S)

			}

		}

	}

	return "", nil
}

FAQs

Package last updated on 05 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc