orzih / mkdocs-with-pdf

Generate a single PDF file from MkDocs repository.
MIT License
318 stars 76 forks source link

Broken PDF generation with Weasyprint>57 #144

Open adelosa opened 1 year ago

adelosa commented 1 year ago

Python 3.11 requirements.txt and installed versions

mkdocs                      1.4.3
mkdocs-awesome-pages-plugin 2.9.1
mkdocs-macros-plugin        0.7.0
mkdocs-material             7.3.6
mkdocs-material-extensions  1.1.1
mkdocs-with-pdf             0.9.3
weasyprint                  59.0

Receiving the following error when generating PDF:

INFO     -  Rendering for PDF.
INFO     -  Output a PDF to "my.pdf".
...
Traceback (most recent call last):
  File "/env/lib64/python3.11/site-packages/mkdocs/__main__.py", line 250, in build_command
    build.build(cfg, dirty=not clean)
  File "/env/lib64/python3.11/site-packages/mkdocs/commands/build.py", line 332, in build
    config.plugins.run_event('post_build', config=config)
  File "/env/lib64/python3.11/site-packages/mkdocs/plugins.py", line 522, in run_event
    result = method(**kwargs)
             ^^^^^^^^^^^^^^^^
  File "/env/lib64/python3.11/site-packages/mkdocs_with_pdf/plugin.py", line 135, in on_post_build
    self.generator.on_post_build(config, self.config['output_path'])
  File "/env/lib64/python3.11/site-packages/mkdocs_with_pdf/generator.py", line 157, in on_post_build
    render.write_pdf(abs_pdf_path)
  File "/env/lib64/python3.11/site-packages/weasyprint/document.py", line 399, in write_pdf
    pdf = generate_pdf(self, target, zoom, **options)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/env/lib64/python3.11/site-packages/weasyprint/pdf/__init__.py", line 261, in generate_pdf
    pdf_fonts = build_fonts_dictionary(
                ^^^^^^^^^^^^^^^^^^^^^^^
  File "/env/lib64/python3.11/site-packages/weasyprint/pdf/fonts.py", line 47, in build_fonts_dictionary
    for letter, key in font.ttfont.getBestCmap().items():
                       ^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'getBestCmap'

Same run using weasyprint 58.1

INFO     -  Rendering for PDF.
INFO     -  Output a PDF to "my.pdf".
Traceback (most recent call last):
...
  File "/env/lib64/python3.11/site-packages/mkdocs/__main__.py", line 250, in build_command
    build.build(cfg, dirty=not clean)
  File "/env/lib64/python3.11/site-packages/mkdocs/commands/build.py", line 332, in build
    config.plugins.run_event('post_build', config=config)
  File "/env/lib64/python3.11/site-packages/mkdocs/plugins.py", line 522, in run_event
    result = method(**kwargs)
             ^^^^^^^^^^^^^^^^
  File "/env/lib64/python3.11/site-packages/mkdocs_with_pdf/plugin.py", line 135, in on_post_build
    self.generator.on_post_build(config, self.config['output_path'])
  File "/env/lib64/python3.11/site-packages/mkdocs_with_pdf/generator.py", line 157, in on_post_build
    render.write_pdf(abs_pdf_path)
  File "/env/lib64/python3.11/site-packages/weasyprint/document.py", line 360, in write_pdf
    pdf = generate_pdf(
          ^^^^^^^^^^^^^
  File "/env/lib64/python3.11/site-packages/weasyprint/pdf/__init__.py", line 256, in generate_pdf
    pdf_fonts = build_fonts_dictionary(pdf, document.fonts, optimize_size)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/env/lib64/python3.11/site-packages/weasyprint/pdf/fonts.py", line 50, in build_fonts_dictionary
    ratio = 1024 / font.ttfont['head'].unitsPerEm
                   ~~~~~~~~~~~^^^^^^^^
TypeError: 'NoneType' object is not subscriptable

If you revert your weasyprint to latest 57 (57.2) then the generation works. Here in case others want a quick fix.

avihaySunny commented 2 months ago

thanks!