Export Multiple Markdown Files to PDF?

What is the best way to export multiple Markdown files in a folder to (individual) PDF files?

I installed pandoc but I can’t figure out how to:

a) export multiple files to multiple PDF files. The manual seems to focus on merging multiple Markdown files into a single PDF file.

b) keep the same file name - input to output.

I tried this:for f in *.md; do pandoc “$f” -o “${f%.md}.pdf”; done

But I get an error:

andoc(45574,0x207810e00) malloc: *** set a breakpoint in malloc_error_break to debug

pandoc: pdflatex: createProcess: runInteractiveProcess: exec: resource exhausted (Cannot allocate memory)

1 Like

I was looking at Marked 2 support pages to see if it could do it.

https://marked2app.com/help/Exporting.html

Brett Terpstra suggested a script using wkhtmltopdf in combination with Pandoc.

Marked does not support converting an entire folder of markdown files to PDF AFIK.

I saw that later on in the Marked forum, already amended my answer :slight_smile:

did find this script, maybe it helps?

Tried running the script but got these errors:

conv.sh: line 7: syntax error near unexpected token `newline'
conv.sh: line 7: `<!DOCTYPE html>'

you’d hope a use case like this would be easier to solve :slight_smile:
Can’t imagine you’re the first one to think of this?

Looks like the error that I am running into is related to pandoc and M1 MacBook Air

https://groups.google.com/g/pandoc-discuss/c/tWWIEgW94U0/m/yKMfldtYBgAJ

Is this something that you need to do once, or a lot?

The easiest way to do this, of course, is to combine the Markdown files into one file, and then just change that file into a PDF.

For example, if you have a bunch of .md files:


cat *.md > combined.txt

pandoc --from=markdown --to=pdf --output=“combined.pdf” “combined.txt"


Make sure that combined.txt does not exist already, because the cat command will overwrite it.

Of course, if you need to combine them in a different order, you could do that too:


cat 5.md 3.md 1.md 2.md 4.md > combined.txt

pandoc --from=markdown --to=pdf --output=“combined.pdf” “combined.txt"


If needed, you can edit combined.txt before running the pandoc command, but again that gets back to how frequently are you doing this vs how automatic does it need to be.

Thanks but I need each markdown file to be converted into its own PDF file.

1 Like

For M1 Macs, this works

for f in *.md; do pandoc --pdf-engine=/Library/TeX/texbin/pdflatex "$f" -s -o "${f%.txt}.pdf"; done 

Source: https://groups.google.com/g/pandoc-discuss/c/tWWIEgW94U0/m/HyJ7IqX2BgAJ

2 Likes

Ugh. Sorry. Reading comprehension fail.