{"id":15033359,"url":"https://github.com/sebastiaanklippert/go-wkhtmltopdf","last_synced_at":"2025-05-14T23:04:19.139Z","repository":{"id":38018363,"uuid":"46633120","full_name":"SebastiaanKlippert/go-wkhtmltopdf","owner":"SebastiaanKlippert","description":"Golang commandline wrapper for wkhtmltopdf","archived":false,"fork":false,"pushed_at":"2024-07-02T13:32:14.000Z","size":164,"stargazers_count":1117,"open_issues_count":10,"forks_count":157,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-05-14T23:03:36.709Z","etag":null,"topics":["go","golang","html","html-to-pdf","pdf","pdf-document","pdf-generation","wkhtmltopdf"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SebastiaanKlippert.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-11-21T20:44:39.000Z","updated_at":"2025-05-13T07:24:15.000Z","dependencies_parsed_at":"2023-01-25T09:15:50.625Z","dependency_job_id":"9e76bc10-edf7-4bf9-81ce-9658cd2a89e3","html_url":"https://github.com/SebastiaanKlippert/go-wkhtmltopdf","commit_stats":{"total_commits":149,"total_committers":13,"mean_commits":"11.461538461538462","dds":0.6711409395973154,"last_synced_commit":"e67bc7d28589b18056866ba63420b28bf7635ecf"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SebastiaanKlippert%2Fgo-wkhtmltopdf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SebastiaanKlippert%2Fgo-wkhtmltopdf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SebastiaanKlippert%2Fgo-wkhtmltopdf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SebastiaanKlippert%2Fgo-wkhtmltopdf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SebastiaanKlippert","download_url":"https://codeload.github.com/SebastiaanKlippert/go-wkhtmltopdf/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254243358,"owners_count":22038046,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","html","html-to-pdf","pdf","pdf-document","pdf-generation","wkhtmltopdf"],"created_at":"2024-09-24T20:20:55.339Z","updated_at":"2025-05-14T23:04:19.110Z","avatar_url":"https://github.com/SebastiaanKlippert.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![PkgGoDev](https://pkg.go.dev/badge/github.com/SebastiaanKlippert/go-wkhtmltopdf)](https://pkg.go.dev/github.com/SebastiaanKlippert/go-wkhtmltopdf)\n[![Go Report Card](https://goreportcard.com/badge/SebastiaanKlippert/go-wkhtmltopdf)](https://goreportcard.com/report/SebastiaanKlippert/go-wkhtmltopdf)\n[![codebeat badge](https://codebeat.co/badges/a6bb7f66-7ae2-4de8-8b61-623ef68096c9)](https://codebeat.co/projects/github-com-sebastiaanklippert-go-wkhtmltopdf-master)\n[![codecov](https://codecov.io/gh/SebastiaanKlippert/go-wkhtmltopdf/branch/master/graph/badge.svg)](https://codecov.io/gh/SebastiaanKlippert/go-wkhtmltopdf)\n\n[![Build Status](https://github.com/SebastiaanKlippert/go-wkhtmltopdf/actions/workflows/ubuntu.yml/badge.svg?branch=master)](https://github.com/SebastiaanKlippert/go-wkhtmltopdf/actions/workflows/ubuntu.yml)\n[![Build Status](https://github.com/SebastiaanKlippert/go-wkhtmltopdf/actions/workflows/macos.yml/badge.svg?branch=master)](https://github.com/SebastiaanKlippert/go-wkhtmltopdf/actions/workflows/macos.yml)\n\n# go-wkhtmltopdf\nGolang commandline wrapper for wkhtmltopdf\n\nSee http://wkhtmltopdf.org/index.html for wkhtmltopdf docs.\n\n| :warning: WARNING          |\n|:---------------------------|\n| wkhtmltopdf is no longer maintained and now archived on GitHub. See https://wkhtmltopdf.org/status.html |\n| This go package is still maintained and will be for a while, but I recommend looking for alternatives for new projects. |\n\n\n# What and why\nWe needed a way to generate PDF documents from Go. These vary from invoices with highly customizable lay-outs to reports with tables, graphs and images. In our opinion the best way to do this was by using HTML/CSS templates as source for our PDFs. Using CSS print media types and millimeters instead of pixel units we can generate very acurate PDF documents using wkhtmltopdf.\n\ngo-wkhtmltopdf is a pure Golang wrapper around the wkhtmltopdf command line utility.\n\nIt has all options typed out as struct members which makes it very easy to use if you use an IDE with\ncode completion and it has type safety for all options.\nFor example you can set general options like\n```go\npdfg.Dpi.Set(600)\npdfg.NoCollate.Set(false)\npdfg.PageSize.Set(PageSizeA4)\npdfg.MarginBottom.Set(40)\n``` \nThe same goes for adding pages, settings page options, TOC options per page etc.\n\nIt takes care of setting the correct order of options as these can become very long with muliple pages where \nyou have page and TOC options for each page.\n\nSecondly it makes usage in server-type applications easier, every instance (PDF process) has its own output buffer \nwhich contains the PDF output and you can feed one input document from an io.Reader (using stdin in wkhtmltopdf).\nYou can combine any number of external HTML documents (HTTP(S) links) with at most one HTML document from stdin and set \noptions for each input document.\n\nNote: You can also ignore the internal buffer and let wkhtmltopdf write directly to disk if required for large files, or use the [SetOutput](https://godoc.org/github.com/SebastiaanKlippert/go-wkhtmltopdf#PDFGenerator.SetOutput) method to pass any `io.Writer`.\n\nFor us this is one of the easiest ways to generate PDF documents from Go(lang) and performance is very acceptable.\n\n# Installation\ngo get or use a Go dependency manager of your liking.\n\n```\ngo get -u github.com/SebastiaanKlippert/go-wkhtmltopdf\n```\n\ngo-wkhtmltopdf finds the path to wkhtmltopdf by\n* first looking in the current dir\n* looking in the PATH and PATHEXT environment dirs\n* using the WKHTMLTOPDF_PATH environment dir\n\n**Warning**: Running executables from the current path is no longer possible in Go 1.19, see https://pkg.go.dev/os/exec@master#hdr-Executables_in_the_current_directory\n\nIf you need to set your own wkhtmltopdf path or want to change it during execution, you can call SetPath().\n\n# Usage\nSee testfile ```wkhtmltopdf_test.go``` for more complex options, a common use case test is in ```simplesample_test.go``` \n\n```go\npackage wkhtmltopdf\n\nimport (\n  \"fmt\"\n  \"log\"\n)\n\nfunc ExampleNewPDFGenerator() {\n\n  // Create new PDF generator\n  pdfg, err := NewPDFGenerator()\n  if err != nil {\n    log.Fatal(err)\n  }\n\n  // Set global options\n  pdfg.Dpi.Set(300)\n  pdfg.Orientation.Set(OrientationLandscape)\n  pdfg.Grayscale.Set(true)\n\n  // Create a new input page from an URL\n  page := NewPage(\"https://godoc.org/github.com/SebastiaanKlippert/go-wkhtmltopdf\")\n\n  // Set options for this page\n  page.FooterRight.Set(\"[page]\")\n  page.FooterFontSize.Set(10)\n  page.Zoom.Set(0.95)\n\n  // Add to document\n  pdfg.AddPage(page)\n\n  // Create PDF document in internal buffer\n  err = pdfg.Create()\n  if err != nil {\n    log.Fatal(err)\n  }\n\n  // Write buffer contents to file on disk\n  err = pdfg.WriteFile(\"./simplesample.pdf\")\n  if err != nil {\n    log.Fatal(err)\n  }\n\n  fmt.Println(\"Done\")\n  // Output: Done\n}\n```\n\nAs mentioned before, you can provide one document from stdin, this is done by using a [PageReader](https://godoc.org/github.com/SebastiaanKlippert/go-wkhtmltopdf#PageReader \"GoDoc\") object as input to AddPage. This is best constructed with  [NewPageReader](https://godoc.org/github.com/SebastiaanKlippert/go-wkhtmltopdf#NewPageReader \"GoDoc\") and will accept any io.Reader so this can be used with files from disk (os.File) or memory (bytes.Buffer) etc.  \nA simple example snippet:\n```go\nhtml := \"\u003chtml\u003eHi\u003c/html\u003e\"\npdfgen.AddPage(NewPageReader(strings.NewReader(html)))\n```\n\n# Saving to and loading from JSON\n\nThe package now has the possibility to save the PDF Generator object as JSON and to create\na new PDF Generator from a JSON file.\nAll options and pages are saved in JSON, pages added using NewPageReader are read to memory before saving and then saved as Base64 encoded strings\nin the JSON file.\n\nThis is useful to prepare a PDF file and generate the actual PDF elsewhere, for example on AWS Lambda.\nTo create PDF Generator on the client, where wkhtmltopdf might not be present, function `NewPDFPreparer` can be used.\n\nUse `NewPDFPreparer` to create a PDF Generator object on the client and `NewPDFGeneratorFromJSON` to reconstruct it on the server.\n\n```go \n// Client code\npdfg := NewPDFPreparer()\nhtmlfile, err := ioutil.ReadFile(\"testdata/htmlsimple.html\")\nif err != nil {\n  log.Fatal(err)\n}\n    \npdfg.AddPage(NewPageReader(bytes.NewReader(htmlfile)))\npdfg.Dpi.Set(600)\n    \n// The contents of htmlsimple.html are saved as base64 string in the JSON file\njb, err := pdfg.ToJSON()\nif err != nil {\n  log.Fatal(err)\n}\n    \n// Server code\npdfgFromJSON, err := NewPDFGeneratorFromJSON(bytes.NewReader(jb))\nif err != nil {\n  log.Fatal(err)\n}\n    \nerr = pdfgFromJSON.Create()\nif err != nil {\n  log.Fatal(err)\n}    \n```\n\nFor an example of running this in AWS Lambda see https://github.com/SebastiaanKlippert/go-wkhtmltopdf-lambda\n\n# Speed \nThe speed if pretty much determined by wkhtmltopdf itself, or if you use external source URLs, the time it takes to get and render the source HTML.\n\nThe go wrapper time is negligible with around 0.04ms for parsing an above average number of commandline options.\n\nBenchmarks are included.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastiaanklippert%2Fgo-wkhtmltopdf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsebastiaanklippert%2Fgo-wkhtmltopdf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsebastiaanklippert%2Fgo-wkhtmltopdf/lists"}