Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adrg/go-wkhtmltopdf
Handcrafted Go bindings for wkhtmltopdf and high-level HTML to PDF conversion interface
https://github.com/adrg/go-wkhtmltopdf
bindings converter go golang golang-library golang-package html html-to-pdf library native pdf pdf-conversion pdf-converter wkhtmltopdf wkhtmltox
Last synced: about 12 hours ago
JSON representation
Handcrafted Go bindings for wkhtmltopdf and high-level HTML to PDF conversion interface
- Host: GitHub
- URL: https://github.com/adrg/go-wkhtmltopdf
- Owner: adrg
- License: mit
- Created: 2016-07-29T13:43:46.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2025-01-17T20:05:46.000Z (15 days ago)
- Last Synced: 2025-01-25T08:05:08.452Z (8 days ago)
- Topics: bindings, converter, go, golang, golang-library, golang-package, html, html-to-pdf, library, native, pdf, pdf-conversion, pdf-converter, wkhtmltopdf, wkhtmltox
- Language: Go
- Homepage: https://pkg.go.dev/github.com/adrg/go-wkhtmltopdf
- Size: 121 KB
- Stars: 255
- Watchers: 6
- Forks: 21
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
Go bindings and high-level HTML to PDF conversion interface.
Implements [wkhtmltopdf](https://wkhtmltopdf.org) 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](examples/basic-usage/main.go)
* [Converter callbacks](examples/converter-callbacks/main.go)
* [Convert HTML document based on JSON input](examples/json-input/main.go)
* [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 `HTTP` server [example](examples/http-server)
> for more information.## Prerequisites
In order to use the package, `wkhtmltox` must be installed. Installation packages
for multiple operating systems can be found at [https://builds.wkhtmltopdf.org](https://wkhtmltopdf.org/downloads.html).Please see the wiki pages of this project for detailed installation instructions.
- [Install on Linux](https://github.com/adrg/go-wkhtmltopdf/wiki/Install-on-Linux)
- [Install on Windows](https://github.com/adrg/go-wkhtmltopdf/wiki/Install-on-Windows)> Note: `wkhtmltox` does not seem to be actively maintained. Please see the [project status](https://wkhtmltopdf.org/status.html) for more information, recommendations and future plans.
## Installation
go get github.com/adrg/go-wkhtmltopdf## Usage
```go
package mainimport (
"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"// 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)
}
}```
## Stargazers over time
[![Stargazers over time](https://starchart.cc/adrg/go-wkhtmltopdf.svg)](https://starchart.cc/adrg/go-wkhtmltopdf)
## Contributing
Contributions in the form of pull requests, issues or just general feedback,
are always welcome.
See [CONTRIBUTING.MD](CONTRIBUTING.md).**Contributors**:
[adrg](https://github.com/adrg),
[leandrosilva](https://github.com/leandrosilva),
[MicahParks](https://github.com/MicahParks).## References
For more information see the [wkhtmltopdf documentation](https://wkhtmltopdf.org/usage/wkhtmltopdf.txt)
and the [wkhtmltox documentation](https://wkhtmltopdf.org/libwkhtmltox).## License
Copyright (c) 2016 Adrian-George Bostan.
This project is licensed under the [MIT license](https://opensource.org/licenses/MIT).
See [LICENSE](LICENSE) for more details.