• Stars
    star
    117
  • Rank 301,828 (Top 6 %)
  • Language
    Go
  • License
    MIT License
  • Created over 3 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A PDF renderer for the goldmark markdown parser.

goldmark-pdf

goldmark-pdf is a renderer for goldmark that allows rendering to PDF.

goldmark-pdf screenshot

Reference

See https://pkg.go.dev/github.com/stephenafamo/goldmark-pdf

Usage

Care has been taken to match the semantics of goldmark and its extensions.

The PDF renderer can be initiated with pdf.New() and the returned value satisfies goldmark's renderer.Renderer interface, so it can be passed to goldmark.New() using the goldmark.WithRenderer() option.

markdown := goldmark.New(
    goldmark.WithRenderer(pdf.New()),
)

Options can also be passed to pdf.New(), the options interface to be satisfied is:

// An Option interface is a functional option type for the Renderer.
type Option interface {
	SetConfig(*Config)
}

Here is the Config struct that is to be modified:

type Config struct {
	Context context.Context

	PDF PDF

	// A source for images
	ImageFS fs.FS

	// All other options have sensible defaults
	Styles Styles

	// A cache for the fonts
	FontsCache fonts.Cache

	// For debugging
	TraceWriter io.Writer

	NodeRenderers util.PrioritizedSlice
}

Some helper functions for adding options are already provided. See option.go

An example with some more options:

goldmark.New(
    goldmark.WithRenderer(
        pdf.New(
            pdf.WithTraceWriter(os.Stdout),
            pdf.WithContext(context.Background()),
            pdf.WithImageFS(os.DirFS(".")),
            pdf.WithLinkColor("cc4578"),
            pdf.WithHeadingFont(pdf.GetTextFont("IBM Plex Serif", pdf.FontLora)),
            pdf.WithBodyFont(pdf.GetTextFont("Open Sans", pdf.FontRoboto)),
            pdf.WithCodeFont(pdf.GetCodeFont("Inconsolata", pdf.FontRobotoMono)),
        ),
    ),
)

Fonts

The fonts that can be used in the PDF are based on the Font struct

// Represents a font.
type Font struct {
	CanUseForText bool
	CanUseForCode bool

	Category string
	Family   string

	FileRegular    string
	FileItalic     string
	FileBold       string
	FileBoldItalic string

	Type fontType
}

To be used for text, a font should have regular, italic, bold and bold-italic styles. Each of these has to be loaded separately.

To ease this process, variables have been generated for all the Google fonts that have these styles. For example:

var FontRoboto = Font{
	CanUseForCode:  false,
	CanUseForText:  true,
	Category:       "sans-serif",
	Family:         "Roboto",
	FileBold:       "700",
	FileBoldItalic: "700italic",
	FileItalic:     "italic",
	FileRegular:    "regular",
	Type:           fontTypeGoogle,
}

For codeblocks, if any other style is missing, the regular font is used in place.

var FontMajorMonoDisplay = Font{
	CanUseForCode:  true,
	CanUseForText:  false,
	Category:       "monospace",
	Family:         "Major Mono Display",
	FileBold:       "regular",
	FileBoldItalic: "regular",
	FileItalic:     "regular",
	FileRegular:    "regular",
	Type:           fontTypeGoogle,
}

When loading the fonts, they are downloaded on the fly using the fonts.

If you'd like to use a font outside of these, you should pass your own font struct which have been loaded into the PDF object you set in the Config. Be sure to set the FontType to FontTypeCustom so that we do not attempt to download it.

Contributing

Here's a list of things that I'd love help with:

  • More documentation
  • Testing
  • Finish the (currently buggy) implementation based on gopdf

License

MIT

Author

Stephen Afam-Osemene

More Repositories

1

bob

SQL query builder and ORM/Factory generator for Go with support for PostgreSQL, MySQL and SQLite
Go
717
star
2

orchestra

Orchestra is a library to manage long running go processes.
Go
152
star
3

nginx-proxy-load-balancer

Complete proxy with much simpler configuration
Go
106
star
4

scan

Scan provides the ability to to scan sql rows directly to any defined structure.
Go
69
star
5

laravel-2fa-starter

A laravel application with 2fa added to the registration and login flows
PHP
57
star
6

kronika

Kronika adds some extra utility around the standard time package. It does not have any other external dependencies.
Go
36
star
7

adonisjs-docker

A docker image for adonis.js applications
Shell
33
star
8

javascript-autocomplete-demo

JavaScript
27
star
9

isbot

Detect bots/crawlers/spiders using the user agent string.
Go
18
star
10

golangci-server

Go
15
star
11

audio-slideshow

Make audio based slideshows
JavaScript
13
star
12

go-site-boilerplate

a boilerplate for building webapps with golang
Go
13
star
13

woocommerce-global-cart

Global cart for woocommerce on WordPress multisite installs
PHP
13
star
14

boilingfactory

BoilingFactory is a CLI tool that generates factories for models generated by sqlboiler. https://github.com/volatiletech/sqlboiler.
Smarty
12
star
15

ci-bot

Go
12
star
16

boilingseed

BoilingSeed is a CLI tool that helps generate database seeding helpers with sqlboiler. https://github.com/volatiletech/sqlboiler.
Smarty
9
star
17

fakedb

fakedb registers a fake database driver named test for... testing.
Go
7
star
18

eventbus

Go
6
star
19

janus

A modular framework for Go web applications
Go
6
star
20

demo-site-hng-challenge

Source code for a nice UI concept
HTML
5
star
21

knowledgebase

knowldegebase is a tool to quickly start a knowledge base server or add one to a go web app.
Go
5
star
22

go-sql-builder-benchmarks

Benchmarking Golang SQL query builders
Go
5
star
23

sqlparser

Go Package to parse SQL statements for MySQL and SQLite
ANTLR
4
star
24

crdbstore

Gorilla's Session store implementation with cockroachDB
Go
3
star
25

typesql

SQL Query Builder for Go
Go
3
star
26

expense-tracker

Go
2
star
27

manticore-docker

A docker image for manticore search
2
star
28

audio-slideshow-wp-plugin

PHP
1
star
29

spa-nginx

Dockerfile
1
star
30

web-components

TypeScript
1
star
31

authboss-oauth1

Oauth1 module for Authboss (github.com/volatiletech/authboss)
Go
1
star
32

packr-migrate

A driver to read migration files for golang-migrate from packr boxes
Go
1
star
33

signup-login-ui-concept

Source code for a nice UI concept
JavaScript
1
star
34

jsctags-docker

docker image for jsctags. Tag js files without Node or NPM
Shell
1
star
35

at-ussd-airtime

PHP
1
star