This page describes how to convert PDF to text in Golang using the cloud-based Pdfcrowd API. The API is easy to use and it takes only a few of lines of code to integrate it to your application.
The Golang API client library provides easy access to the Pdfcrowd API. No third-party libraries are required.
Install the client library from Githubgo get github.com/pdfcrowd/pdfcrowd-go
We also offer other installation options.
The credentials to access the API are your Pdfcrowd username and the API key. You can try out the API without registering using the following demo credentials:
demo
ce544b6ea52a5621fb9d55f8b542d14d
To get your personal API credentials, you can start a free API trial or buy the API license.
Refer to the PDF to Text Golang Reference for a description of all API methods.
Here are a few Golang examples to get you started quickly with the API. See more examples.
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // create the API client instance client := pdfcrowd.NewPdfToTextClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // run the conversion and write the result to a file err := client.ConvertFileToFile("/path/to/invoice.pdf", "invoice.txt") // check for the conversion error handleError(err) } func handleError(err error) { if err != nil { // report the error why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } // rethrow or handle the exception panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { // create the API client instance client := pdfcrowd.NewPdfToTextClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // run the conversion and write the result to a file err := client.ConvertUrlToFile("https://pdfcrowd.com/static/pdf/apisamples/invoice.pdf", "invoice.txt") // check for the conversion error handleError(err) } func handleError(err error) { if err != nil { // report the error why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } // rethrow or handle the exception panic(err.Error()) } }
package main import ( "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" "io/ioutil" ) func readFile(fileName string) []byte { content, err := ioutil.ReadFile(fileName) handleError(err) return content } func main() { // create the API client instance client := pdfcrowd.NewPdfToTextClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d") // run the conversion and write the result to a file err := client.ConvertRawDataToFile(readFile("/path/to/hello_world.pdf"), "invoice.txt") // check for the conversion error handleError(err) } func handleError(err error) { if err != nil { // report the error why, ok := err.(pdfcrowd.Error) if ok { os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why)) } else { os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err)) } // rethrow or handle the exception panic(err.Error()) } }