Documentation
¶
Overview ¶
Package wkhtmltopdf implements a wrapper for the html to pdf converter wkhtmltopdf.
For more information on wkhtmltopdf see: http://wkhtmltopdf.org/
Creating Documents ¶
Creating a pdf document is simple:
doc := wkhtmltopdf.NewDocument() pg := wkhtmltopdf.NewPage("www.google.com") doc.AddPages(pg) doc.WriteToFile("google.pdf")
Pages can be sourced from both URLs and local filenames.
Applying Options ¶
You can apply options to both the document and individual pages when creating them.
doc := wkhtmltopdf.NewDocument(wkhtmltopdf.Grayscale(), wkhtmltopdf.PageSize("A4")) pg := wkhtmltopdf.NewPage("www.google.com", wkhtmltopdf.DefaultHeader()) doc.AddPages(pg) doc.WriteToFile("google.pdf")
Using Readers and Writers ¶
As well as URLs/filenames, you can source pages from an io.Reader, and write them to an io.Writer.
If a single reader is provided, this is piped to wkhtmltopdf through stdin. If multiple readers are provided, the contents are written to a temporary directory and used from there. The location of the temporary directories can be changed by setting the TempDir variable.
doc := wkhtmltopdf.NewDocument() buf := bytes.NewBufferString("<html><body><h1>Test Page</h1></body></html>") pg, err := wkhtmltopdf.NewPageReader(buf) if err != nil { log.Fatal("Error reading from reader.") } doc.AddPages(pg) output := &bytes.Buffer{} err := doc.Write(output) if err != nil { log.Fatal("Error writing to writer.") }
Example ¶
package main /* This example creates an http server, which returns a simple pdf document with a title and the path of the request. */ import ( "bytes" "html/template" "log" "net/http" "github.com/andrewcharlton/wkhtmltopdf-go" ) const page = ` <html> <body> <h1>Test Page</h1> <p>Path: {{.}}</p> </body> </html>` func handler(w http.ResponseWriter, r *http.Request) { tmpl := template.Must(template.New("page").Parse(page)) buf := &bytes.Buffer{} tmpl.Execute(buf, r.URL.String()) doc := wkhtmltopdf.NewDocument() pg, err := wkhtmltopdf.NewPageReader(buf) if err != nil { log.Fatal("Error reading page buffer") } doc.AddPages(pg) w.Header().Set("Content-Type", "application/pdf") w.Header().Set("Content-Disposition", `attachment; filename="test.pdf"`) err = doc.Write(w) if err != nil { log.Fatal("Error serving pdf") } } func main() { http.HandleFunc("/", handler) http.ListenAndServe(":8080", nil) }
Output:
Index ¶
- Variables
- type Document
- type GlobalOption
- func CookieJar(path string) GlobalOption
- func DPI(dpi int) GlobalOption
- func DisableDottedLines() GlobalOption
- func DisableTocLinks() GlobalOption
- func Grayscale() GlobalOption
- func ImageDPI(dpi int) GlobalOption
- func ImageQuality(quality int) GlobalOption
- func Landscape() GlobalOption
- func LowQuality() GlobalOption
- func MarginBottom(units string) GlobalOption
- func MarginLeft(units string) GlobalOption
- func MarginRight(units string) GlobalOption
- func MarginTop(units string) GlobalOption
- func NoCollate() GlobalOption
- func NoOutline() GlobalOption
- func NoPDFCompression() GlobalOption
- func Outline() GlobalOption
- func OutlineDepth(level int) GlobalOption
- func PageHeight(units string) GlobalOption
- func PageSize(size string) GlobalOption
- func PageWidth(units string) GlobalOption
- func Quiet() GlobalOption
- func Title(title string) GlobalOption
- func TocHeaderText(text string) GlobalOption
- func TocLevelIndentation(width string) GlobalOption
- func TocTextSizeShrink(factor float64) GlobalOption
- func XSLStyleSheet(file string) GlobalOption
- type Option
- type Page
- type PageOption
- func Allow(path string) PageOption
- func Background() PageOption
- func BypassProxy(host string) PageOption
- func CacheDir(path string) PageOption
- func CheckboxCheckedSVG(path string) PageOption
- func CheckboxSVG(path string) PageOption
- func Cookie(name, value string) PageOption
- func CustomHeader(name, value string) PageOption
- func CustomHeaderPropagation() PageOption
- func DefaultHeader() PageOption
- func DisableExternalLinks() PageOption
- func DisableForms() PageOption
- func DisableInternalLinks() PageOption
- func DisableJavascript() PageOption
- func DisableLocalFileAccess() PageOption
- func DisablePlugins() PageOption
- func DisableSmartShrinking() PageOption
- func DisableTocBackLinks() PageOption
- func EnableExternalLinks() PageOption
- func EnableForms() PageOption
- func EnableInternalLinks() PageOption
- func EnableJavascript() PageOption
- func EnableLocalFileAccess() PageOption
- func EnablePlugins() PageOption
- func EnableSmartShrinking() PageOption
- func EnableTocBackLinks() PageOption
- func Encoding(encoding string) PageOption
- func ExcludeFromOutline() PageOption
- func FooterCenter(text string) PageOption
- func FooterFontName(font string) PageOption
- func FooterFontSize(size int) PageOption
- func FooterHTML(url string) PageOption
- func FooterLeft(text string) PageOption
- func FooterLine() PageOption
- func FooterRight(text string) PageOption
- func FooterSpacing(spacing float64) PageOption
- func HeaderCenter(text string) PageOption
- func HeaderFontName(font string) PageOption
- func HeaderFontSize(size int) PageOption
- func HeaderHTML(url string) PageOption
- func HeaderLeft(text string) PageOption
- func HeaderLine() PageOption
- func HeaderRight(text string) PageOption
- func HeaderSpacing(spacing float64) PageOption
- func Images() PageOption
- func IncludeInOutline() PageOption
- func JavascriptDelay(msec int) PageOption
- func KeepRelativeLinks() PageOption
- func LoadErrorHandling(handler string) PageOption
- func LoadMediaErrorHandling(handler string) PageOption
- func MinFontSize(size int) PageOption
- func NoBackground() PageOption
- func NoCustomHeaderPropagation() PageOption
- func NoFooterLine() PageOption
- func NoHeaderLine() PageOption
- func NoImages() PageOption
- func NoPrintMediaType() PageOption
- func NoStopSlowScripts() PageOption
- func PageOffset(offset int) PageOption
- func Password(password string) PageOption
- func Post(name, value string) PageOption
- func PostFile(name, path string) PageOption
- func PrintMediaType() PageOption
- func Proxy(proxy string) PageOption
- func RadioButton(path string) PageOption
- func RadioButtonChecked(path string) PageOption
- func Replace(name, value string) PageOption
- func ResolveRelativeLinks() PageOption
- func RunScript(js string) PageOption
- func StopSlowScripts() PageOption
- func UserStyleSheet(url string) PageOption
- func Username(username string) PageOption
- func ViewportSize(size string) PageOption
- func WindowStatus(status string) PageOption
- func Zoom(factor float64) PageOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // Executable is the command to run wkhtmltopdf. If wkhtmltopdf // cannot be found on your path, amend this to its location. Executable = "wkhtmltopdf" // TempDir is where the directories for creating temporary // files are created. TempDir = "." )
Functions ¶
This section is empty.
Types ¶
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
A Document represents a single pdf document.
func (*Document) AddOptions ¶
AddOptions allows the setting of options after document creation.
func (*Document) AddPages ¶
AddPages to the document. Pages will be included in the final pdf in the order they are added.
func (*Document) WriteToFile ¶
WriteToFile creates the pdf document and writes it to the specified filename.
type GlobalOption ¶
type GlobalOption struct {
// contains filtered or unexported fields
}
A GlobalOption can be applied only to a document.
func CookieJar ¶
func CookieJar(path string) GlobalOption
CookieJar - read and write cookies from and to the supplied cookie jar file.
func DisableDottedLines ¶
func DisableDottedLines() GlobalOption
DisableDottedLines - do not use dotted lines in the toc
func DisableTocLinks ¶
func DisableTocLinks() GlobalOption
DisableTocLinks - do not link from toc to sections
func ImageDPI ¶
func ImageDPI(dpi int) GlobalOption
ImageDPI - When embedding images, scale them down to this dpi.
func ImageQuality ¶
func ImageQuality(quality int) GlobalOption
ImageQuality - When jpeg compressing images, use this quality (default 94).
func LowQuality ¶
func LowQuality() GlobalOption
LowQuality - Generates lower quality pdf/ps. Useful to shrink the result document space.
func MarginBottom ¶
func MarginBottom(units string) GlobalOption
MarginBottom - Set the page bottom margin.
func MarginRight ¶
func MarginRight(units string) GlobalOption
MarginRight - Set the page right margin.
func NoCollate ¶
func NoCollate() GlobalOption
NoCollate - do not collate when printing multiple copies.
func NoPDFCompression ¶
func NoPDFCompression() GlobalOption
NoPDFCompression - Do not use lossless compression on pdf objects.
func OutlineDepth ¶
func OutlineDepth(level int) GlobalOption
OutlineDepth - set the depth of the outline
func PageSize ¶
func PageSize(size string) GlobalOption
PageSize - Set paper size to A4, letter etc.
func Title ¶
func Title(title string) GlobalOption
Title - the title of the generated pdf file (the title of the first document is used if not specified).
func TocHeaderText ¶
func TocHeaderText(text string) GlobalOption
TocHeaderText - the header text of the toc
func TocLevelIndentation ¶
func TocLevelIndentation(width string) GlobalOption
TocLevelIndentation - for each level of headings in the toc indent by this length
func TocTextSizeShrink ¶
func TocTextSizeShrink(factor float64) GlobalOption
TocTextSizeShrink - for each level of headings in the toc the font is scaled by this factor
func XSLStyleSheet ¶
func XSLStyleSheet(file string) GlobalOption
XSLStyleSheet - use the supplied xsl style sheet for printing the table of content
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
An Option to be applied to a page or document.
type Page ¶
type Page struct {
// contains filtered or unexported fields
}
A Page represents a single html document, which may span multiple pages in the finished pdf document. Options can be applied to the page.
func NewPage ¶
func NewPage(filename string, opts ...PageOption) *Page
NewPage creates a new page from the given filename (which can be a url), with the given options.
func NewPageReader ¶
func NewPageReader(r io.Reader, opts ...PageOption) (*Page, error)
NewPageReader creates a new page from an io.Reader. The reader will be drained on page creation, and stored in a temporary buffer.
func (*Page) AddOptions ¶
func (pg *Page) AddOptions(opts ...PageOption)
AddOptions allows the setting of options after page creation.
type PageOption ¶
type PageOption struct {
// contains filtered or unexported fields
}
A PageOption can be applied to pages and/or documents.
func Allow ¶
func Allow(path string) PageOption
Allow the file or files from the specified folder to be loaded (repeatable)
func BypassProxy ¶
func BypassProxy(host string) PageOption
BypassProxy - bypass proxy for host (repeatable)
func CheckboxCheckedSVG ¶
func CheckboxCheckedSVG(path string) PageOption
CheckboxCheckedSVG - Use this svg file when rendering checked checkboxes
func CheckboxSVG ¶
func CheckboxSVG(path string) PageOption
CheckboxSVG - Use this svg file when rendering unchecked checkboxes
func Cookie ¶
func Cookie(name, value string) PageOption
Cookie - Set an additional cookie (repeatable), value should be url encoded.
func CustomHeader ¶
func CustomHeader(name, value string) PageOption
CustomHeader - Set an additional HTTP header (repeatable)
func CustomHeaderPropagation ¶
func CustomHeaderPropagation() PageOption
CustomHeaderPropagation - Add HTTP headers specified by --custom-header for each resource request.
func DefaultHeader ¶
func DefaultHeader() PageOption
DefaultHeader - Add a default header, with the name of the page to the left and the page numner to the right.
func DisableExternalLinks ¶
func DisableExternalLinks() PageOption
DisableExternalLinks - Do not make links to remote web pages
func DisableForms ¶
func DisableForms() PageOption
DisableForms - Do not turn HTML form fields into pdf form fields
func DisableInternalLinks ¶
func DisableInternalLinks() PageOption
DisableInternalLinks - do not make local links
func DisableJavascript ¶
func DisableJavascript() PageOption
DisableJavascript - do not allow web pages to run javascript
func DisableLocalFileAccess ¶
func DisableLocalFileAccess() PageOption
DisableLocalFileAccess - do not allow conversion of a local file to read in other local files unless explicitly allowed with Allow()
func DisableSmartShrinking ¶
func DisableSmartShrinking() PageOption
DisableSmartShrinking - disable the intelligent shrinking strategy used by webkit that makes the pixel/dpi ratio none constant.
func DisableTocBackLinks ¶
func DisableTocBackLinks() PageOption
DisableTocBackLinks - do not link from section header to toc
func EnableExternalLinks ¶
func EnableExternalLinks() PageOption
EnableExternalLinks - Make links to remote web pages
func EnableForms ¶
func EnableForms() PageOption
EnableForms - Turn HTML form fields into pdf form fields
func EnableInternalLinks ¶
func EnableInternalLinks() PageOption
EnableInternalLinks - make local links
func EnableJavascript ¶
func EnableJavascript() PageOption
EnableJavascript - do allow web pages to run javascript
func EnableLocalFileAccess ¶
func EnableLocalFileAccess() PageOption
EnableLocalFileAccess - do not allow conversion of a local file to read in other local files unless explicitly allowed with Allow()
func EnablePlugins ¶
func EnablePlugins() PageOption
EnablePlugins - enable installed plugins (plugins will likely not work)
func EnableSmartShrinking ¶
func EnableSmartShrinking() PageOption
EnableSmartShrinking - enable the intelligent shrinking strategy used by webkit that makes the pixel/dpi ratio none constant.
func EnableTocBackLinks ¶
func EnableTocBackLinks() PageOption
EnableTocBackLinks - link from section header to toc
func Encoding ¶
func Encoding(encoding string) PageOption
Encoding - Set the default text encoding for text input
func ExcludeFromOutline ¶
func ExcludeFromOutline() PageOption
ExcludeFromOutline - do not include in the table of contents and outlines
func FooterFontName ¶
func FooterFontName(font string) PageOption
FooterFontName - set footer font name
func FooterFontSize ¶
func FooterFontSize(size int) PageOption
FooterFontSize - set footer font size
func FooterSpacing ¶
func FooterSpacing(spacing float64) PageOption
FooterSpacing - spacing between the footer and content in mm.
func HeaderFontName ¶
func HeaderFontName(font string) PageOption
HeaderFontName - set header font name
func HeaderFontSize ¶
func HeaderFontSize(size int) PageOption
HeaderFontSize - set header font size
func HeaderSpacing ¶
func HeaderSpacing(spacing float64) PageOption
HeaderSpacing - spacing between the header and content in mm.
func IncludeInOutline ¶
func IncludeInOutline() PageOption
IncludeInOutline - include in the table of contents and outlines
func JavascriptDelay ¶
func JavascriptDelay(msec int) PageOption
JavascriptDelay - Wait some milliseconds for javascript to finish
func KeepRelativeLinks ¶
func KeepRelativeLinks() PageOption
KeepRelativeLinks - keep relative external links as relative external links
func LoadErrorHandling ¶
func LoadErrorHandling(handler string) PageOption
LoadErrorHandling - Specify how to handle pages that fail to load: abort, ignore or skip.
func LoadMediaErrorHandling ¶
func LoadMediaErrorHandling(handler string) PageOption
LoadMediaErrorHandling - specify how to handle media pages that fail to load: abort, ignore or skip.
func NoCustomHeaderPropagation ¶
func NoCustomHeaderPropagation() PageOption
NoCustomHeaderPropagation - Do not add HTTP headers specified by --custom-header for each resource request.
func NoFooterLine ¶
func NoFooterLine() PageOption
NoFooterLine - do not display line above the footer
func NoHeaderLine ¶
func NoHeaderLine() PageOption
NoHeaderLine - do not display line above the header
func NoPrintMediaType ¶
func NoPrintMediaType() PageOption
NoPrintMediaType - do not use print media type instead of screen
func PostFile ¶
func PostFile(name, path string) PageOption
PostFile - post an additional file (repeatable)
func PrintMediaType ¶
func PrintMediaType() PageOption
PrintMediaType - use print media type instead of screen
func RadioButton ¶
func RadioButton(path string) PageOption
RadioButton - use this svg file when rendering unchecked radio buttons
func RadioButtonChecked ¶
func RadioButtonChecked(path string) PageOption
RadioButtonChecked - use this svg file when rendering checked radio buttons
func Replace ¶
func Replace(name, value string) PageOption
Replace - replace 'name' with value in header and footer (repeatable).
func StopSlowScripts ¶
func StopSlowScripts() PageOption
StopSlowScripts - stop slow running javascripts
func UserStyleSheet ¶
func UserStyleSheet(url string) PageOption
UserStyleSheet - specify a user style sheet, to load with every page
func ViewportSize ¶
func ViewportSize(size string) PageOption
ViewportSize - set viewport size if you have custom scrollbars or css attribute over-flow to emulate window size
func WindowStatus ¶
func WindowStatus(status string) PageOption
WindowStatus - wait until window.status is equal to this string before rendering page