Giter VIP home page Giter VIP logo

gotenberg-go-client-fork's Introduction

Gotenberg Go Client

๐Ÿ”ฅ 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.

Install

$ go get -u github.com/dcaraxes/gotenberg-go-client/v8

Usage

First steps

Create the client

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}
}

Prepare files

// 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>"))

Generating PDF from 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)

Read and write EXIF metadata

Reading metadata available only for PDF files, but you can write metadata to all Gotenberg supporting files.

Write

// 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)

Read

// 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

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.

Badges

GoDoc Go Report Card

gotenberg-go-client-fork's People

Contributors

gulien avatar dcaraxes avatar chetanyakan avatar csandman avatar ungerik avatar mdjward avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.