Documentation
¶
Index ¶
- Variables
- func FromHTML(ctx context.Context, htmlStr string, cfgs ...*entity.Config) (*core.Pdf, error)
- func FromHTMLReader(ctx context.Context, r io.Reader, cfgs ...*entity.Config) (*core.Pdf, error)
- type Paper
- func (m *Paper) AddAutoRow(cols ...core.Col) core.Row
- func (m *Paper) AddHTML(ctx context.Context, htmlStr string) error
- func (m *Paper) AddPages(pages ...core.Page)
- func (m *Paper) AddRow(rowHeight float64, cols ...core.Col) core.Row
- func (m *Paper) AddRows(rows ...core.Row)
- func (m *Paper) FitInCurrentPage(heightNewLine float64) bool
- func (m *Paper) Generate(ctx context.Context) (*core.Pdf, error)
- func (m *Paper) GetCurrentConfig() *entity.Config
- func (m *Paper) GetStructure() *node.Node[core.Structure]
- func (m *Paper) RegisterFooter(rows ...core.Row) error
- func (m *Paper) RegisterHeader(rows ...core.Row) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrCannotGenerateInLowMemoryMode = errors.New("an error has occurred while trying to generate PDFs in low memory mode") ErrCannotGenerateInParallelMode = errors.New("an error has occurred while trying to generate PDFs concurrently") ErrHeaderHeightIsGreaterThanUsefulArea = errors.New("header height is greater than page useful area") // ErrHTMLHeaderAfterContent is returned by AddHTML when the HTML contains // a top-level <header>/<footer> but the document already has rows or a // registered header/footer: late registration would not reserve space on // already-built pages. ErrHTMLHeaderAfterContent = errors.New("html top-level header/footer must be added before any other content") )
Functions ¶
func FromHTML ¶
FromHTML converts an HTML string directly into a PDF document. It is the shortest path for callers that only need HTML-to-PDF output. It observes ctx while parsing/translating HTML and while generating the PDF. Optional configs are the same configs accepted by New.
When no config argument is given, a plain `@page { size; margin }` rule in the HTML configures the page; an explicit config argument disables @page handling entirely (documented precedence rule, no field-level merging).
Example ¶
ExampleFromHTML demonstrates the shortest path from HTML to PDF.
package main
import (
"context"
"log"
"github.com/avdoseferovic/paper"
)
func main() {
doc, err := paper.FromHTML(context.Background(), `<h1>Hello</h1><p>World</p>`)
if err != nil {
log.Fatal(err)
}
_ = doc.GetBytes()
}
Output:
func FromHTMLReader ¶
FromHTMLReader reads HTML from r and converts it directly into a PDF document. It observes ctx before and after reading, while translating HTML, and while generating the PDF. Optional configs are the same configs accepted by New.
Types ¶
type Paper ¶
type Paper struct {
// contains filtered or unexported fields
}
func New ¶
New creates a concrete Paper instance. It's optional to provide an *entity.Config with customizations those customization are created by using the config.Builder.
Example ¶
ExampleNew demonstrates how to create a paper instance.
package main
import (
"context"
"github.com/avdoseferovic/paper"
"github.com/avdoseferovic/paper/pkg/config"
)
func main() {
// optional
b := config.NewBuilder()
cfg := b.Build()
m := paper.New(cfg) // cfg is an optional
// Do things and generate
_, _ = m.Generate(context.Background())
}
Output:
func (*Paper) AddAutoRow ¶
AddAutoRow is responsible for adding a line with automatic height to the current document. The row height will be calculated based on its content.
func (*Paper) AddHTML ¶
AddHTML parses an HTML string into Paper rows and adds them to the current document. It observes ctx while parsing and translating the HTML. Headers, footers, and pagination continue to work as with manually constructed rows. For advanced options (e.g. html.WithImageBaseDir for safe <img> loading), call html.FromString directly and append the returned rows via m.AddRows(rows...). Supported HTML subset is documented in docs/html-support.md.
func (*Paper) AddPages ¶
AddPages is responsible for add pages directly in the document. By adding a page directly, the current cursor will reset and the new page will appear as the next. If the page provided have more rows than the maximum useful area of a page, paper will split that page in more than one.
Example ¶
ExamplePaper_AddPages demonstrates how to add a new page in paper.
package main
import (
"github.com/avdoseferovic/paper"
"github.com/avdoseferovic/paper/pkg/components/code"
"github.com/avdoseferovic/paper/pkg/components/page"
)
func main() {
m := paper.New()
p := page.New()
p.Add(code.NewBarRow(10, "barcode"))
m.AddPages(p)
// Do things and generate
}
Output:
func (*Paper) AddRow ¶
AddRow is responsible for add one row in the current document. By adding a row, if the row will extrapolate the useful area of a page, paper will automatically add a new page. Paper use the information of PageSize, PageMargin, FooterSize and HeaderSize to calculate the useful area of a page.
Example ¶
ExamplePaper_AddRow demonstrates how to add a new row in paper.
package main
import (
"github.com/avdoseferovic/paper/pkg/components/text"
"github.com/avdoseferovic/paper"
)
func main() {
m := paper.New()
m.AddRow(10, text.NewCol(12, "text"))
// Do things and generate
}
Output:
func (*Paper) AddRows ¶
AddRows is responsible for add rows in the current document. By adding a row, if the row will extrapolate the useful area of a page, paper will automatically add a new page. Paper use the information of PageSize, PageMargin, FooterSize and HeaderSize to calculate the useful area of a page.
Example ¶
ExamplePaper_AddRows demonstrates how to add new rows in paper.
package main
import (
"github.com/avdoseferovic/paper/pkg/components/text"
"github.com/avdoseferovic/paper"
"github.com/avdoseferovic/paper/pkg/components/code"
)
func main() {
m := paper.New()
m.AddRows(
code.NewBarRow(12, "barcode"),
text.NewRow(12, "text"),
)
// Do things and generate
}
Output:
func (*Paper) FitInCurrentPage ¶
FitInCurrentPage reports whether a row of the given height fits in the remaining useful area of the current page.
Example ¶
ExamplePaper_FitInCurrentPage demonstrates how to check if the new line fits on the current page.
package main
import (
"github.com/avdoseferovic/paper"
)
func main() {
m := paper.New()
m.FitInCurrentPage(12)
// Do things and generate
}
Output:
func (*Paper) Generate ¶
Generate is responsible to compute the component tree created by the usage of all other Paper methods, and generate the PDF document. It observes ctx between pages and generation phases.
Example ¶
ExamplePaper_Generate demonstrates how to generate a file.
package main
import (
"context"
"log"
"github.com/avdoseferovic/paper"
)
func main() {
m := paper.New()
// Add rows, pages and etc.
doc, err := m.Generate(context.Background())
if err != nil {
log.Fatal(err)
}
// You can retrieve as Base64, Save file, Merge with another file or GetReport.
_ = doc.GetBytes()
}
Output:
func (*Paper) GetCurrentConfig ¶
GetCurrentConfig is responsible for returning the current settings from the file
Example ¶
ExamplePaper_GetCurrentConfig demonstrates how to get the current paper configuration.
package main
import (
"github.com/avdoseferovic/paper"
)
func main() {
m := paper.New()
m.GetCurrentConfig()
// Do things and generate
}
Output:
func (*Paper) GetStructure ¶
GetStructure is responsible for return the component tree, this is useful on unit tests cases.
Example ¶
ExamplePaperGetStruct demonstrates how to get paper component tree
package main
import (
"github.com/avdoseferovic/paper/pkg/components/text"
"github.com/avdoseferovic/paper"
)
func main() {
m := paper.New()
m.AddRow(40, text.NewCol(12, "text"))
m.GetStructure()
// Do things and generate
}
Output:
func (*Paper) RegisterFooter ¶
RegisterFooter is responsible to define a set of rows as a footer of the document. The footer will appear in every new page of the document. The footer cannot occupy an area greater than the useful area of the page, it this case the method will return an error.
func (*Paper) RegisterHeader ¶
RegisterHeader is responsible to define a set of rows as a header of the document. The header will appear in every new page of the document. The header cannot occupy an area greater than the useful area of the page, it this case the method will return an error.
Example ¶
ExamplePaper_RegisterHeader demonstrates how to register a header to me added in every new page. An error is returned if the area occupied by the header is greater than the page area.
package main
import (
"github.com/avdoseferovic/paper/pkg/components/text"
"github.com/avdoseferovic/paper"
"github.com/avdoseferovic/paper/pkg/components/code"
)
func main() {
m := paper.New()
err := m.RegisterHeader(
code.NewBarRow(12, "barcode"),
text.NewRow(12, "text"))
if err != nil {
panic(err)
}
// Do things and generate
}
Output:
Directories
¶
| Path | Synopsis |
|---|---|
|
docs
module
|
|
|
examples
module
|
|
|
internal
|
|
|
assert
Package assert provides lightweight test assertions used by the root module.
|
Package assert provides lightweight test assertions used by the root module. |
|
cmd/mockfix
command
Command mockfix rewrites generated mocks in internal/mocks so they import the vendored mock runtime (internal/mocktest) instead of testify.
|
Command mockfix rewrites generated mocks in internal/mocks so they import the vendored mock runtime (internal/mocktest) instead of testify. |
|
goleak
Package goleak provides a small goroutine leak check for this repository's tests.
|
Package goleak provides a small goroutine leak check for this repository's tests. |
|
mocktest
Package mock provides the small mock API subset used by generated internal test mocks.
|
Package mock provides the small mock API subset used by generated internal test mocks. |
|
pdf
Package pdf contains Paper's internal PDF runtime.
|
Package pdf contains Paper's internal PDF runtime. |
|
providers
Package providers contains repository-wide provider guard tests.
|
Package providers contains repository-wide provider guard tests. |
|
require
Package require provides fail-fast test assertions used by the root module.
|
Package require provides fail-fast test assertions used by the root module. |
|
svg
Package svg provides in-process SVG rasterisation helpers for PDF image embedding.
|
Package svg provides in-process SVG rasterisation helpers for PDF image embedding. |
|
pkg
|
|
|
components/checkbox
Package checkbox implements creation of checkboxes.
|
Package checkbox implements creation of checkboxes. |
|
components/code
Package code implements creation of Barcode, MatrixCode and QrCode.
|
Package code implements creation of Barcode, MatrixCode and QrCode. |
|
components/col
Package col implements creation of columns.
|
Package col implements creation of columns. |
|
components/html
Package html implements a component wrapper for Paper's HTML translator.
|
Package html implements a component wrapper for Paper's HTML translator. |
|
components/htmllist
Package htmllist implements HTML-style bullet and numbered list components.
|
Package htmllist implements HTML-style bullet and numbered list components. |
|
components/image
Package image implements creation of images from file and bytes.
|
Package image implements creation of images from file and bytes. |
|
components/line
Package line implements creation of lines.
|
Package line implements creation of lines. |
|
components/list
Package list implements creation of lists (old tablelist).
|
Package list implements creation of lists (old tablelist). |
|
components/page
Package page implements creation of pages.
|
Package page implements creation of pages. |
|
components/richtext
Package richtext implements a PDF component for paragraphs with mixed inline styling.
|
Package richtext implements a PDF component for paragraphs with mixed inline styling. |
|
components/row
Package row implements creation of rows.
|
Package row implements creation of rows. |
|
components/signature
Package signature implements creation of signatures.
|
Package signature implements creation of signatures. |
|
components/table
Package table implements a PDF table component with colspan, rowspan, and per-cell styling.
|
Package table implements a PDF table component with colspan, rowspan, and per-cell styling. |
|
components/text
Package text implements creation of texts.
|
Package text implements creation of texts. |
|
config
Package config implements custom configuration builder.
|
Package config implements custom configuration builder. |
|
consts
Package consts groups the small enumeration types used across Paper: alignment, orientation, line styles, break-line strategies, font families, barcode types, generation modes, and provider types.
|
Package consts groups the small enumeration types used across Paper: alignment, orientation, line styles, break-line strategies, font families, barcode types, generation modes, and provider types. |
|
consts/border
Package border contains all border types.
|
Package border contains all border types. |
|
consts/extension
Package extension contains all image extensions.
|
Package extension contains all image extensions. |
|
consts/fontstyle
Package fontstyle contains all default font styles.
|
Package fontstyle contains all default font styles. |
|
consts/pagesize
Package pagesize contains all default page sizes.
|
Package pagesize contains all default page sizes. |
|
consts/protection
Package protection contains all protection types.
|
Package protection contains all protection types. |
|
core
Package core contains all core interfaces and basic implementations.
|
Package core contains all core interfaces and basic implementations. |
|
core/entity
Package entity contains all core entities.
|
Package entity contains all core entities. |
|
decorator
Package decorator provides decorators over the core.Paper interface.
|
Package decorator provides decorators over the core.Paper interface. |
|
fontrepository
Package fontrepository implements font repository.
|
Package fontrepository implements font repository. |
|
html
Package html converts HTML strings into Paper rows so they can be added to a Paper document.
|
Package html converts HTML strings into Paper rows so they can be added to a Paper document. |
|
html/dom
Package dom provides a Paper-friendly wrapper over golang.org/x/net/html.
|
Package dom provides a Paper-friendly wrapper over golang.org/x/net/html. |
|
html/translate
Package translate — flex layout dispatch and item-content construction.
|
Package translate — flex layout dispatch and item-content construction. |
|
merge
Package merge implements PDF merge.
|
Package merge implements PDF merge. |
|
metrics
Package metrics contains metrics models, constants and formatting.
|
Package metrics contains metrics models, constants and formatting. |
|
props
Package props contain the public properties of paper.
|
Package props contain the public properties of paper. |
|
test
Package test exposes Paper's golden-structure test helper for library consumers.
|
Package test exposes Paper's golden-structure test helper for library consumers. |
|
tree/node
Package node provides a small generic tree node used to describe Paper document structure.
|
Package node provides a small generic tree node used to describe Paper document structure. |