import os from markdown_pdf import MarkdownPdf, Section import fitz docs_dir = r"c:/Users/lanfr144/Documents/DOPRO1/Antigravity/Food/docs" md_file = os.path.join(docs_dir, "Installation_Guide.md") with open(md_file, "r", encoding="utf-8") as f: md_content = f.read() # Clean paths root_dir = r"c:/Users/lanfr144/Documents/DOPRO1/Antigravity/Food" md_content = md_content.replace("file:///" + root_dir + "/docs/", "") md_content = md_content.replace("file:///" + root_dir + "/", "../") user_css = """ * { color: #1a1a1a !important; } body { font-family: 'Helvetica', 'Arial', sans-serif !important; color: #1a1a1a !important; background-color: #ffffff !important; } h1, h2, h3, h4, h5, h6, h1 *, h2 *, h3 *, h4 *, h5 *, h6 * { color: #000000 !important; margin-top: 18px !important; margin-bottom: 8px !important; } pre { background-color: #f8f9fa !important; border: 1px solid #e9ecef !important; padding: 2px !important; margin-top: 8px !important; margin-bottom: 16px !important; border-radius: 3px !important; font-family: 'Courier New', 'Courier', monospace !important; font-size: 10pt !important; white-space: pre-wrap !important; word-break: break-all !important; } pre code, pre * { font-family: 'Courier New', 'Courier', monospace !important; font-size: 10pt !important; color: #212529 !important; background-color: #f8f9fa !important; white-space: pre-wrap !important; word-break: break-all !important; } code { font-family: 'Courier New', 'Courier', monospace !important; font-size: 10pt !important; color: #b02a37 !important; background-color: #f8f9fa !important; padding: 2px 4px !important; border-radius: 3px !important; white-space: pre-wrap !important; word-break: break-all !important; } a, a * { color: #0d6efd !important; } blockquote, blockquote * { color: #555555 !important; border-left: 4px solid #ccc !important; padding-left: 10px !important; } table, tr, td, th, table * { color: #1a1a1a !important; border-color: #cccccc !important; } th { background-color: #f2f2f2 !important; } ul, li { list-style-type: disc !important; } """ pdf = MarkdownPdf() section = Section(md_content, paper_size="A4", root=docs_dir) pdf.add_section(section, user_css=user_css) doc = pdf._make_doc() total_pages = len(doc) print("Before drawing, Page 2 text length:", len(doc[1].get_text())) image_path = os.path.join(docs_dir, 'am.png') doc_title = "Local Food AI - Detailed Installation and Deployment Guide" r_text = "DOPRO1" l_footer = "lanfr144" fontsize = 8 fontname = "helv" for page_idx in range(total_pages): page = doc[page_idx] width = page.rect.width height = page.rect.height if os.path.exists(image_path): image_rect = fitz.Rect(48, 12, 48 + 16, 12 + 16) page.insert_image(image_rect, filename=image_path) m_width = fitz.get_text_length(doc_title, fontname=fontname, fontsize=fontsize) r_width = fitz.get_text_length(r_text, fontname=fontname, fontsize=fontsize) footer_text = f"Page {page_idx + 1} of {total_pages}" f_width = fitz.get_text_length(footer_text, fontname=fontname, fontsize=fontsize) page.insert_text(fitz.Point((width - m_width) / 2, 22), doc_title, fontname=fontname, fontsize=fontsize, color=(0.5, 0.5, 0.5)) page.insert_text(fitz.Point(width - 48 - r_width, 22), r_text, fontname=fontname, fontsize=fontsize, color=(0.5, 0.5, 0.5)) page.insert_text(fitz.Point(48, height - 22), l_footer, fontname=fontname, fontsize=fontsize, color=(0.5, 0.5, 0.5)) page.insert_text(fitz.Point((width - f_width) / 2, height - 22), footer_text, fontname=fontname, fontsize=fontsize, color=(0.5, 0.5, 0.5)) print("After drawing (in memory), Page 2 text length:", len(doc[1].get_text())) doc.save("scratch/test_both.pdf", clean=True, garbage=3, deflate=True) doc.close() doc2 = fitz.open("scratch/test_both.pdf") print("After saving, Page 2 text length:", len(doc2[1].get_text())) doc2.close()