Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental #16

Merged
merged 37 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
7245a91
feat: add ci workflow
Oct 25, 2024
cd12f1d
feat: add beta branch to ci pr triggers
Oct 25, 2024
0382e50
fix: remove interactive flag from docker run
Oct 25, 2024
d6656a1
fix: remove webgook server running in tests
Oct 25, 2024
758c62d
feat: disable gofumpt linter & remove webhook tests
Oct 25, 2024
8a8deb1
feat: disable goimports linter
Oct 25, 2024
bdd7643
fix: linter issues
Oct 25, 2024
af9529f
fix: readme updates
Oct 25, 2024
bfea675
fix: readme updates
Oct 25, 2024
59abcea
Update README.md
starwalkn Oct 25, 2024
89155b9
Update README.md
starwalkn Oct 25, 2024
9e70be0
Update README.md
starwalkn Oct 25, 2024
206daf0
Update README.md
starwalkn Oct 25, 2024
650b2f0
Update README.md
starwalkn Oct 25, 2024
67e2be8
Update README.md
starwalkn Oct 25, 2024
964491e
Update README.md
starwalkn Oct 25, 2024
ca778c2
Update README.md
starwalkn Oct 25, 2024
03e83e5
Update README.md
starwalkn Oct 25, 2024
caa7463
Update README.md
starwalkn Oct 25, 2024
27c3a91
Update README.md
starwalkn Oct 25, 2024
2bc4a13
feat: move document to separate package
Oct 25, 2024
0ebc1cc
Update README.md
starwalkn Oct 25, 2024
70b4dc3
feat: rename post method to send
Oct 25, 2024
6d8f393
Merge branch 'experimental' of https://github.com/dcaraxes/gotenberg-…
Oct 25, 2024
f34d21e
Update README.md
starwalkn Oct 25, 2024
76a9d1e
Update README.md
starwalkn Oct 25, 2024
db1ac0b
Update README.md
starwalkn Oct 25, 2024
4347933
Update README.md
starwalkn Oct 25, 2024
e2fa088
Update README.md
starwalkn Oct 25, 2024
450165a
Update README.md
starwalkn Oct 25, 2024
7950c3d
Update README.md
starwalkn Oct 25, 2024
0209823
Update README.md
starwalkn Oct 25, 2024
06c6e27
Update README.md
starwalkn Oct 25, 2024
6917e8a
Update README.md
starwalkn Oct 25, 2024
dd19559
Merge branch 'beta' into experimental
starwalkn Oct 25, 2024
95f7188
fix: change script module name
Oct 25, 2024
3d14211
feat: add mit license
Oct 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Alexander Pikeev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
326 changes: 167 additions & 159 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,196 +1,204 @@
<!-- TOC start (generated with https://github.com/derlin/bitdowntoc) -->

- [Gotenberg Go Client](#gotenberg-go-client)
* [Install](#install)
* [Usage](#usage)
+ [First steps](#first-steps)
- [Create the client](#create-the-client)
- [Prepare files](#prepare-files)
+ [Generating PDF from HTML](#generating-pdf-from-html)
+ [Read and write EXIF metadata](#read-and-write-exif-metadata)
- [Write](#write)
- [Read](#read)
+ [Making screenshots](#making-screenshots)
* [Badges](#badges)

<!-- TOC end -->

<!-- TOC --><a name="gotenberg-go-client"></a>
# 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.

<!-- TOC --><a name="install"></a>
## Install

```bash
$ go get -u github.com/dcaraxes/gotenberg-go-client/v8
<p align="center">
<h1 align="center">Gotenberg Go Client</h1>
<p align="center">The Go client for interacting with a Gotenberg API. This project is a further development of the <a href="https://github.com/thecodingmachine/gotenberg-go-client">client from TheCodingMachine</a>, which does not support the new functionality since version 7 of Gotenberg.
</p>
</p>

---

## Installation

> [!IMPORTANT]
> Sometimes beta versions are released, which may contain both new functionality and changes incompatible with previous versions of the client. When installing beta versions, read the release notes carefully!

To get the latest version of the client:

```zsh
$ go get github.com/dcaraxes/gotenberg-go-client@latest
```

<!-- TOC --><a name="usage"></a>
## Usage
## Usage examples

<!-- TOC --><a name="first-steps"></a>
### First steps
### Create a client and prepare the files

<!-- TOC --><a name="create-the-client"></a>
#### Create the client
```golang
```go
package main

import (
"net/http"
"time"

"github.com/dcaraxes/gotenberg-go-client/v8"
"os"

"github.com/dcaraxes/gotenberg-go-client"
"github.com/dcaraxes/gotenberg-go-client/document"
)

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}
// Create the Gotenberg client.
client, err := gotenberg.NewClient("localhost:3000", http.DefaultClient)

// There are several ways to create documents.
f1, err := document.FromPath("data.pdf", "/path/to/file")
f2, err := document.FromString("index.html", "<html>Foo</html>")
f3, err := document.FromBytes("index.html", []byte("<html>Foo</html>"))

r, err := os.Open("index.html")
f4, err := document.FromReader("index.html", r)
}
```

<!-- TOC --><a name="prepare-files"></a>
#### Prepare files
```golang
// From a path.
pdf, _ := gotenberg.NewDocumentFromPath("data.pdf", "/path/to/file")
### Converting PDF to HTML

// From a string.
index, _ := gotenberg.NewDocumentFromString("index.html", "<html>Foo</html>")
> [!TIP]
> Head to the [documentation](https://gotenberg.dev/) to learn about all request parameters.

// From a bytes.
index, _ := gotenberg.NewDocumentFromBytes("index.html", []byte("<html>Foo</html>"))
```go
package main

// From io.Reader.
r, err := os.Open("index.html")
index, _ := gotenberg.NewDocumentFromReader("index.html", r)
```
import (
"net/http"

"github.com/dcaraxes/gotenberg-go-client"
"github.com/dcaraxes/gotenberg-go-client/document"
)

<!-- TOC --><a name="generating-pdf-from-html"></a>
### Generating PDF from HTML
```golang
// 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)
func main() {
client, err := gotenberg.NewClient("localhost:3000", http.DefaultClient)

// Creates the Gotenberg documents from a files paths.
index, err := document.FromPath("index.html", "/path/to/file")
style, err := document.FromPath("style.css", "/path/to/file")
img, err := document.FromPath("img.png", "/path/to/file")

// Skips the IDLE events for faster PDF conversion.
req.SkipNetworkIdleEvent()
// Create the HTML request.
req := gotenberg.NewHTMLRequest(index)

// 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")
// Setting up basic auth (if needed).
req.UseBasicAuth("username", "password")

// Set the document parameters to request (optional).
req.Assets(style, img)
req.Margins(gotenberg.NoMargins)
req.Scale(0.75)
req.PaperSize(gotenberg.A4)

// 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/to/store.pdf")

// If you wish to redirect the response directly to the browser, you may also use:
resp, err := client.Send(req)
}

// If you wish to redirect the response directly to the browser, you may also use:
resp, _ := client.Post(req)
```

<!-- TOC --><a name="read-and-write-exif-metadata"></a>
### Read and write EXIF metadata
### Working with metadata
Reading metadata available only for PDF files, but you can write metadata to all Gotenberg supporting files.

<!-- TOC --><a name="write"></a>
#### Write
```golang
// 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",
}
### Writing metadata:

jsonMetadata, _ := json.Marshal(writeDataStruct)
req.Metadata(jsonMetadata)
_ = client.Store(req, "path/you/want/the/pdf/to/be/stored.pdf")
> [!TIP]
> You can write metadata to PDF for any request using the Metadata method.

resp, _ := client.Post(req)
```
```go
package main

<!-- TOC --><a name="read"></a>
#### Read
```golang
// 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 {
import (
"net/http"

"github.com/dcaraxes/gotenberg-go-client"
"github.com/dcaraxes/gotenberg-go-client/document"
)

func main() {
client, err := gotenberg.NewClient("localhost:3000", http.DefaultClient)

// Prepare the files required for your conversion.
doc, err := document.FromPath("filename.ext", "/path/to/file")
req := gotenberg.NewWriteMetadataRequest(doc)

// Sets result file name.
req.OutputFilename("foo.pdf")

data := struct {
Author string `json:"Author"`
Copyright string `json:"Copyright"`
} `json:"foo.pdf"`
}{
Author: "Author name",
Copyright: "Copyright",
}

md, err := json.Marshal(data)
req.Metadata(md)

resp, err := client.Send(req)
}
// Marshal metadata into a struct.
_ = json.NewDecoder(respRead.Body).Decode(&readData)
```

<!-- TOC --><a name="making-screenshots"></a>
### Making screenshots
Making screenshots only available for HTML, URL and Markdown requests.
```golang
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)
### Reading metadata:

```go
package main

import (
"encoding/json"
"net/http"

"github.com/dcaraxes/gotenberg-go-client"
"github.com/dcaraxes/gotenberg-go-client/document"
)

func main() {
client, err := gotenberg.NewClient("localhost:3000", http.DefaultClient)

// Prepare the files required for your conversion.
doc, err := document.FromPath("filename.ext", "/path/to/file")
req := gotenberg.NewReadMetadataRequest(doc)

resp, err := client.Send(req)

var data = struct {
FooPdf struct {
Author string `json:"Author"`
Copyright string `json:"Copyright"`
} `json:"foo.pdf"`
}

// Decode metadata into a struct.
err = json.NewDecoder(resp.Body).Decode(&data)
}

```

### Creating screenshots

> [!NOTE]
> Screenshot creation is only available for HTML, URL and Markdown requests.

```go
package main

import (
"net/http"

"github.com/dcaraxes/gotenberg-go-client"
"github.com/dcaraxes/gotenberg-go-client/document"
)

For more complete usages, head to the [documentation](https://gotenberg.dev/).
func main() {
c, err := gotenberg.NewClient("localhost:3000", http.DefaultClient)

index, err := document.FromPath("index.html", "/path/to/file")

// Create the HTML request and set the image format (optional).
req := gotenberg.NewHTMLRequest(index)
req.Format(gotenberg.JPEG)

resp, err := client.Screenshot(req)
}

```

<!-- TOC --><a name="badges"></a>
## Badges
---

[![GoDoc](https://godoc.org/github.com/dcaraxes/gotenberg-go-client/v8?status.svg)](https://godoc.org/github.com/dcaraxes/gotenberg-go-client/v8)
[![Go Report Card](https://goreportcard.com/badge/github.com/dcaraxes/gotenberg-go-client/v8)](https://goreportcard.com/report/github.com/dcaraxes/gotenberg-go-client/v8)
**For more complete usages, head to the [documentation](https://gotenberg.dev/).**
Loading
Loading