Search code examples
asciidoctorasciidoctor-pdf

Can I change the font used in source blocks by Asciidoctor PDF?


Is it possible to specify a different font for use in the source blocks of a PDF generated by asciidoctor-pdf?

I'm guessing it has something to do with the theme I'm using (currently just the default), but I've checked the theming guide and can't see any configuration related to the source blocks specifically.


Solution

  • Yes it is possible and yes it has to do with the theming. You can check the default asciidoctor-pdf theming under https://github.com/asciidoctor/asciidoctor-pdf/blob/main/data/themes/default-theme.yml, you will see line 188 # code is used for source blocks (perhaps change to source or listing?), this is what you need to change.

    The simplest way is to create your own theme custom-theme.yml by extending the default theme and set the font family you want, in my example Courier, this works:

    extends: default
    font:
      catalog:
        merge: true
        Courier:
          normal: cour.ttf
          bold: courbd.ttf
          italic: couri.ttf
          bold_italic: courbi.ttf
    code:
      font_family: Courier
    

    I organize my files like this:

    yourFile.adoc
    resources/themes/custom-theme.yml
    resources/fonts/cour.ttf
    resources/fonts/courbd.ttf
    resources/fonts/couri.ttf
    resources/fonts/courbi.ttf
    

    You then need to call asciidoctor-pdf with your new theme and a link to the fonts path, something like this:

    asciidoctor-pdf -a pdf-theme=resources/themes/custom-theme.yml -a pdf-fontsdir="resources/fonts;GEM_FONTS_DIR" yourFile.adoc