Reading and Writing PDF Files in Go
Working with PDF files in Go is an essential skill when dealing with document processing. In this guide, we'll cover how to read and write PDF files using the Go programming language, leveraging popular packages like `pdfcpu` and `gopdf`.
Setting Up Your Environment
Before we dive into the code, ensure you have Go installed on your system. You can download it from the official Go website. After installing Go, you'll need to set up your workspace and ensure that your GOPATH is correctly configured. You can verify your Go installation by running:
go versionNext, initialize a Go module in your project directory:
go mod init pdf_projectReading PDF Files with pdfcpu
The `pdfcpu` library is a widely used tool for handling PDF files in Go. Start by installing pdfcpu:
go get github.com/pdfcpu/pdfcpuOnce installed, you can read a PDF file with the following code:
package main
import (
"fmt"
"github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
)
func readPDF(filePath string) {
ctx, err := api.ReadContextFile(filePath, pdfcpu.NewDefaultConfiguration())
if err != nil {
fmt.Println("Error reading PDF:", err)
return
}
for i := 1; i <= ctx.PageCount; i++ {
page, err := ctx.ExtractPageText(i)
if err != nil {
fmt.Printf("Error extracting text from page %d: %s\n", i, err)
} else {
fmt.Printf("Page %d:\n%s\n", i, page)
}
}
}
func main() {
readPDF("sample.pdf")
}
This code will iterate through each page of the provided PDF and print the extracted text to the console.
Writing PDF Files with gopdf
Now let's write a simple PDF file using the `gopdf` library. Start by installing gopdf:
go get github.com/signintech/gopdfHere's how you can create a PDF with a single page of text:
package main
import (
"fmt"
"github.com/signintech/gopdf"
)
func createPDF(outputFile string) {
var pdf gopdf.GoPdf
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4})
pdf.AddPage()
err := pdf.AddTTFFont("example", "path-to-font.ttf")
if err != nil {
fmt.Println("Error adding font:", err)
return
}
err = pdf.SetFont("example", "", 14)
if err != nil {
fmt.Println("Error setting font:", err)
return
}
pdf.Cell(nil, "Hello, PDF world!")
pdf.WritePdf(outputFile)
}
func main() {
createPDF("output.pdf")
fmt.Println("PDF created successfully!")
}
This snippet initializes a PDF document, adds a font, writes text, and finally creates the PDF file. You need to ensure you have a TTF font file to use with `gopdf`.
Conclusion
Using Go, libraries such as `pdfcpu` and `gopdf` provide powerful tools for reading from and writing to PDF files. This guide introduced basic functionality. For more advanced manipulation, refer to the respective documentation of each library. Happy coding!