0

I want to convert markdown file as pdf in python. how can i do this my library downloads are ok but there is always a problem, i tried the code below and it gives an error

import markdown2
import pdfkit



filename = "sample.md"
mode = "r"

with open(filename, mode) as file:
    markdown_text = file.read()

html_text = markdown2.markdown(markdown_text)

pdfkit.from_string(html_text, "output.pdf")

I wrote the code I tried in the description.

jps
  • 20,041
  • 15
  • 75
  • 79
yusuf
  • 9
  • 3
  • *there is always a problem,... it gives an error* - This is not a useful problem description. Tell us exactly what you tried and which errors/problems you got. Guessing isn't really efficient. I recommend to read [ask]. And please edit the post to add information, don't add information in comments. – jps Mar 31 '23 at 09:21
  • @yusuf: have you checked this ? https://github.com/ljpengelen/markdown-to-pdf – inkredusk Mar 31 '23 at 09:37

1 Answers1

2

This is the exception being thrown:

OSError: No wkhtmltopdf executable found

Browsing the internet, I see it as a common problem. It requires you to download some sort of binaries and pass it in the parameter. Like so:

import markdown2
import pdfkit

filename = "sample.md"
mode = "r"

with open(filename, mode) as file:
    markdown_text = file.read()

html_text = markdown2.markdown(markdown_text)
config = pdfkit.configuration(wkhtmltopdf="/path/to/wkhtmltopdf.exe")
pdfkit.from_string(html_text, "output.pdf", configuration=config)

Please, check this post for further information on how to download this wkhtmltopdf.exe.

Plus, it looks like you can eventually use the pdfkit.from_file() instead of .from_string(). It may give you a more correct output with a lighter code, but it still requires you to download this wkhtmltopdf.exe.

Something like this:

import pdfkit

filename = "sample.md"
mode = "r"

with open(filename, mode) as file:
    config = pdfkit.configuration(wkhtmltopdf="/path/to/wkhtmltopdf.exe")
    pdfkit.from_file(file, "output.pdf", configuration=config)

It should work, but I didn't test the output.

  • Also, learn to give importance to Exceptions being thrown. They're the fastest way to understand where and what is the problem. Search for them in the internet, and if you find nothing useful then post it on StackOverflow along with your code. – Marco Frag Delle Monache Mar 31 '23 at 10:10
  • I did everything requested to give this error: File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pdfkit/configuration.py", line 38, in __init__ raise IOError('No wkhtmltopdf executable found: "%s"\n' OSError: No wkhtmltopdf executable found: "/path/to/wkhtmltopdf.exe" If this file exists please check that this process can read it or you can pass path to it manually in method call, check README. Ot – yusuf Apr 01 '23 at 07:32
  • Please make sure you specified the correct path to where you downloaded the file. /path/to/wkhtmltopdf.exe was just an example. – Marco Frag Delle Monache Apr 01 '23 at 09:27
  • @yusuf just to remind, if this did the trick then feel free to mark it as solution – Marco Frag Delle Monache Apr 11 '23 at 07:49