Create & Manage PDF Documents via Free Go API
Go Library that gives software developers the ability to create, edit, convert & manipulate PDF Documents.
What is gofpdf?
The gofpdf is an open Source Go library that gives software developers the capability to generate and modify PDF files without any external dependencies. The library supports PDF document generation and manipulation with a high level of text, drawings, and images. The library fully supports UTF-8 TrueType fonts and “right-to-left” languages. It supports automatic translation of UTF-8 runes to code page encodings for languages that have fewer than 256 glyphs.
PDF is one of the World’s most used file formats for storing and sharing information across the globe. The gofpdf library supports several important features for PDF document processing, such as generating and editing PDF documents, inserting an image (JPEG, PNG, GIF, TIFF, and SVG images) to PDF, Inserting new pages in the existing PDF file, Page header and footer management, automatic page breaks, Internal and external links support, line breaks and text justification, and many more.
Getting Started with gofpdf
The recommended way to install gofpdf is by using GitHub. To install the gofpdf on your system, please run the following command
Install gofpdf via GitHub
go get https://github.com/jung-kurt/gofpdf
For latest update please run the following command:
go get -u -v github.com/jung-kurt/gofpdf/...
PDF Creation & Processing via Go Library
gofpdf library provides functionality for PDF document creation as well as editing inside Go applications. It allows to create PDF documents with a high level of support for text, drawing, and images. After the document creation, you can easily include text, and images, insert new pages, Layers, Templates, Barcodes, etc. with just a couple of simple commands. You can also generate a PDF document with multiple columns or by Landscape mode with images
How to Generate Simple PDF File via Go API?
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "B", 16)
pdf.Cell(40, 10, "Hello, world")
err := pdf.OutputFileAndClose("hello.pdf")
Images addition to PDF via Go API
Images are very important part of presenting more information in a better and more comprehensive way. It always adds more value to the piece of content. The open source gofpdf API allows programmers to insert images of their own choice into PDF files inside their applications. It provides support for very popular image formats such as JPEG, PNG, GIF, TIFF, and basic path-only SVG images. You can also modify images according to your own needs.
How to Add Images to PDF Pages via Go API?
func ExampleFpdf_Image() {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "", 11)
pdf.Image(example.ImageFile("logo.png"), 10, 10, 30, 0, false, "", 0, "")
pdf.Text(50, 20, "logo.png")
pdf.Image(example.ImageFile("logo.gif"), 10, 40, 30, 0, false, "", 0, "")
pdf.Text(50, 50, "logo.gif")
pdf.Image(example.ImageFile("logo-gray.png"), 10, 70, 30, 0, false, "", 0, "")
pdf.Text(50, 80, "logo-gray.png")
pdf.Image(example.ImageFile("logo-rgb.png"), 10, 100, 30, 0, false, "", 0, "")
pdf.Text(50, 110, "logo-rgb.png")
pdf.Image(example.ImageFile("logo.jpg"), 10, 130, 30, 0, false, "", 0, "")
pdf.Text(50, 140, "logo.jpg")
fileStr := example.Filename("Fpdf_Image")
err := pdf.OutputFileAndClose(fileStr)
example.Summary(err, fileStr)
// Output:
// Successfully generated pdf/Fpdf_Image.pdf
}
Adding Layers to PDF Documents
The use of layers in PDF documents gives you the capability to arrange and display content in a better way. The feature enables users to make the content visible or invisible or add details to diagrams in PDF documents. The gofpdf library fully supports layers addition and management inside PDF files. You can easily define layers, control the visibility of a layer interactively or open a layer pane in PDF viewer.
How to View and Add Layers to PDF File inside Go Apps?
func ExampleFpdf_AddLayer() {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.AddPage()
pdf.SetFont("Arial", "", 15)
pdf.Write(8, "This line doesn't belong to any layer.\n")
// Define layers
l1 := pdf.AddLayer("Layer 1", true)
l2 := pdf.AddLayer("Layer 2", true)
// Open layer pane in PDF viewer
pdf.OpenLayerPane()
// First layer
pdf.BeginLayer(l1)
pdf.Write(8, "This line belongs to layer 1.\n")
pdf.EndLayer()
// Second layer
pdf.BeginLayer(l2)
pdf.Write(8, "This line belongs to layer 2.\n")
pdf.EndLayer()
// First layer again
pdf.BeginLayer(l1)
pdf.Write(8, "This line belongs to layer 1 again.\n")
pdf.EndLayer()
fileStr := example.Filename("Fpdf_AddLayer")
err := pdf.OutputFileAndClose(fileStr)
example.Summary(err, fileStr)
// Output:
// Successfully generated pdf/Fpdf_AddLayer.pdf
}
Add Headers, Footers & Page Breaks to PDFs
documents inside their Go apps. The headers and footers feature gives every PDF document a personal touch by applying page numbers, text labels, or images to PDF files. The library fully supports creating, editing, and managing headers and footers inside your PDF documents. You can also use features like line justification, word-wrapping and page breaks, etc.
How to Perform Headers & Footers Addition to PDF via Go API?
func ExampleFpdf_AddPage() {
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.SetTopMargin(30)
pdf.SetHeaderFuncMode(func() {
pdf.Image(example.ImageFile("logo.png"), 10, 6, 30, 0, false, "", 0, "")
pdf.SetY(5)
pdf.SetFont("Arial", "B", 15)
pdf.Cell(80, 0, "")
pdf.CellFormat(30, 10, "Title", "1", 0, "C", false, 0, "")
pdf.Ln(20)
}, true)
pdf.SetFooterFunc(func() {
pdf.SetY(-15)
pdf.SetFont("Arial", "I", 8)
pdf.CellFormat(0, 10, fmt.Sprintf("Page %d/{nb}", pdf.PageNo()),
"", 0, "C", false, 0, "")
})
pdf.AliasNbPages("")
pdf.AddPage()
pdf.SetFont("Times", "", 12)
for j := 1; j <= 40; j++ {
pdf.CellFormat(0, 10, fmt.Sprintf("Printing line number %d", j),
"", 1, "", false, 0, "")
}
fileStr := example.Filename("Fpdf_AddPage")
err := pdf.OutputFileAndClose(fileStr)
example.Summary(err, fileStr)
// Output:
// Successfully generated pdf/Fpdf_AddPage.pdf
}