Skip to content

Commit 77acb09

Browse files
authored
Merge pull request #905 from jpphilips/markdown-to-pdf
Markdown to pdf
2 parents 7102cd2 + 258b5a0 commit 77acb09

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

markdown_to_pdf/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Markdown to pdf
2+
3+
Convert markdown file or files to pdfs
4+
5+
## Setup instructions
6+
```
7+
python -r requirements.txt
8+
```
9+
- call the script and pass the name or names of the markdown files
10+
```
11+
python markdown_to_pdf.py sample1.md sample2.md sample3.md
12+
```
13+
## Output
14+
15+
will create equivalent pdf files of the markdown files
16+
17+
## Author
18+
[JPPhilips](https://www.github.com/jpphilips)
19+
20+
21+
## Disclaimers, if any
22+
23+
if not installed, need to:
24+
- download and install wkhtmltopdf
25+
- Set download folder path in PATH Environment variables.

markdown_to_pdf/markdown_to_pdf.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import markdown
2+
import pdfkit
3+
import sys
4+
5+
args = sys.argv[1:] # get command line *args
6+
assert args, "No file/dir was provided" # raise error is no arg is passed
7+
8+
print(args)
9+
10+
html_texts = []
11+
for arg in args:
12+
with open(arg, "r", encoding="utf-8") as input_file:
13+
text = input_file.read()
14+
15+
html_texts.append(markdown.markdown(text))
16+
17+
# configuring pdfkit to point to our installation of wkhtmltopdf
18+
config = pdfkit.configuration(
19+
wkhtmltopdf=r"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe")
20+
21+
for html_text in enumerate(html_texts):
22+
print(html_text[0])
23+
filename = args[html_text[0]].split('.')[0]
24+
pdfkit.from_string(html_text[1], f'{filename}.pdf', configuration=config)

markdown_to_pdf/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Markdown==3.4.1
2+
pdfkit==1.0.0

0 commit comments

Comments
 (0)