🔥 Supports Gotenberg version 8 and higher! 🔥
A simple Go client for interacting with a Gotenberg API. This project is a further development of the client github.com/thecodingmachine/gotenberg-go-client, which does not support the functionality of version 8.
$ go get -u github.com/dcaraxes/gotenberg-go-client/v8
package main
import (
"net/http"
"time"
"github.com/dcaraxes/gotenberg-go-client/v8"
)
func main() {
// Create the HTTP-client.
httpClient := &http.Client{
Timeout: 5*time.Second,
}
// Create the Gotenberg client
client := &gotenberg.Client{Hostname: "http://localhost:3000", HTTPClient: httpClient}
}
// From a path.
pdf, _ := gotenberg.NewDocumentFromPath("data.pdf", "/path/to/file")
// From a string.
index, _ := gotenberg.NewDocumentFromString("index.html", "<html>Foo</html>")
// From a bytes.
index, _ := gotenberg.NewDocumentFromBytes("index.html", []byte("<html>Foo</html>"))
// Creates the Gotenberg documents from a files paths.
index, _ := gotenberg.NewDocumentFromPath("index.html", "/path/to/file")
header, _ := gotenberg.NewDocumentFromPath("header.html", "/path/to/file")
footer, _ := gotenberg.NewDocumentFromPath("footer.html", "/path/to/file")
style, _ := gotenberg.NewDocumentFromPath("style.css", "/path/to/file")
img, _ := gotenberg.NewDocumentFromPath("img.png", "/path/to/file")
// Create the HTML request.
req := gotenberg.NewHTMLRequest(index)
// Setting up basic auth (if needed).
req.SetBasicAuth("your_username", "your_password")
// Set the document parameters.
req.Header(header)
req.Footer(footer)
req.Assets(style, img)
req.Margins(gotenberg.NoMargins)
req.Scale(0.75)
req.PaperSize(gotenberg.A4)
// Optional, you can change paper and margins size unit. For example:
paperSize := gotenberg.PaperDimensions{
Height: 17,
Width: 11,
// IN - inches. Other available units are PT (Points), PX (Pixels),
// MM (Millimeters), CM (Centimeters), PC (Picas).
Unit: gotenberg.IN,
}
req.PaperSize(paperSize)
// Skips the IDLE events for faster PDF conversion.
req.SkipNetworkIdleEvent()
// Store method allows you to store the resulting PDF in a particular destination.
client.Store(req, "path/you/want/the/pdf/to/be/stored.pdf")
// If you wish to redirect the response directly to the browser, you may also use:
resp, _ := client.Post(req)
Reading metadata available only for PDF files, but you can write metadata to all Gotenberg supporting files.
// Prepare the files required for your conversion.
pdfFile, err := NewDocumentFromPath("gotenberg1.pdf", test.PDFTestFilePath(t, "gotenberg.pdf"))
req := gotenberg.NewWriteMetadataRequest(pdfFile)
req.SetBasicAuth("your_username", "your_password")
// Sets result file name.
req.ResultFilename("foo.pdf")
writeDataStruct := struct {
Author string `json:"Author"`
Copyright string `json:"Copyright"`
}{
Author: "Author name",
Copyright: "Copyright",
}
jsonMetadata, _ := json.Marshal(writeDataStruct)
req.Metadata(jsonMetadata)
_ = client.Store(req, "path/you/want/the/pdf/to/be/stored.pdf")
resp, _ := client.Post(req)
// Prepare the files required for your conversion.
pdfFile, err := NewDocumentFromPath("gotenberg1.pdf", test.PDFTestFilePath(t, "gotenberg.pdf"))
req := gotenberg.NewReadMetadataRequest(pdfFile)
req.SetBasicAuth("your_username", "your_password")
// Sets result filename
req.ResultFilename("foo.pdf")
// This response body contains JSON-formatted EXIF metadata.
respRead, _ := client.Post(req)
var readData = struct {
FooPdf struct {
Author string `json:"Author"`
Copyright string `json:"Copyright"`
} `json:"foo.pdf"`
}
// Marshal metadata into a struct.
_ = json.NewDecoder(respRead.Body).Decode(&readData)
Making screenshots only available for HTML, URL and Markdown requests.
index, _ := gotenberg.NewDocumentFromPath("index.html", "/path/to/file")
// Create the HTML request.
req := gotenberg.NewHTMLRequest(index)
req.SetBasicAuth("your_username", "your_password")
// Set image format.
req.Format(gotenberg.JPEG) // PNG, JPEG and WebP available now
// Store to path.
client.StoreScreenshot(req, "path/you/want/the/pdf/to/be/stored.jpeg")
// Or get response directly.
resp, _ := client.Screenshot(req)
For more complete usages, head to the documentation.