orzih / mkdocs-with-pdf

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

Mermaid2 not interpret in pdf #93

Open enicolas31 opened 2 years ago

enicolas31 commented 2 years ago

Hi,

Using mermaid2 to display diagrams, they are not interpreted in the pdf having an error during the build.

File mkdocs.yml

  - git-revision-date-localized:
      type: date
  - mermaid2
  - with-pdf:
      author: Me
      copyright: 2022
      cover_title: My doc
      cover_subtitle: Exemple of mermaid2
      render_js: true
      headless_chrome_path: google-chrome-stable 

Having the error :

Converting 2 files to PDF took 1.0s
Traceback (most recent call last):
  File "/usr/local/bin/mkdocs", line 8, in <module>
    sys.exit(cli())
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.10/site-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/mkdocs/__main__.py", line 187, in build_command
    build.build(config.load_config(**kwargs), dirty=not clean)
  File "/usr/local/lib/python3.10/site-packages/mkdocs/commands/build.py", line 317, in build
    config['plugins'].run_event('post_build', config=config)
  File "/usr/local/lib/python3.10/site-packages/mkdocs/plugins.py", line 104, in run_event
    result = method(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/mkdocs_with_pdf/plugin.py", line 135, in on_post_build
    self.generator.on_post_build(config, self.config['output_path'])
  File "/usr/local/lib/python3.10/site-packages/mkdocs_with_pdf/generator.py", line 142, in on_post_build
    html_string = self._render_js(soup)
  File "/usr/local/lib/python3.10/site-packages/mkdocs_with_pdf/generator.py", line 381, in _render_js
    tag.text = self._mixed_script
AttributeError: can't set attribute 'text'

Without using render_js:true the pdf is generated without diagrams but with syntax. Do you have any ideas on this error? How to fix it and generate correctly the pdf

Thanks for you help

DanielFichtner commented 2 years ago

Having the exact same issue. Workaround here: https://github.com/orzih/mkdocs-with-pdf/issues/79

enicolas31 commented 2 years ago

Thanks Daniel for your help

I did not install this library at all before, below is my new configuration:

  - pip install beautifulsoup4==4.9.3
  - pip install mkdocs==1.2.3 mkdocs-material==7.3.4 pymdown-extensions==9.0 markdown-include 
  - pip install mkdocs-mermaid2-plugin 
  - pip install mkdocs-with-pdf
  - wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
  - apt-get update 
  - apt install -y --fix-broken ./google-chrome-stable_current_amd64.deb

Now with this setup, I have a new error :

  Converting 2 files to PDF took 1.5s
  Traceback (most recent call last):
    File "/usr/local/bin/mkdocs", line 8, in <module>
      sys.exit(cli())
    File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1128, in __call__
      return self.main(*args, **kwargs)
    File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1053, in main
      rv = self.invoke(ctx)
    File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1659, in invoke
      return _process_result(sub_ctx.command.invoke(sub_ctx))
    File "/usr/local/lib/python3.10/site-packages/click/core.py", line 1395, in invoke
      return ctx.invoke(self.callback, **ctx.params)
    File "/usr/local/lib/python3.10/site-packages/click/core.py", line 754, in invoke
      return __callback(*args, **kwargs)
    File "/usr/local/lib/python3.10/site-packages/mkdocs/__main__.py", line 187, in build_command
      build.build(config.load_config(**kwargs), dirty=not clean)
    File "/usr/local/lib/python3.10/site-packages/mkdocs/commands/build.py", line 317, in build
      config['plugins'].run_event('post_build', config=config)
    File "/usr/local/lib/python3.10/site-packages/mkdocs/plugins.py", line 104, in run_event
      result = method(**kwargs)
    File "/usr/local/lib/python3.10/site-packages/mkdocs_with_pdf/plugin.py", line 135, in on_post_build
      self.generator.on_post_build(config, self.config['output_path'])
    File "/usr/local/lib/python3.10/site-packages/mkdocs_with_pdf/generator.py", line 157, in on_post_build
      render.write_pdf(abs_pdf_path)
    File "/usr/local/lib/python3.10/site-packages/weasyprint/document.py", line 1204, in write_pdf
      page.paint(stream, scale=scale)
    File "/usr/local/lib/python3.10/site-packages/weasyprint/document.py", line 809, in paint
      draw_page(self._page_box, stream)
    File "/usr/local/lib/python3.10/site-packages/weasyprint/draw.py", line 73, in draw_page
      draw_stacking_context(stream, stacking_context)
    File "/usr/local/lib/python3.10/site-packages/weasyprint/draw.py", line 186, in draw_stacking_context
      draw_stacking_context(stream, child_context)
    File "/usr/local/lib/python3.10/site-packages/weasyprint/draw.py", line 186, in draw_stacking_context
      draw_stacking_context(stream, child_context)
    File "/usr/local/lib/python3.10/site-packages/weasyprint/draw.py", line 181, in draw_stacking_context
      draw_inline_level(
    File "/usr/local/lib/python3.10/site-packages/weasyprint/draw.py", line 972, in draw_inline_level
      draw_inline_level(
    File "/usr/local/lib/python3.10/site-packages/weasyprint/draw.py", line 976, in draw_inline_level
      draw_replacedbox(stream, box)
    File "/usr/local/lib/python3.10/site-packages/weasyprint/draw.py", line 939, in draw_replacedbox
      box.replacement.draw(
    File "/usr/local/lib/python3.10/site-packages/weasyprint/images.py", line 91, in draw
      self._svg.draw(
    File "/usr/local/lib/python3.10/site-packages/weasyprint/svg/__init__.py", line 361, in draw
      self.draw_node(self.tree, size('12pt'))
    File "/usr/local/lib/python3.10/site-packages/weasyprint/svg/__init__.py", line 428, in draw_node
      self.draw_node(child, font_size, fill_stroke)
    File "/usr/local/lib/python3.10/site-packages/weasyprint/svg/__init__.py", line 428, in draw_node
      self.draw_node(child, font_size, fill_stroke)
    File "/usr/local/lib/python3.10/site-packages/weasyprint/svg/__init__.py", line 437, in draw_node
      self.fill_stroke(node, font_size)
    File "/usr/local/lib/python3.10/site-packages/weasyprint/svg/__init__.py", line 606, in fill_stroke
      fill_opacity = float(node.get('fill-opacity', 1))
  ValueError: could not convert string to float: '100%'

Any ideas? Thanks for your help

euphoria commented 2 years ago

I was having the same error as in the initial report, and for what it's worth using this workaround

Having the exact same issue. Workaround here: #79

I no longer have either of the errors pasted into this issue and other JS now works (like MathJax) but mermaid is still just printed as code blocks when setup with mkdocs-material: https://squidfunk.github.io/mkdocs-material/reference/diagrams/#configuration

krukowskid commented 1 year ago

@euphoria did you manage to get mermaid working in exported docs?

euphoria commented 1 year ago

@euphoria did you manage to get mermaid working in exported docs?

@krukowskid I don't think so (it's been awhile) but don't take that as a sign that it can't be done since I moved off the project I was working on shortly after this.

krukowskid commented 1 year ago

For anyone who fall into same problem: I have forked plugin repo and modified mkdocs_with_pdf/drivers /headless_chrome.py file to render diagrams to png before generating pdf. I decided to do it because even if I managed to generate diagrams from javascript in pdf text labels were missing because of underlying print engine + svg files.

Diagrams in static page are still rendered by javascript lib, pngs are only for pdf generation. https://gist.github.com/krukowskid/c47fc76056d1fb7b991ef628db96c96f

its using mmdc so you need to install mermaid-cli

npm install -g @mermaid-js/mermaid-cli

and install from setup.py file

python setup.py install

It is configured to replace <pre class="mermaid"><code>{mermaid_code}</code></pre> so it will work with

  - pymdownx.superfences:
      custom_fences:
        - name: mermaid
          class: mermaid
          format: !!python/name:pymdownx.superfences.fence_code_format

If you are using mermaid2 regexp search and replace needs to be adjusted.