mdtopdf

package module
v1.5.3 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2023 License: MIT Imports: 19 Imported by: 14

README

mdtopdf

CI GoDoc License

Introduction: Markdown to PDF

This package depends on two other packages:

  • The BlackFriday v2 parser to read the markdown source
  • The fpdf packace to generate the PDF

Both of the above are documented at Go Docs.

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.

2019-09-23: It appears that the BlackFriday project is no longer active. There is a fork gomarkdown/markdown that I may be able to move to in the future if needed.

Supported Markdown

The supported elements of markdown are:

  • Emphasized and strong text
  • Headings 1-6
  • Ordered and unordered lists
  • Nested lists
  • Images
  • Tables (but see limitations below)
  • Links
  • Code blocks and backticked text

How to use of non-Latin fonts/languages is documented in a section below.

Limitations and Known Issues

  1. It is common for Markdown to include HTML. HTML is treated as a "code block". There is no attempt to convert raw HTML to PDF.

  2. Github-flavored Markdown permits strikethough using tildes. This is not supported at present by fpdf as a font style.

  3. The markdown link title, which would show when converted to HTML as hover-over text, is not supported. The generated PDF will show the actual URL that will be used if clicked, but this is a function of the PDF viewer.

  4. Currently all levels of unordered lists use a dash for the bullet. This is a planned fix; see here.

  5. Definition lists are not supported (not sure that markdown supports them -- I need to research this)

  6. The following text features may be tweaked: font, size, spacing, styile, fill color, and text color. These are exported and available via the Styler struct. Note that fill color only works if the text is ouput using CellFormat(). This is the case for: tables, codeblocks, and backticked text.

  7. Tables are supported, but no attempt is made to ensure fit. You can, however, change the font size and spacing to make it smaller. See example.

Installation

To install the package, run the usual go get:

$ go get github.com/mandolyte/mdtopdf

You can also install the md2pdf binary directly onto your $GOBIN dir with:

$ go install github.com/mandolyte/mdtopdf/cmd/md2pdf@latest

Syntax highlighting

mdtopdf supports colourised output via the gohighlight module.

For examples, see testdata/Markdown Documentation - Syntax.text and testdata/Markdown Documentation - Syntax.pdf

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 md2pdf.go -i test.md -o test.pdf

To benefit from Syntax highlighting, invoke thusly:

$ go run md2pdf.go -i syn_test.md -s /path/to/syntax_files -o test.pdf

This repo has the gohighlight module configured as a submodule so if you clone with --recursive, you will have the highlight dir in its root. Alternatively, you may issue the below to update an existing clone:

git submodule update --remote

Note 1: the cmd folder has an example for the syntax highlighting. See the script run_syntax_highlighting.sh. This example assumes that the folder with the syntax files is located at relative location: ../../../jessp01/gohighlight/syntax_files.

Note 2: when annotating the code block to specify the language, the annotation name must match syntax base filename.

Additional options
  -i string
    	Input text filename; default is os.Stdin
  -o string
    	Output PDF filename; required
  -s string
    	Path to github.com/jessp01/gohighlight/syntax_files
  --new-page-on-hr
    	Interpret HR as a new page; useful for presentations
  --page-size string
    	[A3 | A4 | A5] (default "A4")
  --theme string
    	[light|dark] (default "light")
  --title string
    	Presentation title
  --author string
    	Author; used if -footer is passed
  --font-file string
    	path to font file to use
  --font-name string
    	Font name ID; e.g 'Helvetica-1251'
  --unicode-encoding string
    	e.g 'cp1251'
  --with-footer
    	Print doc footer (author  title  page number)
  --help
    	Show usage message

For example, the below will:

  • Set the title to My Grand Title
  • Set Random Bloke as the author (used in the footer)
  • Set the dark theme
  • Start a new page when encountering a HR (---); useful for creating presentations
  • Print a footer (author name, title, page number)
$ go run md2pdf.go  -i /path/to/md \
    -o /path/to/pdf --title "My Grand Title" --author "Random Bloke" \
    --theme dark --new-page-on-hr --with-footer

Using non-ASCII Glyphs/Fonts

In order to use a non-ASCII language there are a number things that must be done. The PDF generator must be configured WithUnicodeTranslator:

// https://en.wikipedia.org/wiki/Windows-1251
pf := mdtopdf.NewPdfRenderer("", "", *output, "trace.log", mdtopdf.WithUnicodeTranslator("cp1251")) 

In addition, this package's Styler must be used to set the font to match that is configured with the PDF generator.

A complete working example may be found for Russian in the cmd folder nameed russian.go.

For a full example, run:

$ go run md2pdf.go -i russian.md -o russian.pdf \
    --unicode-encoding cp1251 --font-file helvetica_1251.json --font-name Helvetica_1251

Note to Self

In order to update pkg.go.dev with latest release, the following will do the trick. Essentially, it is creating a module and then running the go get command for the desired release. Using the proxy will have the side effect of updating the info on the go pkg web site.

$ pwd
/home/cecil/Downloads
$ mkdir tmp
$ cd tmp
$ ls
$ go mod init example.com/mypkg
go: creating new go.mod: module example.com/mypkg
$ cat go.mod 
module example.com/mypkg

go 1.20
$ GOPROXY=https://proxy.golang.org GO111MODULE=on go get github.com/mandolyte/mdtopdf@v1.4.1
go: added github.com/go-pdf/fpdf v0.8.0
go: added github.com/jessp01/gohighlight v0.21.1-7
go: added github.com/mandolyte/mdtopdf v1.4.1
go: added github.com/russross/blackfriday/v2 v2.1.0
go: added gopkg.in/yaml.v2 v2.4.0

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 fpdf 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/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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

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 added in v1.5.0

func Colorlookup(s string) Color

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
	// contains filtered or unexported fields
}

PdfRenderer is the struct to manage conversion of a markdown object to PDF format.

func NewPdfRenderer

func NewPdfRenderer(orient, papersz, pdfFile, tracerFile string, opts []RenderOption, theme Theme) *PdfRenderer

NewPdfRenderer creates and configures an PdfRenderer object, which satisfies the Renderer interface.

func NewPdfRendererWithDefaultStyler added in v1.2.0

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 *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) Run added in v1.4.1

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) SetDarkTheme added in v1.5.0

func (r *PdfRenderer) SetDarkTheme()

SetDarkTheme sets theme to 'dark'

func (*PdfRenderer) SetLightTheme added in v1.5.0

func (r *PdfRenderer) SetLightTheme()

SetLightTheme sets theme to 'light'

func (*PdfRenderer) SetPageBackground added in v1.5.0

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) UpdateBlockquoteStyler added in v1.3.1

func (r *PdfRenderer) UpdateBlockquoteStyler()

UpdateBlockquoteStyler - update Blockquote fill styler

func (*PdfRenderer) UpdateCodeStyler added in v1.3.1

func (r *PdfRenderer) UpdateCodeStyler()

UpdateCodeStyler - update code fill styler

func (*PdfRenderer) UpdateParagraphStyler added in v1.2.0

func (r *PdfRenderer) UpdateParagraphStyler(defaultStyler Styler)

UpdateParagraphStyler - update with default styler

type RenderOption added in v1.4.1

type RenderOption func(r *PdfRenderer)

RenderOption allows to define functions to configure the renderer

func IsHorizontalRuleNewPage added in v1.4.1

func IsHorizontalRuleNewPage(value bool) RenderOption

IsHorizontalRuleNewPage if true, will start a new page when encountering a HR (---). Useful for presentations.

func SetSyntaxHighlightBaseDir added in v1.4.1

func SetSyntaxHighlightBaseDir(path string) RenderOption

SetSyntaxHighlightBaseDir path to https://github.com/jessp01/gohighlight/tree/master/syntax_files

func WithUnicodeTranslator added in v1.4.1

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 Theme added in v1.5.0

type Theme int

Theme [light|dark]

const (
	// DARK const
	DARK Theme = 1
	// LIGHT const
	LIGHT Theme = 2
)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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