#!/usr/bin/env python3 """ Simple Markdown to PDF converter using markdown and xhtml2pdf """ import markdown from xhtml2pdf import pisa import os def convert_md_to_pdf(input_file, output_file): """Convert markdown file to PDF""" # Read markdown file with open(input_file, 'r', encoding='utf-8') as f: md_content = f.read() # Convert markdown to HTML html = markdown.markdown(md_content, extensions=['tables', 'fenced_code', 'codehilite']) # Add CSS styling html_with_style = f"""
{html} """ # Convert HTML to PDF with encoding with open(output_file, 'wb') as pdf_file: pisa_status = pisa.CreatePDF( html_with_style.encode('utf-8'), dest=pdf_file, encoding='utf-8' ) if pisa_status.err: print(f"Error creating PDF: {pisa_status.err}") return False else: print(f"PDF created successfully: {output_file}") return True if __name__ == '__main__': input_file = r'C:\Users\alexi\Documents\projects\freelance-dashboard\TECHNICAL_REFERENCE_EN.md' output_file = r'C:\Users\alexi\Documents\projects\freelance-dashboard\TECHNICAL_REFERENCE_EN.pdf' if os.path.exists(input_file): convert_md_to_pdf(input_file, output_file) else: print(f"Input file not found: {input_file}")