Documentation
¶
Overview ¶
Package mdtopdf implements a PDF document generator for markdown documents.
Introduction ¶
This package depends on two other packages:
* The [gomarkdown](https://github.com/gomarkdown/markdown) parser to read the markdown source
* The fpdf package to generate the PDF
The tests included here are from the BlackFriday package. See the "testdata" folder. The tests create PDF files and thus while the tests may complete without errors, visual inspection of the created PDF is the only way to determine if the tests *really* pass!
The tests create log files that trace the BlackFriday parser callbacks. This is a valuable debug tool showing each callback and data provided in each while the AST is presented.
Installation ¶
To install the package:
go get github.com/mandolyte/mdtopdf
Quick start ¶
In the cmd folder is an example using the package. It demonstrates a number of features. The test PDF was created with this command:
go run convert.go -i test.md -o test.pdf
See README for limitations and known issues ¶
Package mdtopdf converts markdown to PDF.
Index ¶
- func ExtractTextFromNode(node ast.Node) string
- type Color
- type PdfRenderer
- func (r *PdfRenderer) Process(content []byte) error
- func (r *PdfRenderer) RenderFooter(w io.Writer, _ ast.Node)
- func (r *PdfRenderer) RenderHeader(w io.Writer, ast ast.Node)
- func (r *PdfRenderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.WalkStatus
- func (r *PdfRenderer) Run(content []byte) error
- func (r *PdfRenderer) SetCustomTheme(themeJSONFile string)
- func (r *PdfRenderer) SetDarkTheme()
- func (r *PdfRenderer) SetLightTheme()
- func (r *PdfRenderer) SetPageBackground(colorStr string, color Color)
- func (r *PdfRenderer) SetTOCLinks(tocHeaders map[string]*int)
- func (r *PdfRenderer) UpdateBlockquoteStyler()
- func (r *PdfRenderer) UpdateCodeStyler()
- func (r *PdfRenderer) UpdateParagraphStyler(defaultStyler Styler)
- type PdfRendererParams
- type RenderOption
- type Styler
- type TOCEntry
- type TOCVisitor
- type Theme
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExtractTextFromNode ¶
ExtractTextFromNode recursively extracts text content from AST nodes
Types ¶
type Color ¶
type Color struct {
Red, Green, Blue int
}
Color is a RGB set of ints; for a nice picker see https://www.w3schools.com/colors/colors_picker.asp
func Colorlookup ¶
Colorlookup returns a RGB triple corresponding to the named color, "rgb(r,g,b)" or "#rrggbb" string. On error, return black.
type PdfRenderer ¶
type PdfRenderer struct {
// Pdf can be used to access the underlying created fpdf object
// prior to processing the markdown source
Pdf *fpdf.Fpdf
// normal text
Normal Styler
// link text
Link Styler
// backticked text
Backtick Styler
// blockquote text
Blockquote Styler
IndentValue float64
// Headings
H1 Styler
H2 Styler
H3 Styler
H4 Styler
H5 Styler
H6 Styler
// Table styling
THeader Styler
TBody Styler
// code styling
Code Styler
// update styling
NeedCodeStyleUpdate bool
NeedBlockquoteStyleUpdate bool
HorizontalRuleNewPage bool
SyntaxHighlightBaseDir string
InputBaseURL string
Theme Theme
BackgroundColor Color
Extensions parser.Extensions
ColumnWidths map[ast.Node][]float64
// contains filtered or unexported fields
}
PdfRenderer is the struct to manage conversion of a markdown object to PDF format.
func NewPdfRenderer ¶
func NewPdfRenderer(params PdfRendererParams) *PdfRenderer
NewPdfRenderer creates and configures an PdfRenderer object, which satisfies the Renderer interface.
func NewPdfRendererWithDefaultStyler ¶
func NewPdfRendererWithDefaultStyler(orient, papersz, pdfFile, tracerFile string, defaultStyler Styler, opts []RenderOption, theme Theme) *PdfRenderer
NewPdfRendererWithDefaultStyler creates and configures an PdfRenderer object, which satisfies the Renderer interface. update default styler for normal
func (*PdfRenderer) Process ¶
func (r *PdfRenderer) Process(content []byte) error
Process takes the markdown content, parses it to generate the PDF
func (*PdfRenderer) RenderFooter ¶
func (r *PdfRenderer) RenderFooter(w io.Writer, _ ast.Node)
RenderFooter is not supported.
func (*PdfRenderer) RenderHeader ¶
func (r *PdfRenderer) RenderHeader(w io.Writer, ast ast.Node)
RenderHeader is not supported.
func (*PdfRenderer) RenderNode ¶
func (r *PdfRenderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.WalkStatus
RenderNode is a default renderer of a single node of a syntax tree. For block nodes it will be called twice: first time with entering=true, second time with entering=false, so that it could know when it's working on an open tag and when on close. It writes the result to w.
The return value is a way to tell the calling walker to adjust its walk pattern: e.g. it can terminate the traversal by returning Terminate. Or it can ask the walker to skip a subtree of this node by returning SkipChildren. The typical behavior is to return GoToNext, which asks for the usual traversal to the next node. (above taken verbatim from the blackfriday v2 package)
func (*PdfRenderer) Run ¶
func (r *PdfRenderer) Run(content []byte) error
Run takes the markdown content, parses it but don't generate the PDF. you can access the PDF with youRenderer.Pdf
func (*PdfRenderer) SetCustomTheme ¶
func (r *PdfRenderer) SetCustomTheme(themeJSONFile string)
SetCustomTheme sets a custom theme based on JSON config
func (*PdfRenderer) SetDarkTheme ¶
func (r *PdfRenderer) SetDarkTheme()
SetDarkTheme sets theme to 'dark'
func (*PdfRenderer) SetLightTheme ¶
func (r *PdfRenderer) SetLightTheme()
SetLightTheme sets theme to 'light'
func (*PdfRenderer) SetPageBackground ¶
func (r *PdfRenderer) SetPageBackground(colorStr string, color Color)
SetPageBackground - sets background colour of page. String IDs ("blue", "grey", etc) and `Color` structs are both supported
func (*PdfRenderer) SetTOCLinks ¶
func (r *PdfRenderer) SetTOCLinks(tocHeaders map[string]*int)
SetTOCLinks these will be used in `nodeProcessing.go:processText()` if the header is encoutered as we need to call `r.Pdf.SetLink()` if that's the case
func (*PdfRenderer) UpdateBlockquoteStyler ¶
func (r *PdfRenderer) UpdateBlockquoteStyler()
UpdateBlockquoteStyler - update Blockquote fill styler
func (*PdfRenderer) UpdateCodeStyler ¶
func (r *PdfRenderer) UpdateCodeStyler()
UpdateCodeStyler - update code fill styler
func (*PdfRenderer) UpdateParagraphStyler ¶
func (r *PdfRenderer) UpdateParagraphStyler(defaultStyler Styler)
UpdateParagraphStyler - update with default styler
type PdfRendererParams ¶
type PdfRendererParams struct {
Orientation, Papersz, PdfFile, TracerFile, FontFile, FontName string
Opts []RenderOption
Theme Theme
CustomThemeFile string
}
PdfRendererParams struct to hold params passed to NewPdfRenderer
type RenderOption ¶
type RenderOption func(r *PdfRenderer)
RenderOption allows to define functions to configure the renderer
func IsHorizontalRuleNewPage ¶
func IsHorizontalRuleNewPage(value bool) RenderOption
IsHorizontalRuleNewPage if true, will start a new page when encountering a HR (---). Useful for presentations.
func SetSyntaxHighlightBaseDir ¶
func SetSyntaxHighlightBaseDir(path string) RenderOption
SetSyntaxHighlightBaseDir path to https://github.com/jessp01/gohighlight/tree/master/syntax_files
func WithUnicodeTranslator ¶
func WithUnicodeTranslator(cp string) RenderOption
WithUnicodeTranslator configures a unico translator to support characters for latin, russian, etc..
type Styler ¶
type Styler struct {
Font string
Style string
Size float64
Spacing float64
TextColor Color
FillColor Color
}
Styler is the struct to capture the styling features for text Size and Spacing are specified in points. The sum of Size and Spacing is used as line height value in the fpdf API
type TOCEntry ¶
TOCEntry represents a table of contents entry
func GetTOCEntries ¶
GetTOCEntries returns TOC entries
type TOCVisitor ¶
type TOCVisitor struct {
Entries []TOCEntry
}
TOCVisitor implements ast.NodeVisitor to collect headers
func (*TOCVisitor) Visit ¶
func (v *TOCVisitor) Visit(node ast.Node, entering bool) ast.WalkStatus
Visit implements the ast.NodeVisitor interface