Implements wkhtmltopdf Go bindings. It can be used to convert HTML documents to PDF files.
The package does not use the wkhtmltopdf
binary. Instead, it uses the wkhtmltox
library directly.
Full documentation can be found at https://pkg.go.dev/github.com/adrg/go-wkhtmltopdf.
Examples
- Basic usage
- Converter callbacks
- Convert HTML document based on JSON input
- Basic web page to PDF conversion server
- Configurable web page to PDF conversion server
In order to use the package, wkhtmltox
must be installed. Installation packages
for multiple operating systems can be found at https://builds.wkhtmltopdf.org.
Please see the wiki pages of this project for detailed installation instructions.
Note:
wkhtmltox
does not seem to be actively maintained. Please see the project status for more information, recommendations and future plans.
go get github.com/adrg/go-wkhtmltopdf
package main
import (
"log"
"os"
pdf "github.com/adrg/go-wkhtmltopdf"
)
func main() {
// Initialize library.
if err := pdf.Init(); err != nil {
log.Fatal(err)
}
defer pdf.Destroy()
// Create object from file.
object, err := pdf.NewObject("sample1.html")
if err != nil {
log.Fatal(err)
}
object.Header.ContentCenter = "[title]"
object.Header.DisplaySeparator = true
// Create object from URL.
object2, err := pdf.NewObject("https://google.com")
if err != nil {
log.Fatal(err)
}
object2.Footer.ContentLeft = "[date]"
object2.Footer.ContentCenter = "Sample footer information"
object2.Footer.ContentRight = "[page]"
object2.Footer.DisplaySeparator = true
// Create object from reader.
inFile, err := os.Open("sample2.html")
if err != nil {
log.Fatal(err)
}
defer inFile.Close()
object3, err := pdf.NewObjectFromReader(inFile)
if err != nil {
log.Fatal(err)
}
object3.Zoom = 1.5
object3.TOC.Title = "Table of Contents"
// Create converter.
converter, err := pdf.NewConverter()
if err != nil {
log.Fatal(err)
}
defer converter.Destroy()
// Add created objects to the converter.
converter.Add(object)
converter.Add(object2)
converter.Add(object3)
// Set converter options.
converter.Title = "Sample document"
converter.PaperSize = pdf.A4
converter.Orientation = pdf.Landscape
converter.MarginTop = "1cm"
converter.MarginBottom = "1cm"
converter.MarginLeft = "10mm"
converter.MarginRight = "10mm"
// Convert objects and save the output PDF document.
outFile, err := os.Create("out.pdf")
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
// Run converter.
if err := converter.Run(outFile); err != nil {
log.Fatal(err)
}
}
Contributions in the form of pull requests, issues or just general feedback,
are always welcome.
See CONTRIBUTING.MD.
Contributors: adrg, leandrosilva.
For more information see the wkhtmltopdf documentation and the wkhtmltox documentation.
Copyright (c) 2016 Adrian-George Bostan.
This project is licensed under the MIT license. See LICENSE for more details.