Skip to content

Instantly share code, notes, and snippets.

@RANUX
Created February 24, 2018 21:01
Show Gist options
  • Save RANUX/2d66641eaca6502078f919ad1af701c0 to your computer and use it in GitHub Desktop.
Save RANUX/2d66641eaca6502078f919ad1af701c0 to your computer and use it in GitHub Desktop.
Compile *.md files to single or multiple pdf files with Python
import sys
import os
import re
from shellpython.helpers import Dir
'''
Compile *.md files to single or multiple pdf files
Requirments:
Python 3
pip install shellpy
Node
Before use install node and then run: npm install -g markdown-pdf
apt-get install gs # linux
brew install gs # mac
'''
# get dest dir like "up\ \&\ going/"
dst_dir = sys.argv[1]
dst_dir_name = re.sub(r"&|/|\ |\\", "_", dst_dir)
print(dst_dir_name)
with Dir(dst_dir):
result = `ls
if result:
mdfiles = filter(lambda fname: os.path.splitext(fname)[1] == '.md', result)
converted = []
for mdfile in mdfiles:
result = `markdown-pdf -f A4 {mdfile}
if result:
print('Converting %s done' % mdfile)
converted.append(os.path.splitext(mdfile)[0]+'.pdf')
files = ' '.join(converted)
print('Join converted files: %s' % files)
result = `gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile={dst_dir_name}.pdf {files}
if result:
print('Convert done in to %s' % dst_dir_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment