goldpdf

package module
v0.0.0-...-3d296b2 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2024 License: MIT Imports: 19 Imported by: 0

README

goldpdf

Yet another PDF renderer for goldmark.

動機

MarkdownをPDFに変換する、以下の要求を満たすライブラリが必要でした。

  • Markdownの十分なサポート
  • カスタマイズが可能
    • フォントサイズ
    • 出力する判型やテーブルの背景色
  • ストレスの無いAPI

stephenafamo/goldmark-pdf は完成度が高く有力な候補でしたが、テーブルのセル幅が動的に調整されないという不満がありました。またカスタムフォントを使おうとすると不自然なAPIを経由する必要がありました。

raykov/mdtopdf もまた有用でしたが、goldmark-pdfと同様の不満に加え、Markdownの一部のサポートが不十分であり、カスタマイズ性に欠けるという問題がありました。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetNaturalWidth

func GetNaturalWidth(mc MeasureContext, elements []InlineElement) float64

TODO リネーム

func New

func New(options ...Option) renderer.Renderer

func TableLayoutAutoCompact

func TableLayoutAutoCompact(r *Renderer, n *xast.Table, mc MeasureContext, borderBox HalfBounds) ([]float64, error)

TableLayoutAutoCompact determines the width of a column in proportion to the width of the column's content, but does not expand beyond the width required for the content

func TableLayoutAutoFilled

func TableLayoutAutoFilled(r *Renderer, n *xast.Table, mc MeasureContext, borderBox HalfBounds) ([]float64, error)

TableLayoutAutoFilled is a TableLayout that expands the column width to fill the table by a ratio based on the width of the column content

func TableLayoutEvenly

func TableLayoutEvenly(r *Renderer, n *xast.Table, mc MeasureContext, borderBox HalfBounds) ([]float64, error)

TableLayoutEvenly is a TableLayout that expands to fill the table so that each column is of equal width, without considering the column contents

Types

type BlockStyle

type BlockStyle struct {
	Margin          Spacing
	Padding         Spacing
	BackgroundColor color.Color
	Border          Border
	TextAlign       xast.Alignment
	TableLayout     TableLayout
}

type Border

type Border interface {
	Spacer
}

type BorderEdge

type BorderEdge struct {
	Width float64
	Color color.Color
}

type DefaultImageLoader

type DefaultImageLoader struct {
	ErrorMode DefaultImageLoaderErrorMode
	// contains filtered or unexported fields
}

func (*DefaultImageLoader) LoadImage

func (il *DefaultImageLoader) LoadImage(src string) (img *ImageElement, err error)

type DefaultImageLoaderErrorMode

type DefaultImageLoaderErrorMode int
const (
	ReturnError DefaultImageLoaderErrorMode = iota
	IgnoreErrorAndShowAlt
)

type DefaultStyler

type DefaultStyler struct {
	FontFamily  string
	FontSize    float64
	Color       color.Color
	TableLayout TableLayout
}

func (*DefaultStyler) Style

type HalfBounds

type HalfBounds struct {
	Left, Right float64
	Top         VerticalCoord
}

HalfBounds represents a bounds on the left, right and top coordinates.

func (HalfBounds) Expand

func (b HalfBounds) Expand(spacers ...Spacer) HalfBounds

func (HalfBounds) Shrink

func (b HalfBounds) Shrink(spacers ...Spacer) HalfBounds

func (HalfBounds) ToRect

func (b HalfBounds) ToRect(Bottom VerticalCoord) Rect

func (HalfBounds) Width

func (b HalfBounds) Width() float64

type ImageElement

type ImageElement struct {
	Name          string
	ImageType     string // see ImageType of fpdf.ImageOptions
	Width, Height float64
	Bytes         []byte
}

ImageElement は、単一の画像です

func (*ImageElement) String

func (t *ImageElement) String() string

type ImageLoader

type ImageLoader interface {
	LoadImage(string) (*ImageElement, error)
}

type IndividualBorder

type IndividualBorder struct {
	Left, Top, Right, Bottom BorderEdge
}

func (IndividualBorder) Space

type InlineElement

type InlineElement interface {
	String() string
	// contains filtered or unexported methods
}

InlineElement は PDFに描画されるインラインの要素であり、テキストか画像の2種類があります

type LineBreakElement

type LineBreakElement struct {
	Format TextFormat
}

LineBreakElement は、改行を表すインライン要素です

func (*LineBreakElement) String

func (t *LineBreakElement) String() string

type MeasureContext

type MeasureContext interface {
	GetTextWidth(span *TextElement) float64
	GetSubText(span *TextElement, width float64) *TextElement
	GetPageVerticalBounds(page int) (float64, float64)
	GetRenderContext(fn func(RenderContext) error) error
}

MeasureContext provides a way to measure the dimensions of the drawing element.

type Option

type Option func(*Renderer)

func WithImageLoader

func WithImageLoader(imageLoader ImageLoader) Option

func WithPDFProvider

func WithPDFProvider(pdfProvider PDFProvider) Option

func WithStyler

func WithStyler(styler Styler) Option

type PDFProvider

type PDFProvider func() *gofpdf.Fpdf

type Rect

type Rect struct {
	Left, Right float64
	Top, Bottom VerticalCoord
}

Rect is a rectangle that can span multiple pages of a PDF document.

func (Rect) Expand

func (r Rect) Expand(spacers ...Spacer) Rect

func (Rect) Shrink

func (r Rect) Shrink(spacers ...Spacer) Rect

func (Rect) ToHalfBounds

func (r Rect) ToHalfBounds() HalfBounds

func (Rect) Width

func (r Rect) Width() float64

type RenderContext

type RenderContext interface {
	MeasureContext
	DrawText(page int, x, y float64, span *TextElement)
	DrawImage(page int, x, y float64, img *ImageElement)
	DrawBullet(page int, x, y float64, c color.Color, r float64)
	DrawBox(rect Rect, bgColor color.Color, border Border)
}

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

func (*Renderer) AddOptions

func (r *Renderer) AddOptions(options ...renderer.Option)

AddOptions does nothing

func (*Renderer) Render

func (r *Renderer) Render(w io.Writer, source []byte, n ast.Node) error

type Spacer

type Spacer interface {
	Space() (float64, float64, float64, float64)
}

Spacer はボックス間のサイズを計算する際にマージン・ボーダー・パディングを共通で扱うためのインターフェースです

type Spacing

type Spacing struct {
	Left, Top, Right, Bottom float64
}

Spacing は単純な余白です

func (Spacing) Space

func (s Spacing) Space() (float64, float64, float64, float64)

type Styler

type Styler interface {
	Style(ast.Node, TextFormat) (BlockStyle, TextFormat)
}

type TableLayout

type TableLayout func(r *Renderer, n *xast.Table, mc MeasureContext, borderBox HalfBounds) ([]float64, error)

type TextElement

type TextElement struct {
	Format TextFormat
	Text   string
}

TextElement は、単一のテキストフォーマットが設定された改行を含まないテキストを持つインライン要素です

func (*TextElement) String

func (t *TextElement) String() string

type TextFormat

type TextFormat struct {
	Color           color.Color
	BackgroundColor color.Color
	FontSize        float64
	FontFamily      string
	Bold            bool
	Italic          bool
	Strike          bool
	Underline       bool
	Border          UniformBorder
}

type UniformBorder

type UniformBorder struct {
	Width  float64
	Color  color.Color
	Radius float64
}

func (UniformBorder) Space

func (b UniformBorder) Space() (float64, float64, float64, float64)

type VerticalCoord

type VerticalCoord struct {
	Page     int
	Position float64
}

func (VerticalCoord) LessThan

func (vc VerticalCoord) LessThan(vc2 VerticalCoord) bool

Directories

Path Synopsis
test module

Jump to

Keyboard shortcuts

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