asciidoctor / asciidoctor-pdf

:page_with_curl: Asciidoctor PDF: A native PDF converter for AsciiDoc based on Asciidoctor and Prawn, written entirely in Ruby.
https://docs.asciidoctor.org/pdf-converter/latest/
MIT License
1.14k stars 501 forks source link

Unable to produce PDF due to prawn font/ image related issue #2415

Closed wxguy closed 1 year ago

wxguy commented 1 year ago

Here is the version of asciidoctor-pdf I am using:

$ asciidoctor-pdf -v                                                                                                                  
Asciidoctor PDF 2.3.4 using Asciidoctor 2.0.18 [https://asciidoctor.org]
Runtime Environment (ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]) (lc:UTF-8 fs:UTF-8 in:UTF-8 ex:UTF-8)

I have been using the asciidoctor-pdf for quite some time. I don't remember the earlier version of asciidoctor-pdf I was using. But recently upgraded my system hardware and installed the application fresh. Following is the command I use for generating PDF file using a custom theme file:-

$ asciidoctor-pdf -r asciidoctor-mathematical -a mathematical-format=png  --theme data/book_theme.yml -a pdf-fontsdir=data/fonts -o output/book.pdf  book._draftadoc --trace

However, it produces the following error.

/home/sundar/.rvm/gems/ruby-3.1.2/gems/prawn-2.4.0/lib/prawn/fonts/ttf.rb:277:in `/': nil can't be coerced into Float (TypeError)
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/prawn-2.4.0/lib/prawn/fonts/ttf.rb:277:in `scale_factor'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/prawn-2.4.0/lib/prawn/fonts/ttf.rb:58:in `initialize'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/prawn-2.4.0/lib/prawn/font.rb:309:in `new'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/prawn-2.4.0/lib/prawn/font.rb:309:in `load'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/prawn-2.4.0/lib/prawn/font.rb:252:in `find_font'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/prawn-2.4.0/lib/prawn/font.rb:55:in `font'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/asciidoctor-pdf-2.3.4/lib/asciidoctor/pdf/ext/prawn/extensions.rb:329:in `font'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/asciidoctor-pdf-2.3.4/lib/asciidoctor/pdf/converter.rb:178:in `convert_document'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/asciidoctor-pdf-2.3.4/lib/asciidoctor/pdf/converter.rb:152:in `convert'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/asciidoctor-2.0.18/lib/asciidoctor/document.rb:956:in `convert'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/asciidoctor-2.0.18/lib/asciidoctor/convert.rb:118:in `convert'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/asciidoctor-2.0.18/lib/asciidoctor/convert.rb:190:in `block in convert_file'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/asciidoctor-2.0.18/lib/asciidoctor/convert.rb:190:in `open'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/asciidoctor-2.0.18/lib/asciidoctor/convert.rb:190:in `convert_file'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/asciidoctor-2.0.18/lib/asciidoctor/cli/invoker.rb:129:in `block in invoke!'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/asciidoctor-2.0.18/lib/asciidoctor/cli/invoker.rb:112:in `each'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/asciidoctor-2.0.18/lib/asciidoctor/cli/invoker.rb:112:in `invoke!'
        from /home/sundar/.rvm/gems/ruby-3.1.2/gems/asciidoctor-pdf-2.3.4/bin/asciidoctor-pdf:46:in `<top (required)>'
        from /home/sundar/.rvm/gems/ruby-3.1.2/bin/asciidoctor-pdf:25:in `load'
        from /home/sundar/.rvm/gems/ruby-3.1.2/bin/asciidoctor-pdf:25:in `<main>'
        from /home/sundar/.rvm/gems/ruby-3.1.2/bin/ruby_executable_hooks:22:in `eval'
        from /home/sundar/.rvm/gems/ruby-3.1.2/bin/ruby_executable_hooks:22:in `<main>'

Here is the content of data/fonts directory:

$ ls data/fonts                                                                                                                        ─╯
cmex10.ttf                   MesloLGS_NF_Regular.ttf          mplus1mn-regular-ascii-conums.ttf  notosans-bold-subset.ttf
cmmi10.ttf                   mplus1mn-bold-ascii.ttf          mplus1mn-regular-subset.ttf        notosans-italic-subset.ttf
cmr10.ttf                    mplus1mn-bold_italic-ascii.ttf   mplus1p-regular-fallback.ttf       notoserif-bold_italic-subset.ttf
cmsy10.ttf                   mplus1mn-bold_italic-subset.ttf  msam10.ttf                         notoserif-bold-subset.ttf
MesloLGS_NF_Bold_Italic.ttf  mplus1mn-bold-subset.ttf         msbm10.ttf                         notoserif-italic-subset.ttf
MesloLGS_NF_Bold.ttf         mplus1mn-italic-ascii.ttf        notoemoji-subset.ttf               notoserif-regular-subset.ttf
MesloLGS_NF_Italic.ttf       mplus1mn-italic-subset.ttf       notosans-bold_italic-subset.ttf

and here is the custom theme font section I use:

font:
  catalog:
    # Noto Serif supports Latin, Latin-1 Supplement, Latin Extended-A, Greek, Cyrillic, Vietnamese & an assortment of symbols
    Noto Serif:
      normal: notoserif-regular-subset.ttf
      bold: notoserif-bold-subset.ttf
      italic: notoserif-italic-subset.ttf
      bold_italic: notoserif-bold_italic-subset.ttf
    # M+ 1mn supports ASCII and the circled numbers used for conums
    M+ 1mn:
      normal: mplus1mn-regular-ascii-conums.ttf
      bold: mplus1mn-bold-ascii.ttf
      italic: mplus1mn-italic-ascii.ttf
      bold_italic: mplus1mn-bold_italic-ascii.ttf
    # M+ 1p supports Latin, Latin-1 Supplement, Latin Extended, Greek, Cyrillic, Vietnamese, Japanese & an assortment of symbols
    # It also provides arrows for ->, <-, => and <= replacements in case these glyphs are missing from font
    M+ 1p Fallback:
      normal: mplus1p-regular-fallback.ttf
      bold: mplus1p-regular-fallback.ttf
      italic: mplus1p-regular-fallback.ttf
      bold_italic: mplus1p-regular-fallback.ttf
  fallbacks:
    - M+ 1p Fallback

I looked at the similar error reported at https://github.com/asciidoctor/asciidoctor-pdf/issues/2349 but it appears to be not related to it as I have not included any SVG image in my draft book. I used the same command earlier (around 4-5 months back) as well but that time, it produced PDF without error.

mojavelinux commented 1 year ago

Given the location of this error, this looks like a problem in Prawn itself (and perhaps due to bad data provided by ttfunk). Here's the code at the line in question:

def scale_factor
  @scale_factor ||= 1000.0 / @ttf.header.units_per_em
end

This is not something we can deal with in Asciidoctor PDF. You'll need to file the issue in Prawn (or more likely, ttfunk). I recommend trying to reduce this to a reproducible scenario so that the authors are able to debug it.

Another approach would be to open the fonts in FontForge and resave them. It's possible one of the fonts is malformed somehow. Sometimes reexorting it will fix it.

wxguy commented 1 year ago

Thank you for your reply. Solved the issue by replacing the font section with following which was copied from default theme:

font:
  catalog:
    # Noto Serif supports Latin, Latin-1 Supplement, Latin Extended-A, Greek, Cyrillic, Vietnamese & an assortment of symbols
    Noto Serif:
      normal: GEM_FONTS_DIR/notoserif-regular-subset.ttf
      bold: GEM_FONTS_DIR/notoserif-bold-subset.ttf
      italic: GEM_FONTS_DIR/notoserif-italic-subset.ttf
      bold_italic: GEM_FONTS_DIR/notoserif-bold_italic-subset.ttf
    # M+ 1mn supports ASCII and the circled numbers used for conums
    M+ 1mn:
      normal: GEM_FONTS_DIR/mplus1mn-regular-subset.ttf
      bold: GEM_FONTS_DIR/mplus1mn-bold-subset.ttf
      italic: GEM_FONTS_DIR/mplus1mn-italic-subset.ttf
      bold_italic: GEM_FONTS_DIR/mplus1mn-bold_italic-subset.ttf

It works now without giving me error.

mojavelinux commented 1 year ago

Glad to hear it!

Btw, if you extend the default theme, you don't have to declare the built-in fonts as it will use them by default. See https://docs.asciidoctor.org/pdf-converter/latest/theme/create-theme/#extend-default If you need to add your own fonts in addition to the default fonts, then you would use the merge: true key. See https://docs.asciidoctor.org/pdf-converter/latest/theme/font/#extend-catalog