Pandoc与Emoji和象形文字

9

有没有一种方法可以从包含Unicode、表情符号和象形文字的Markdown文件中获取PDF?

我正在运行这个测试:

echo ':smile: → ★   ⚠️' | pandoc -f markdown+emoji --latex-engine=xelatex -o foo.pdf

所有系统字体都可用。

到目前为止,我得到的最佳结果是使用:

我尝试了一些不同的 pandoc 输入格式(-f / --from) 和 LaTeX 引擎,但都没有成功。是否存在完美的字体,或者有关如何实现这一点的任何建议?

2个回答

7
前几天我在寻找解决这个问题的方法,但没有找到任何...所以我开发了自己的解决方案:Pandoc 过滤器,用于在生成的 PDF 中包含表情符号
然而,这里有一些妥协和限制:
  • 我需要使用一个template.tex文件,因此如果您已经使用了一个模板,您将不得不将我的模板内容与您的模板合并。这可能会很麻烦...但我标记了对 Pandoc 生成的原始template.tex所做的更改。至少这可以帮助一点。
  • 我没有使用过Lua语言来创建过滤器。所以我尝试了Python,但它并没有像我期望的那样工作,因为我无法找到将 Unicode 表情符号代码点转换为可处理的内容的库。最终,我使用Javascript来制作它,因此需要NodeJS,还必须安装一些 NPM 包。
  • 我使用InkScape将在线源中的 SVG 转换为 PDF,以便将其插入到 PDF 中。这是必要的,因为 LaTeX 无法识别 SVG 图像格式。
  • 目前,我还没有时间实现过滤器的自动安装。查看 Git 存储库,并从那里使用它...这是我能给出的最好建议。有一个示例脚本可以将readme.md转换为 pdf,我还提交了生成的 PDF
  • 我不知道它是否适用于xelatexlualatex或除pdflatex之外的任何引擎,因为我只有时间使用 Pandoc 的默认引擎pdflatex 进行测试。

使用表情符号过滤器

先决条件:PandocNodeJS

  1. Checkout the repository

  2. Install NPM packages that are needed:

    npm install
    
  3. Run Pandoc, passing the filter, the template, the correct input format, the emoji source, and so on. The following is the command used to compile the example.pdf from the readme.md:

    pandoc --template="template.tex" -o example.pdf readme.md \
        --filter=emoji_filter.js -M emoji=noto-emoji --from gfm \
        -V links-as-notes=true -V colorlinks -V urlcolor=NavyBlue
    
    • --template="template.tex" indicate the template.tex file present in the repository
    • -o example.pdf represents the output filename
    • readme.md is the input filename
    • --filter=emoji_filter.js indicate the emoji filter script filename
    • -M emoji=noto-emoji is a metadata parameter that the emoji filter reads to know what kind of emoji you want. There are two options at this moment: noto-emoji and twemoji. You can see a full emoji list at unicode.org.
    • --from gfm is the input format, gfm means GitHub Flavored Markdown. I use it because it can convert emojis in the format :__name__: to unicode code-points, that my filter can recognize.
    • the other parameters are not meaningful for this answer... they are used to adjust how links appear in the final PDF.
我希望这个答案和表情符号过滤器能够帮助你!如有进一步问题,请在评论中提问...也许如果你认为我应该发布并解释代码的某些部分,我真的不知道哪些部分可以在简洁的答案中有所帮助,因为代码相当复杂。无论如何,欢迎自由提问 =)

这似乎是个好的解决方案 @Miguel Angelo!我刚刚克隆了你的存储库并执行了你的示例,结果如下:`/usr/bin/env: ‘node\r’: 没有那个文件或目录 运行过滤器emoji_filter.js时发生错误: 过滤器返回了错误状态码127`我的环境是Ubuntu 20.04 LTS,NodeJs v14.18.1,pandoc 2.5,inkscape 0.92.5和TexLive 2021(希望我没漏掉什么)。你能告诉我问题出在哪里吗? - Greenfly77

2

我知道这是一个旧的问题,但我曾经使用pdf转换器遇到过太多痛苦的时刻。对于表情符号,我使用pandocmarkdown转换为html,并使用twemoji将表情符号转换为svg,并使用以下metadata.yml:

header-includes: |
    <link rel="stylesheet" href="./style.css">
    <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script src="https://twemoji.maxcdn.com/v/latest/twemoji.min.js" crossorigin="anonymous"></script>
    <script>
        $(document).ready
        (
          function()
          {
                twemoji.parse(document.body);
          }
        );
    </script>

在CSS中:
.emoji {                
  display: inline-block;
  width: 1em;           
  height: 1em;          
  vertical-align: -.1em;
}                       

构建HTML:

pandoc -s my_doc.md  -o my_doc.html --metadata-file metadata.yml --from markdown+emoji

然后我使用Chromium来制作我的PDF文件。

chromium --headless --disable-gpu --print-to-pdf=my_doc.pdf my_doc.html --print-to-pdf-no-header

但是可以使用pandoc html转pdf,对我来说只是一个更好的javascript引擎。

这是我发现的处理表情符号、pdf和markdown的最聪明和最简单的方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接