Go API for Creating & Processing PDF Document
Go Library that gives software developers the ability to Import, Modify, Protect, Manipulate & Save PDF Documents.
What is gopdf?
The gopdf is an open Source Go library that has included functionality for working with PDF documents inside Go applications. The library helps developers to generate and modify PDF files with just a couple of lines of Go code. The library has included embedding of Unicode sub-font for popular languages like Chinese, Japanese, Korean, etc.
The library is very stable and very easy to use. It has included several important features related to PDF creation and management such as print text, add and replace images, use links inside PDF, add a page to PDF, Draw a line, oval, or polygon to the PDF page, image or text rotation, set transparency for text, Font kerning, generate password-protected PDF files and much more.
.
Getting Started with gopdf
The recommended way to install gopdf is by using GitHub. To install the gopdf on your system, please run the following command
Install gopdf via GitHub
go get https://github.com/signintech/gopdf.git
For the latest update please run the following command.
Generate PDF Files via Go Library
The open source gopdf library gives software developers the capability to create and modify PDF documents inside Go applications. PDF is one of the leading file formats that is widely used in the educational, legal, medical, small businesses, and IT industries. The PDF created with the library has several advantages over other leading file formats such as ease of creation, better security, portability, universal compatibility, reliability, and much more.
Inserting Images to PDF via Go
The gopdf library has included functionality for inserting images inside their PDF documents inside their own applications. You can also set the width, height, and place of your choice inside a PDF page. Once inserted, you can easily modify and replace it with any other supported image type using a couple of lines of GO code. You can use some of the popular image formats such as JPEG, PNG, GIF, TIFF, and many more
How to Load Image to PDF Pages via Go Library?
package main
import (
"log"
"github.com/signintech/gopdf"
)
func main() {
pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{PageSize: *gopdf.PageSizeA4 })
pdf.AddPage()
var err error
err = pdf.AddTTFFont("loma", "../ttf/Loma.ttf")
if err != nil {
log.Print(err.Error())
return
}
pdf.Image("../imgs/gopher.jpg", 200, 50, nil) //print image
err = pdf.SetFont("loma", "", 14)
if err != nil {
log.Print(err.Error())
return
}
pdf.SetXY(250, 200) //move current location
pdf.Cell(nil, "gopher and gopher") //print text
pdf.WritePdf("image.pdf")
}
Import & Modify Existing PDF
The open source gopdf library enables software programmers to import an existing PDF document and make changes to it with ease using Go command. The DownloadFile function will download a file via URL to a local file. It is very useful because it will not load the whole file and only a specific page into the memory. Once you make the changes and are satisfied you can save it again with ease.
Password Protected PDF Files Creation using Go Library
The open source gofpdf library enables software programmers to programmatically safeguard their PDF documents by applying password protection to it. It has provided complete support for creating PDF files with custom password or can set their own password. Users can select from the set of various available features such as allowing PDF file reading, permission for opening a PDF, content modification of PDF file and so on.
How to Load Image to PDF Pages via Go API?
package main
import (
"log"
"github.com/signintech/gopdf"
)
func main() {
pdf := gopdf.GoPdf{}
pdf.Start(gopdf.Config{
PageSize: *gopdf.PageSizeA4, //595.28, 841.89 = A4
Protection: gopdf.PDFProtectionConfig{
UseProtection: true,
Permissions: gopdf.PermissionsPrint | gopdf.PermissionsCopy | gopdf.PermissionsModify,
OwnerPass: []byte("123456"),
UserPass: []byte("123456789")},
})
pdf.AddPage()
pdf.AddTTFFont("loma", "../ttf/loma.ttf")
pdf.Cell(nil,"Hi")
pdf.WritePdf("protect.pdf")
}