Sling Academy
Home/Golang/How to read and write PDF files using Go

How to read and write PDF files using Go

Last updated: November 27, 2024

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 version

Next, initialize a Go module in your project directory:

go mod init pdf_project

Reading 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/pdfcpu

Once 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/gopdf

Here'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!

Next Article: How to send emails using Go

Previous Article: How to download a large file from the internet using Go

Series: Networking and Server

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant