Skip to content

Instantly share code, notes, and snippets.

@rememberlenny
Created April 20, 2023 00:05
Show Gist options
  • Save rememberlenny/3fcdbd25cb20fa7a58babc3616482f11 to your computer and use it in GitHub Desktop.
Save rememberlenny/3fcdbd25cb20fa7a58babc3616482f11 to your computer and use it in GitHub Desktop.
import markdown2
from xhtml2pdf import pisa
import os
def create_pdf_from_markdown(markdown_file, output_filename):
# Read the content of the Markdown file
with open(markdown_file, 'r', encoding='utf-8') as md_file:
markdown_content = md_file.read()
# Convert the Markdown content to HTML with markdown2
html_content = markdown2.markdown(markdown_content)
# Create a PDF file from the HTML content
with open(output_filename, 'wb') as pdf_file:
pisa_status = pisa.CreatePDF(html_content, dest=pdf_file)
# Check if there was an error during PDF generation
if not pisa_status.err:
print('PDF file successfully generated:', output_filename)
else:
print('Error generating PDF file:', pisa_status.err)
# Specify the path to the input Markdown file
markdown_file = 'input_markdown.md'
# Specify the name of the output PDF file
output_filename = 'output_from_markdown.pdf'
# Call the function to create the PDF file from the Markdown file
create_pdf_from_markdown(markdown_file, output_filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment