All Projects β†’ SebastiaanKlippert β†’ Go Wkhtmltopdf

SebastiaanKlippert / Go Wkhtmltopdf

Licence: mit
Golang commandline wrapper for wkhtmltopdf

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Go Wkhtmltopdf

Expense Tracker with Pdf report
An expense Tracker πŸ”₯πŸ”₯ which lets you add transactions πŸ–ŠπŸ–Š and generate a pdf report of all of your transactionsπŸ“‹πŸ“‹
Stars: ✭ 16 (-97.16%)
Mutual labels:  pdf-document, pdf-generation
Pdf Lib
Create and modify PDF documents in any JavaScript environment
Stars: ✭ 3,426 (+507.45%)
Mutual labels:  pdf-generation, pdf-document
Labelmake
Declarative style JavaScript PDF generator library. Works on Node and the browser πŸ–¨οΈŽ
Stars: ✭ 112 (-80.14%)
Mutual labels:  pdf-generation, pdf-document
Android-XML-to-PDF-Generator
This library is for convert XML to PDF very easily using Step Builders Pattern
Stars: ✭ 140 (-75.18%)
Mutual labels:  pdf-document, pdf-generation
Printable Mockups
Create printable UI mockups & wireframes templates
Stars: ✭ 479 (-15.07%)
Mutual labels:  pdf-generation, pdf-document
laravel-print-api
Laravel package to access our print-api
Stars: ✭ 16 (-97.16%)
Mutual labels:  pdf-document, pdf-generation
Pdfgen
Simple C PDF Writer/Generation library
Stars: ✭ 200 (-64.54%)
Mutual labels:  pdf-generation, pdf-document
pdfio
PDFio is a simple C library for reading and writing PDF files.
Stars: ✭ 55 (-90.25%)
Mutual labels:  pdf-document, pdf-generation
pdftron-android-samples
PDFTron Android Samples
Stars: ✭ 30 (-94.68%)
Mutual labels:  pdf-document, pdf-generation
scryber.core
Scryber.Core is a dotnet 5 html to pdf engine written entirely in C# for creating beautiful flowing documents from html templates including css styles, object data binding and svg drawing.
Stars: ✭ 74 (-86.88%)
Mutual labels:  pdf-document, pdf-generation
Rinohtype
The Python document processor
Stars: ✭ 365 (-35.28%)
Mutual labels:  pdf-generation
Md To Pdf
Hackable CLI tool for converting Markdown files to PDF using Node.js and headless Chrome.
Stars: ✭ 374 (-33.69%)
Mutual labels:  pdf-generation
Runoob Pdf
ηˆ¬ε–θœιΈŸζ•™η¨‹η½‘η«™εΉΆθ½¬PDF__python_crawer_by_chrome
Stars: ✭ 430 (-23.76%)
Mutual labels:  pdf-generation
Dart pdf
Pdf creation module for dart/flutter
Stars: ✭ 500 (-11.35%)
Mutual labels:  pdf-generation
Technical Ebooks
PDFs for programming tutorials.
Stars: ✭ 342 (-39.36%)
Mutual labels:  pdf-document
Tabulizer
Bindings for Tabula PDF Table Extractor Library
Stars: ✭ 413 (-26.77%)
Mutual labels:  pdf-document
Tea School
Simplified HTML + CSS --> PDF Generator for Nodejs
Stars: ✭ 326 (-42.2%)
Mutual labels:  pdf-generation
Phpjasper
A PHP report generator
Stars: ✭ 327 (-42.02%)
Mutual labels:  pdf-generation
Django Easy Pdf
PDF views, the easy way
Stars: ✭ 324 (-42.55%)
Mutual labels:  pdf-generation
Laravel Pdf
πŸ“„ Easily generate PDF documents from HTML inside of Laravel 5
Stars: ✭ 552 (-2.13%)
Mutual labels:  pdf-generation

PkgGoDev Build Status Go Report Card codebeat badge codecov

go-wkhtmltopdf

Golang commandline wrapper for wkhtmltopdf

See http://wkhtmltopdf.org/index.html for wkhtmltopdf docs.

What and why

We 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.

go-wkhtmltopdf is a pure Golang wrapper around the wkhtmltopdf command line utility.

It has all options typed out as struct members which makes it very easy to use if you use an IDE with code completion and it has type safety for all options. For example you can set general options like

pdfg.Dpi.Set(600)
pdfg.NoCollate.Set(false)
pdfg.PageSize.Set(PageSizeA4)
pdfg.MarginBottom.Set(40)

The same goes for adding pages, settings page options, TOC options per page etc.

It takes care of setting the correct order of options as these can become very long with muliple pages where you have page and TOC options for each page.

Secondly it makes usage in server-type applications easier, every instance (PDF process) has its own output buffer which contains the PDF output and you can feed one input document from an io.Reader (using stdin in wkhtmltopdf). You can combine any number of external HTML documents (HTTP(S) links) with at most one HTML document from stdin and set options for each input document.

Note: You can also ignore the internal buffer and let wkhtmltopdf write directly to disk if required for large files, or use the SetOutput method to pass any io.Writer.

For us this is one of the easiest ways to generate PDF documents from Go(lang) and performance is very acceptable.

Installation

go get or use a Go dependency manager of your liking.

go get -u github.com/SebastiaanKlippert/go-wkhtmltopdf

go-wkhtmltopdf finds the path to wkhtmltopdf by

  • first looking in the current dir
  • looking in the PATH and PATHEXT environment dirs
  • using the WKHTMLTOPDF_PATH environment dir

If you need to set your own wkhtmltopdf path or want to change it during execution, you can call SetPath().

Usage

See testfile wkhtmltopdf_test.go for more complex options, a common use case test is in simplesample_test.go

package wkhtmltopdf

import (
  "fmt"
  "log"
)

func ExampleNewPDFGenerator() {

  // Create new PDF generator
  pdfg, err := NewPDFGenerator()
  if err != nil {
    log.Fatal(err)
  }

  // Set global options
  pdfg.Dpi.Set(300)
  pdfg.Orientation.Set(OrientationLandscape)
  pdfg.Grayscale.Set(true)

  // Create a new input page from an URL
  page := NewPage("https://godoc.org/github.com/SebastiaanKlippert/go-wkhtmltopdf")

  // Set options for this page
  page.FooterRight.Set("[page]")
  page.FooterFontSize.Set(10)
  page.Zoom.Set(0.95)

  // Add to document
  pdfg.AddPage(page)

  // Create PDF document in internal buffer
  err = pdfg.Create()
  if err != nil {
    log.Fatal(err)
  }

  // Write buffer contents to file on disk
  err = pdfg.WriteFile("./simplesample.pdf")
  if err != nil {
    log.Fatal(err)
  }

  fmt.Println("Done")
  // Output: Done
}

As mentioned before, you can provide one document from stdin, this is done by using a PageReader object as input to AddPage. This is best constructed with NewPageReader and will accept any io.Reader so this can be used with files from disk (os.File) or memory (bytes.Buffer) etc.
A simple example snippet:

html := "<html>Hi</html>"
pdfgen.AddPage(NewPageReader(strings.NewReader(html)))

Saving to and loading from JSON

The package now has the possibility to save the PDF Generator object as JSON and to create a new PDF Generator from a JSON file. All options and pages are saved in JSON, pages added using NewPageReader are read to memory before saving and then saved as Base64 encoded strings in the JSON file.

This is useful to prepare a PDF file and generate the actual PDF elsewhere, for example on AWS Lambda. To create PDF Generator on the client, where wkhtmltopdf might not be present, function NewPDFPreparer can be used.

Use NewPDFPreparer to create a PDF Generator object on the client and NewPDFGeneratorFromJSON to reconstruct it on the server.

// Client code
pdfg := NewPDFPreparer()
htmlfile, err := ioutil.ReadFile("./testfiles/htmlsimple.html")
if err != nil {
  log.Fatal(err)
}
    
pdfg.AddPage(NewPageReader(bytes.NewReader(htmlfile)))
pdfg.Dpi.Set(600)
    
// The contents of htmlsimple.html are saved as base64 string in the JSON file
jb, err := pdfg.ToJSON()
if err != nil {
  log.Fatal(err)
}
    
// Server code
pdfgFromJSON, err := NewPDFGeneratorFromJSON(bytes.NewReader(jb))
if err != nil {
  log.Fatal(err)
}
    
err = pdfgFromJSON.Create()
if err != nil {
  log.Fatal(err)
}    

For an example of running this in AWS Lambda see https://github.com/SebastiaanKlippert/go-wkhtmltopdf-lambda

Speed

The 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.

The go wrapper time is negligible with around 0.04ms for parsing an above average number of commandline options.

Benchmarks are included.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].