Pandoc with Emoji and Pictographs
Asked Answered
D

2

11

Is there a way to get a PDF from a Markdown file which contains unicode, emoji and pictographs ?

I'm running this test:

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

With all my system fonts.

Best result I got so far was with:

I've tried some different pandoc input format (-f / --from) and latex engine without success. Is there a perfect font out there or any recommendations on how to achieve that ?

Dorrie answered 4/2, 2020 at 12:5 Comment(2)
As per the following really in depth explanation: overleaf.com/learn/latex/Articles/… Alas xelatex isn't able to deal with most of the emoji fonts that we actually want to use. The workaround I've succesfully used is tex.stackexchange.com/questions/572212/…Loiretcher
@AlexanderKellett how do you integrate/import the LaTeX commands with your pandoc workflow?Bartizan
F
7

A few days ago I was looking for a solution for this exact problem, but couldn't find any... so I developed my own solution: a Pandoc filter to include emojis in the generated PDF.

There are some compromises and limitations though:

  • I had to use a template.tex file, so if you already use a template, you'll have to merge pieces from my template with yours. That could be a real pain... but I flagged the changes that I made to the original template.tex generated by Pandoc. At least this can help a little.
  • I have no experience with Lua language to make a filter. So I tried Python, and it didn't work as I expected, because I couldn't find a library to translate the unicode emoji code-points to something I could work with. Finally, I made it using Javascript, so NodeJS is required. Also, some NPM packages must be installed.
  • I used InkScape to convert SVG from online sources, to PDF, so that they could be inserted into the PDF. This is necessary because LaTeX was not recognizing the SVG image format.
  • At this moment, I didn't have time yet to implement an automatic install of the filter. Checkout the Git repository, and use it from there... it's the best advice I can give. There is a an example script to convert the readme.md to pdf. I also committed the resulting PDF.
  • I don't know if it will work with xelatex or lualatex or anything othar than pdflatex, because I only had time to test it using the default engine for Pandoc, that is pdflatex.

Using the Emoji filter

Prerequisites: Pandoc and NodeJS

  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.

I hope this answer, and the emoji filter will help! Any further question, please ask in the comment... maybe if you think that I should post and explain some parts of the code. I really don't know what parts could be helpful in a concise answer since the code is quite complicated.

Anyway, feel free to ask =)

Floatstone answered 27/4, 2020 at 22:52 Comment(1)
This does seem to be a nice solution @Miguel Angelo! I just cloned into your repository and executed your example and it results in: /usr/bin/env: ‘node\r’: No such file or directory Error running filter emoji_filter.js: Filter returned error status 127 My environment is Ubuntu 20.04 LTS, NodeJs v14.18.1, pandoc 2.5, inkscape 0.92.5 and TexLive 2021 (hope I didn't miss anything here). Can you tell me what my problem is?Ease
N
3

I know is an old ask, but I've had too painful time with pdf converter. For the emojis I use pandoc to convert markdown in html and twemoji to convert emoji in svg with this 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>

And in css

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

build the html:

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

After that I use chromium to make my pdf with

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

But can work with pandoc html to pdf, just a better engine for javascript to me.

This is the smarter and simplest way I fine to deal with emojis, pdf and markdown.

Nous answered 15/2, 2022 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.