简体   繁体   English

使用 mkdocs 将包含美人鱼图的 markdown 文件渲染为组合的 PDF 文件

[英]Render markdown files contain mermaid diagrams to a combined PDF file using mkdocs

Currently, I'm using mkdocs-material to use mermaid diagrams, configured as follows (in mkdocs.yml ):目前,我正在使用mkdocs-material来使用美人鱼图,配置如下(在mkdocs.yml中):

...
markdown_extensions:
- pymdownx.superfences:
    custom_fences:
      - name: mermaid
        class: mermaid
...

However, I encounter troubles with PDF exporting.但是,我在 PDF 导出时遇到了麻烦。

I have tried several plugins.我试过几个插件。 Most of them depend on Weasy Print and have problems with javascript parts or mermaid diagrams (didn't render and still in code block's style).他们中的大多数都依赖于 Weasy Print 并且在 javascript 部分或美人鱼图(未呈现并且仍然是代码块的样式)方面存在问题。 There is one plugin ( mkdocs-pdf-with-js-plugin ) that prints pages in an easy and simple way which uses browser to do the job.有一个插件 ( mkdocs-pdf-with-js-plugin ) 可以使用浏览器以一种简单易行的方式打印页面。 However, it doesn't contain the combined feature (combine all pages into a single PDF file) that I need asmkdocs-pdf-export-plugin package.但是,它不包含我需要的combined功能(将所有页面组合成一个 PDF 文件)作为mkdocs-pdf-export-plugin package。

Is there any other plugins that support exporting PDF with mermaid diagrams and combine feature?是否有其他插件支持导出 PDF 美人鱼图和组合功能?

My current workaround我目前的解决方法

Run: ENABLE_PDF_EXPORT=1 mkdocs build .运行: ENABLE_PDF_EXPORT=1 mkdocs build Each markdown file will be exported to a PDF file.每个 markdown 文件将导出到一个 PDF 文件。

Then, I will define the order of all PDFs when merging into one unique file by putting the PDF name from top to bottom:然后,我将通过从上到下放置 PDF 名称来定义所有 PDF 合并到一个唯一文件时的顺序:

In chapters.txt :chapters.txt中:

A.pdf
B.pdf
C.pdf
...

Then run the following script.然后运行以下脚本。 Remember that this script is just a hint of what I have done, it has not been completed yet and has not run "as is".请记住,这个脚本只是我所做工作的一个提示,它尚未完成,也没有“按原样”运行。

# ================================================================================================
# Move all pdfs from "site" (the output dir of pdf exporting) to the scripts/pdf_export/pdfs
# ================================================================================================
find site -name "*.pdf" -exec mv {} scripts/pdf_export/pdfs \;

cd scripts/pdf_export/pdfs

# ================================================================================================
# Merge all pdfs into one single pdf file wrt the file name's order in chapters.txt
# ================================================================================================
# REMEMBER to put the chapters.txt into scripts/pdf_export/pdfs.
# Install: https://www.pdflabs.com/tools/pdftk-server/
# Install for M1 only: https://stackoverflow.com/a/60889993/6563277 to avoid the "pdftk: Bad CPU type in executable" on Mac
pdftk $(cat chapters.txt) cat output book.pdf

# ================================================================================================
# Add page numbers
# ================================================================================================
# Count pages https://stackoverflow.com/a/27132157/6563277
pageCount=$(pdftk book.pdf dump_data | grep "NumberOfPages" | cut -d":" -f2)

# Turn back to scripts/pdf_export
cd ..

# https://stackoverflow.com/a/30416992/6563277
# Create an overlay pdf file containing only page numbers
gs -o pagenumbers.pdf    \
   -sDEVICE=pdfwrite        \
   -g5950x8420              \
   -c "/Helvetica findfont  \
       12 scalefont setfont \
       1 1  ${pageCount} {      \
       /PageNo exch def     \
       450 20 moveto        \
       (Page ) show         \
       PageNo 3 string cvs  \
       show                 \
       ( of ${pageCount}) show  \
       showpage             \
       } for"

# Blend pagenumbers.pdf with the original pdf file
pdftk pdfs/book.pdf              \
  multistamp pagenumbers.pdf \
  output final_book.pdf

However, we need other customization like table of contents, book cover, and author section, ... All the above steps are just merging and adding page nums.然而,我们需要其他定制,如目录、书籍封面和作者部分,...以上所有步骤只是合并和添加页码。 Lots of things to do.有很多事情要做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 django 将 Markdown 文件渲染为 HTML? - How to render markdown file to HTML using django? 使用 Python 合并 PDF - 合并时关闭 PDF 文件 - Using Python to combine PDFs - Closing PDF files when they are combined 如何使用 mkdocs 将.md 文件自动添加到 output html 文件中? - How to add .md files automatically to the output html file(s) with mkdocs? 无法让 Markdown 在 Flask 中渲染(使用 Flask-Markdown 扩展) - Cannot Get Markdown to Render in Flask (Using the Flask-Markdown Extension) Python:使用文件包含的数字重命名文件夹中的所有文件 - Python: rename all files in a folder using numbers that file contain 使用两个CSV文件中的匹配列值来创建包含合并数据的新文件 - Using matching column values in two CSV files to create a new file with combined data 如果链接不包含.pdf,如何测试链接目标是否为pdf文件 - How to test if a link target is a pdf file if the link does not contain .pdf 如何使用 python-markdown 从 Markdown 文件中获取元数据? - How to get the metadata from a Markdown file using python-markdown? 使用python从服务器获取一组zip文件作为zip文件 - Getting set of pdf files as a zip file from a server using python 使用 Python PyPDF2 合并 PDF 文件并命名新文件 - Merging PDF files using Python PyPDF2 and naming new file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM