项目作者: stephenafamo

项目描述 :
A PDF renderer for the goldmark markdown parser.
高级语言: Go
项目地址: git://github.com/stephenafamo/goldmark-pdf.git
创建时间: 2021-04-20T18:05:22Z
项目社区:https://github.com/stephenafamo/goldmark-pdf

开源协议:MIT License

下载


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.

  1. markdown := goldmark.New(
  2. goldmark.WithRenderer(pdf.New()),
  3. )

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

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

Here is the Config struct that is to be modified:

  1. type Config struct {
  2. Context context.Context
  3. PDF PDF
  4. // A source for images
  5. ImageFS fs.FS
  6. // All other options have sensible defaults
  7. Styles Styles
  8. // A cache for the fonts
  9. FontsCache fonts.Cache
  10. // For debugging
  11. TraceWriter io.Writer
  12. NodeRenderers util.PrioritizedSlice
  13. }

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

An example with some more options:

  1. goldmark.New(
  2. goldmark.WithRenderer(
  3. pdf.New(
  4. pdf.WithTraceWriter(os.Stdout),
  5. pdf.WithContext(context.Background()),
  6. pdf.WithImageFS(os.DirFS(".")),
  7. pdf.WithLinkColor("cc4578"),
  8. pdf.WithHeadingFont(pdf.GetTextFont("IBM Plex Serif", pdf.FontLora)),
  9. pdf.WithBodyFont(pdf.GetTextFont("Open Sans", pdf.FontRoboto)),
  10. pdf.WithCodeFont(pdf.GetCodeFont("Inconsolata", pdf.FontRobotoMono)),
  11. ),
  12. ),
  13. )

Fonts

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

  1. // Represents a font.
  2. type Font struct {
  3. CanUseForText bool
  4. CanUseForCode bool
  5. Category string
  6. Family string
  7. FileRegular string
  8. FileItalic string
  9. FileBold string
  10. FileBoldItalic string
  11. Type fontType
  12. }

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:

  1. var FontRoboto = Font{
  2. CanUseForCode: false,
  3. CanUseForText: true,
  4. Category: "sans-serif",
  5. Family: "Roboto",
  6. FileBold: "700",
  7. FileBoldItalic: "700italic",
  8. FileItalic: "italic",
  9. FileRegular: "regular",
  10. Type: fontTypeGoogle,
  11. }

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

  1. var FontMajorMonoDisplay = Font{
  2. CanUseForCode: true,
  3. CanUseForText: false,
  4. Category: "monospace",
  5. Family: "Major Mono Display",
  6. FileBold: "regular",
  7. FileBoldItalic: "regular",
  8. FileItalic: "regular",
  9. FileRegular: "regular",
  10. Type: fontTypeGoogle,
  11. }

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