This page describes how to use the Pdfcrowd online API to convert PDF to text
in Golang.
The API is user-friendly and can be integrated into your application with just a few lines of code.
Here are Golang examples for quickly getting started with the API.
See more examples.
packagemainimport("os""fmt""github.com/pdfcrowd/pdfcrowd-go")funcmain(){// create the API client instanceclient:=pdfcrowd.NewPdfToTextClient("demo","ce544b6ea52a5621fb9d55f8b542d14d")// run the conversion and write the result to a fileerr:=client.ConvertFileToFile("/path/to/invoice.pdf","invoice.txt")// check for the conversion errorhandleError(err)}funchandleError(errerror){iferr!=nil{why,ok:=err.(pdfcrowd.Error)ifok{os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n",why))}else{os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n",err))}panic(err.Error())}}
packagemainimport("os""fmt""github.com/pdfcrowd/pdfcrowd-go")funcmain(){// create the API client instanceclient:=pdfcrowd.NewPdfToTextClient("demo","ce544b6ea52a5621fb9d55f8b542d14d")// run the conversion and write the result to a fileerr:=client.ConvertUrlToFile("https://pdfcrowd.com/static/pdf/apisamples/invoice.pdf","invoice.txt")// check for the conversion errorhandleError(err)}funchandleError(errerror){iferr!=nil{why,ok:=err.(pdfcrowd.Error)ifok{os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n",why))}else{os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n",err))}panic(err.Error())}}
packagemainimport("os""fmt""github.com/pdfcrowd/pdfcrowd-go""io/ioutil")funcreadFile(fileNamestring)[]byte{content,err:=ioutil.ReadFile(fileName)handleError(err)returncontent}funcmain(){// create the API client instanceclient:=pdfcrowd.NewPdfToTextClient("demo","ce544b6ea52a5621fb9d55f8b542d14d")// run the conversion and write the result to a fileerr:=client.ConvertRawDataToFile(readFile("/path/to/hello_world.pdf"),"invoice.txt")// check for the conversion errorhandleError(err)}funchandleError(errerror){iferr!=nil{why,ok:=err.(pdfcrowd.Error)ifok{os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n",why))}else{os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n",err))}panic(err.Error())}}
Authentication
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:
It is recommended that you implement error handling to catch errors
that the API may return, see the example code below. A list of status
codes and their description can be found
here.
// call the API // print the erroros.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n",why))// print the error codeos.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error Code: %v\n",why.getCode()))// print the error messageos.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error Message: %v\n",why.getMessage()))