Skip to content

Commit

Permalink
Merge pull request #52 from MicahParks/master
Browse files Browse the repository at this point in the history
Document main thread conversion requirement in more places
  • Loading branch information
adrg authored Dec 28, 2024
2 parents c8a4e68 + 27cb09e commit 54f1cbc
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 9 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ Full documentation can be found at https://pkg.go.dev/github.com/adrg/go-wkhtmlt
* [Basic web page to PDF conversion server](examples/http-server)
* [Configurable web page to PDF conversion server](examples/http-server-advanced)

> Note: The `HTML` to `PDF` conversion (calls to the `Converter.Run` method) must be performed on the main thread.
> This is a limitation of the wkhtmltox library. Please see the basic `HTTP` server [example](examples/http-server)
> for more information.
## Prerequisites

In order to use the package, `wkhtmltox` must be installed. Installation packages
Expand Down Expand Up @@ -129,14 +133,15 @@ func main() {
converter.MarginLeft = "10mm"
converter.MarginRight = "10mm"

// Convert objects and save the output PDF document.
// Create output file.
outFile, err := os.Create("out.pdf")
if err != nil {
log.Fatal(err)
}
defer outFile.Close()

// Run converter.
// Run converter. Due to a limitation of the `wkhtmltox` library, the
// conversion must be performed on the main thread.
if err := converter.Run(outFile); err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 2 additions & 0 deletions converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ func (c *Converter) Add(object *Object) {
}

// Run performs the conversion and copies the output to the provided writer.
// Due to a limitation of the `wkhtmltox` library, this method must be called
// on the main thread.
func (c *Converter) Run(w io.Writer) error {
if c.converter == nil {
return errors.New("cannot use uninitialized or destroyed converter")
Expand Down
5 changes: 3 additions & 2 deletions examples/basic-usage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ func main() {
converter.MarginLeft = "10mm"
converter.MarginRight = "10mm"

// Convert objects and save the output PDF document.
// Create output file.
outFile, err := os.Create("out.pdf")
if err != nil {
log.Fatal(err)
}
defer outFile.Close()

// Run converter.
// Run converter. Due to a limitation of the `wkhtmltox` library, the
// conversion must be performed on the main thread.
if err := converter.Run(outFile); err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion examples/converter-callbacks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ func main() {

converter.Add(object)

// Convert objects and save the output PDF document.
// Create output file.
outFile, err := os.Create("out.pdf")
if err != nil {
log.Fatal(err)
}
defer outFile.Close()

// Run converter. Due to a limitation of the `wkhtmltox` library, the
// conversion must be performed on the main thread.
if err := converter.Run(outFile); err != nil {
log.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion examples/http-server-advanced/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ func startServer() {
// Add object to the converter.
converter.Add(object)

// Perform the conversion.
// Run converter. Due to a limitation of the `wkhtmltox` library,
// the conversion must be performed on the main thread.
return converter.Run(out)
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
3 changes: 2 additions & 1 deletion examples/http-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ func startServer() {
converter.Title = url
converter.PaperSize = pdf.A4

// Perform the conversion.
// Run converter. Due to a limitation of the `wkhtmltox` library,
// the conversion must be performed on the main thread.
return converter.Run(out)
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
4 changes: 3 additions & 1 deletion examples/json-input/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ func main() {
// Add object to the converter.
converter.Add(object)

// Convert object and save the output PDF document.
// Create output file.
outFile, err := os.Create("out.pdf")
if err != nil {
log.Fatal(err)
}
defer outFile.Close()

// Run converter. Due to a limitation of the `wkhtmltox` library, the
// conversion must be performed on the main thread.
if err := converter.Run(outFile); err != nil {
log.Fatal(err)
}
Expand Down
5 changes: 4 additions & 1 deletion pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Package pdf implements wkhtmltopdf Go bindings. It can be used to convert HTML d
The package does not use the wkhtmltopdf binary. Instead, it uses the wkhtmltox library directly.
Example
package main
import (
Expand Down Expand Up @@ -69,13 +70,15 @@ Example
converter.MarginLeft = "10mm"
converter.MarginRight = "10mm"
// Convert objects and save the output PDF document.
// Create output file.
outFile, err := os.Create("out.pdf")
if err != nil {
log.Fatal(err)
}
defer outFile.Close()
// Run converter. Due to a limitation of the `wkhtmltox` library, the
// conversion must be performed on the main thread.
if err := converter.Run(outFile); err != nil {
log.Fatal(err)
}
Expand Down

0 comments on commit 54f1cbc

Please sign in to comment.