gopdfwrapper

package module
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2020 License: MIT Imports: 6 Imported by: 1

README

gopdf-wrapper

The gopdf library is a great library for creating PDFs in golang. This wrapper provides some convince abstractions for recurring tasks. Like:

  • Maintain a default font size
  • Add formatted text
  • Add multiline text
  • Auto wrap text
  • gopdf-wrapper embeds the Lato font and Liberation Sans which is probably enough for simple reports

The code of this library is licensed under the MIT License the fonts (Lato and Liberation Sans) on the other hand are licensed under the SIL Open Font License (OFL).

Example

import (
	"github.com/72nd/gopdf-wrapper/fonts"
	wrapper "github.com/72nd/gopdf-wrapper"
)

// New document with font size 12 and line spread 1. 
doc, err := wrapper.NewDoc(12, 1)
if err != nil {
	t.Error(err)
}
liberation, err := fonts.NewLiberationSansFamily()
if err != nil {
	t.Error(err)
}
doc.SetFontFamily(*liberation)
doc.AddPage()

// Text
doc.AddFormattedText(10, 20, "This is a document", 25, "bold")
doc.AddSizedText(10, 20 + doc.LineHeight(25), "Some subtitle", 18)
doc.AddText(10, 40, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.")
doc.AddMultilineText(10, 50, "Ut enim ad minim veniam,\nquis nostrud exercitation ullamco laboris\nnisi ut aliquip ex ea commodo consequat.\nDuis aute irure dolor in reprehenderit\nin voluptate velit esse cillum dolore\neu fugiat nulla pariatur.")
doc.AddWrapText(10, 80, 140, "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.")

// Lines
doc.AddLine(10, 140, 30, 160, 0.1, SolidLine)
doc.AddLine(10, 160, 30, 140, 0.1, SolidLine)
doc.AddLine(10, 150, 30, 150, 0.1, SolidLine)
doc.AddLine(20, 140, 20, 160, 0.1, SolidLine)

// Image
doc.Image("image.png", 10, 190, nil)

// Write document to PDF.
doc.WritePdf("document.pdf")

This generates the following output:

Output

Documentation

Overview

Package gopdfwrapper is a simple wrapper around the gopdf(https://github.com/signintech/gopdf) library aiming to simplify recurring tasks of creating PDF's with gopdf.

Index

Constants

View Source
const (
	// NormalStyle is the normal font style aka. regular style.
	NormalStyle FontStyle = gopdf.Regular
	// ItalicStyle represents the italic font style.
	ItalicStyle = gopdf.Italic
	// BoldStyle represents the bold font style.
	BoldStyle = gopdf.Bold
	// UnderlineStyle represents the underline font style.
	UnderlineStyle = gopdf.Underline
)
View Source
const (
	// SolidLine is a solid line.
	SolidLine LineStyle = ""
	// DashedLine is a dashed line.
	DashedLine = "dashed"
	// DottedLine is a dotted line.
	DottedLine = "dotted"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Doc

type Doc struct {
	gopdf.GoPdf
	// contains filtered or unexported fields
}

Doc is the basic structure for a PDF file.

func NewDoc

func NewDoc(fontSize int, lineSpread float64) (*Doc, error)

NewDoc returns a new Doc.

func (*Doc) AddFormattedMultilineText

func (d *Doc) AddFormattedMultilineText(x, y float64, content string, size int, style string)

AddFormattedMultilineText has the same functionality as AddMultilineText but with a individual font-size and style.

func (*Doc) AddFormattedText

func (d *Doc) AddFormattedText(x, y float64, content string, size int, style string)

AddFormattedText adds a text field at the given position with individual size and style.

func (*Doc) AddFormattedWrapText added in v0.3.0

func (d *Doc) AddFormattedWrapText(x1, y, x2 float64, content string, size int, style string)

AddFormattedWrapText automatically wraps the formatted text when the line reaches x2.

func (*Doc) AddLine

func (d *Doc) AddLine(x1, y1, x2, y2, width float64, lineStyle LineStyle)

AddLine adds a line to the Document.

func (*Doc) AddMultilineText

func (d *Doc) AddMultilineText(x, y float64, content string)

AddMultilineText adds a text field with multiple lines of text. The lines are divided by the new-line character `\n`.

func (*Doc) AddSizedText

func (d *Doc) AddSizedText(x, y float64, content string, size int)

AddSizedText adds a text field at the given position with the given size.

func (*Doc) AddText

func (d *Doc) AddText(x, y float64, content string) error

AddText adds a text field at the given position.

func (*Doc) AddWrapText added in v0.3.0

func (d *Doc) AddWrapText(x1, y, x2 float64, content string)

AddWrapText automatically wraps the text when the line reaches x2.

func (*Doc) DefaultFontSize

func (d *Doc) DefaultFontSize()

DefaultFontSize resets the font size to the initial default.

func (*Doc) DefaultFontStyle

func (d *Doc) DefaultFontStyle()

DefaultFontStyle resets the font style to normal style.

func (Doc) DefaultLineHeight

func (d Doc) DefaultLineHeight() float64

DefaultLineHeight calculates and returns the line height.

func (Doc) LineHeight

func (d Doc) LineHeight(fontSize int) float64

LineHeight returns the line height of a text line with a given height in mm.

func (*Doc) SetFontFamily

func (d *Doc) SetFontFamily(family fonts.FontFamily) error

SetFontFamily sets the used font family.

func (*Doc) SetFontSize

func (d *Doc) SetFontSize(size int) error

SetFontSize sets the font size for all elements added after.

func (*Doc) SetFontStyle

func (d *Doc) SetFontStyle(style string) error

SetFontStyle changes the font style (italic, bold...) for elements added afterwards.

func (*Doc) SetPosition

func (d *Doc) SetPosition(x, y float64)

SetPosition encapsulates the SetX and SetY methods of gopdf. New elements will be added at the currently set position.

type FontStyle

type FontStyle int

FontStyle wraps the gopdf font style constants into a type.

type LineStyle

type LineStyle string

LineStyle wraps the gopdf line styles into a type.

Directories

Path Synopsis
Package fonts contains the embedded fonts and utility functions.
Package fonts contains the embedded fonts and utility functions.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL