从 Markdown 中的美人鱼图生成正确渲染的 SVG 以进行 Pandoc PDF 转换

问题描述 投票:0回答:1

我正在尝试使用 Pandoc 将包含美人鱼图的 Markdown 文件转换为 PDF。但是,我遇到了 SVG 渲染问题,类似于此处报告的问题。我探索了两种方法:

  1. 使用mermaid-cli

    mmdc -i "path\to\file.md" --outputFormat=svg -o "path\to\file\file-svg.md" -c "path\to\mermaid-config.json"
    
  2. mermaid-filter 与 Pandoc 一起使用,正如我在here所解释的那样。

问题是生成的 SVG 在 Inkscape 或 Pandoc 似乎使用的

rsvg-convert.exe
中无法正确渲染。

我尝试使用 Mermaid 配置文件来禁用 HTML 标签,如建议的 herehere:

{
  "flowchart": {
    "htmlLabels": false
  },
  "sequence": {
    "htmlLabels": false
  },
  "gantt": {
    "htmlLabels": false
  },
  "class": {
    "htmlLabels": false
  },
  "state": {
    "htmlLabels": false
  },
  "er": {
    "htmlLabels": false
  }
}

但是,这似乎并不能解决问题。

是否可以将 Mermaid JSON 配置文件与

mermaid-cli
mermaid-filter
一起使用来生成可由 Inkscape 和
rsvg-convert.exe
正确渲染的 SVG?如果是的话,怎么办?

我的最终目标是拥有一个可靠的流程,使用 Pandoc 将带有美人鱼图的 Markdown 文件转换为 PDF,确保所有图表都正确呈现。

svg markdown pandoc mermaid pdf-conversion
1个回答
0
投票

好吧,我想经过几周的努力解决这个问题,我有了一个可行的解决方案,而且比我最初预期的要容易得多。我的意思是,在我发布 SVG 之前,我正在编写一些代码来解析 Markdown,SVG 并不是

mmdc
可以导出的唯一矢量化格式,它还可以导出 PDF!因此,对于那些最终来到此页面的可怜的灵魂,这是我的 cmd 批处理脚本:

@echo off
setlocal enabledelayedexpansion

REM Check if a file path is provided
if "%~1"=="" (
    echo Please provide a Markdown file path as an argument.
    exit /b 1
)

REM Get the full path, filename, and directory of the input file
set "fullpath=%~f1"
set "filename=%~n1"
set "filedir=%~dp1"
set "scriptdir=%~dp0"

REM Create (or recreate) a temporary folder
set "tempfolder=%filedir%%filename%"
if exist "%tempfolder%" rmdir /s /q "%tempfolder%"
mkdir "%tempfolder%"

REM Run Mermaid-cli to preprocess the Markdown file
echo Processing Markdown file with Mermaid...
call mmdc -i "%fullpath%" --outputFormat=pdf --pdfFit -o "%tempfolder%\%filename%.md"
if %errorlevel% neq 0 (
    echo Error: Mermaid-cli failed with exit code %errorlevel%
    goto :cleanup
)
echo Mermaid-cli processing complete.

@REM exit /b

REM Change to the temporary directory
echo Changing to temporary directory...
pushd "%tempfolder%"
if %errorlevel% neq 0 (
    echo Error: Failed to change to temporary directory
    goto :cleanup
)
echo Changed to temporary directory successfully.

REM Convert the processed Markdown to PDF using Pandoc
echo Converting to PDF...
call pandoc "%filename%.md" -f markdown-implicit_figures -o "..\%filename%.pdf"
if %errorlevel% neq 0 (
    echo Error: Pandoc failed with exit code %errorlevel%
    popd
    goto :cleanup
)
echo PDF conversion complete.

REM Change back to the original directory
echo Changing back to original directory...
popd
echo Changed back to original directory.

:cleanup
REM Clean up the temporary folder
echo Cleaning up...
@REM rmdir /s /q "%tempfolder%"

if exist "%filedir%%filename%.pdf" (
    echo Conversion complete. Output file: %filedir%%filename%.pdf
) else (
    echo Error: PDF file was not created.
)

REM Pause to see any error messages
pause

简而言之,这个脚本的作用是:

  • 使用
    mmdc
    预处理原始Markdown文件并将Mermaid代码块导出为PDF
  • 使用 Pandoc,如果我没记错的话,使用
    pdflatex
    将预处理后的 Markdown 转换为 PDF。
© www.soinside.com 2019 - 2024. All rights reserved.