Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

How to convert a markdown file to PDF?

+3
−0

How to convert a markdown file to PDF? The final PDF output should represent the rendered markdown file. Bonus points if the method also preserves colored emojis.

Both CLI and GUI solutions are welcome.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

2 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+4
−0

It feels like it has become a bit trickier to do so, but pandoc should do most of the work, here. For example, on a Debian-like system for installation commands:

sudo apt install pandoc
pandoc --from=markdown --to=pdf in.md --output out.pdf

You probably don't need to specify the file types, but it seems better to get in the habit of over-specifying things.

I say that it'll do "most of the work," because pandoc actually farms out the final PDF conversion by producing an intermediate step, LaTeX by default. My machine, for example, doesn't have the LaTeX engine that it expects and I couldn't figure out what to install quickly enough for this response, so I needed to find an alternative and specify it, settling on weasyprint, which takes its side-trip through HTML instead of LaTeX.

sudo apt install weasyprint
pandoc --from=markdown --to=pdf in.md --output out.pdf --pdf-engine=weasyprint

This does preserve the emoji, which comes as a pleasant surprise to me, too.

In any case, this gets you a fairly boring PDF, looking a lot like a web page, which might do the trick. From there, though, you can go in more sophisticated directions. If, for example, you install texlive-latex-base, that will (should?) serve as the default LaTeX-to-PDF converter. You don't want to start there, because you'll need to configure all the LaTeX templates and stylesheets to get a valid conversion, which becomes an annoying tradeoff of how much effort you want to put in versus how much you want to change the look of the PDF.

I haven't seen any GUI interfaces that save you from the command-line, but they must exist, even if they only issue the pandoc command on selected files.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Works for me (1 comment)
+1
−0

You could use the LaTeX markdown package to create a pdf.

To make sure that the coloured emoji are visible:

  • choose a unicode-aware engine like lualatex
  • choose a font which has coloured emoji, e.g. Noto Color Emoji

Assuming your markdown file is called test.md, the create a .tex with the content as shown below and compile it with lualatex --shell-escape <name of .tex file>.

\documentclass{article} 

\usepackage{markdown}
\usepackage{fontspec}

% from https://tex.stackexchange.com/a/572220/36296
\directlua{luaotfload.add_fallback
   ("emojifallback",
    {
      "NotoColorEmoji:mode=harf;"
    }
   )}

\setmainfont{Arial}[RawFeature={fallback=emojifallback}]

\begin{document} 

\markdownInput{test.md} 

\end{document}

screenshot of the rendered document, showing the text "I'm a markdown file 😺"

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »