Documentation ¶
Overview ¶
Package mdtopdf implements a PDF document generator for markdown documents.
Introduction ¶
This package depends on two other packages:
* The BlackFriday v2 parser to read the markdown source
* The gofpdf packace 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/rickb777/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 ¶
- Variables
- type Color
- type PdfRenderer
- func (r *PdfRenderer) Output(w io.Writer) error
- func (r *PdfRenderer) Process(markdown []byte) *PdfRenderer
- func (r *PdfRenderer) RenderFooter(w io.Writer, ast *bf.Node)
- func (r *PdfRenderer) RenderHeader(w io.Writer, ast *bf.Node)
- func (r *PdfRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus
- func (r *PdfRenderer) ToFile(pdfFile string) error
- type Styler
Constants ¶
This section is empty.
Variables ¶
var ( Black = Color{0, 0, 0} White = Color{255, 255, 255} )
Functions ¶
This section is empty.
Types ¶
type Color ¶
type Color struct {
Red, Green, Blue int
}
Color is expressed in RGB components (0 - 255). For a nice picker, see https://www.w3schools.com/colors/colors_picker.asp
type PdfRenderer ¶
type PdfRenderer struct { // Pdf can be used to access the underlying created gofpdf object // prior to processing the markdown source Pdf *gofpdf.Fpdf // trace/log file - used if not blank TracerFile string // 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 // contains filtered or unexported fields }
PdfRenderer is the struct to manage conversion of a markdown object to PDF format.
func NewPdfRenderer ¶
func NewPdfRenderer(orientation, paperSize, fontDir string) *PdfRenderer
NewPdfRenderer creates and configures an PdfRenderer object, which satisfies the BlackFriday Renderer interface.
Any the parameters may be blank, with the defaults being "portrait", "A4", "."
It is not safe to use instances in more than one goroutine.
func (*PdfRenderer) Output ¶ added in v0.3.0
func (r *PdfRenderer) Output(w io.Writer) error
Output renders PDF content to a writer.
func (*PdfRenderer) Process ¶
func (r *PdfRenderer) Process(markdown []byte) *PdfRenderer
Process sets the markdown source and must be called prior to ToFile or Output.
func (*PdfRenderer) RenderFooter ¶
func (r *PdfRenderer) RenderFooter(w io.Writer, ast *bf.Node)
RenderFooter is not supported.
func (*PdfRenderer) RenderHeader ¶
func (r *PdfRenderer) RenderHeader(w io.Writer, ast *bf.Node)
RenderHeader is not supported.
func (*PdfRenderer) RenderNode ¶
func (r *PdfRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.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) ToFile ¶ added in v0.3.0
func (r *PdfRenderer) ToFile(pdfFile string) error
ToFile renders to a PDF file.
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 gofpdf API