Generating PDF documents from Go has become an essential task for many developers, especially when creating invoices, reports, or any document where visual presentation matters. Luckily, with the wrap-around utility of go-wkhtmltopdf, the process is as easy as pie. In this article, we’ll dive into how to use this handy tool and troubleshoot any hiccups along the way.
What is go-wkhtmltopdf?
go-wkhtmltopdf is a Golang command-line wrapper for the wkhtmltopdf tool. It allows users to generate PDF documents using HTML and CSS templates, ensuring highly customizable layouts. The beauty of this package lies in its simplicity, type safety, and integration capabilities.
Installation
Before you can start using go-wkhtmltopdf, you need to install it. This can be done using:
- Go get:
go get -u github.com/SebastiaanKlippert/go-wkhtmltopdf - Alternatively, use any Go dependency manager you prefer.
Make sure that the wkhtmltopdf executable is available in your system. The package will look for it in the current directory, PATH, or via the WKHTMLTOPDF_PATH environment variable.
Basic Usage
Let’s go through a simple example to understand how to use go-wkhtmltopdf effectively. This package makes it feel like you’re assembling a jigsaw puzzle, where each function serves as a piece fitting perfectly into the larger picture of generating PDFs.
package main
import (
"fmt"
"log"
"github.com/SebastiaanKlippert/go-wkhtmltopdf"
)
func main() {
// Create new PDF generator
pdfg, err := wkhtmltopdf.NewPDFGenerator()
if err != nil {
log.Fatal(err)
}
// Set global options
pdfg.Dpi.Set(300)
pdfg.Orientation.Set(wkhtmltopdf.OrientationLandscape)
pdfg.Grayscale.Set(true)
// Create a new input page from an URL
page := wkhtmltopdf.NewPage("https://example.com")
page.FooterRight.Set("[page]")
page.FooterFontSize.Set(10)
page.Zoom.Set(0.95)
// Add to document
pdfg.AddPage(page)
// Create PDF document in internal buffer
err = pdfg.Create()
if err != nil {
log.Fatal(err)
}
// Write buffer contents to file on disk
err = pdfg.WriteFile("example.pdf")
if err != nil {
log.Fatal(err)
}
fmt.Println("Done")
}
In this example, imagine creating a custom infographic. You start by laying out the elements you want: a clean background (initializing the PDF generator), followed by striking colors and shapes (setting options) to create pages (adding content) that finally transform into a beautiful artwork (the resulting PDF).
Troubleshooting
As with any tool, there may be challenges you encounter along the way. Here are some common issues and solutions:
- Issue: The wkhtmltopdf executable is not found.
- Solution: Ensure that wkhtmltopdf is installed correctly on your system, and its path is included in your system’s PATH or set the
WKHTMLTOPDF_PATHenvironment variable. - Issue: Generated PDFs are blank or not rendering correctly.
- Solution: Double-check the URLs you’re using for pages. Make sure they’re accessible and that your HTML/CSS meet the required standards.
- Issue: Performance issues with large documents.
- Solution: Consider breaking down large documents into smaller components or utilizing asynchronous processing if applicable.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Advanced Features
For advanced users, the package also allows:
- Saving and loading PDF generator states in JSON format.
- Running the PDF generator in server-type applications with multiple instances.
- Using
PageReaderto load HTML from different sources (memory, files, etc.).
Conclusion
In conclusion, go-wkhtmltopdf is a powerful tool for creating PDFs in Go using HTML and CSS, flourishing with customization and efficiency. Whether you’re crafting reports, invoices, or any illustrative documents, it’s a valuable addition to your toolkit.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

