test_css_and_headers.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import os
  2. from markdown_pdf import MarkdownPdf, Section
  3. import fitz
  4. docs_dir = r"c:/Users/lanfr144/Documents/DOPRO1/Antigravity/Food/docs"
  5. md_file = os.path.join(docs_dir, "Installation_Guide.md")
  6. with open(md_file, "r", encoding="utf-8") as f:
  7. md_content = f.read()
  8. # Clean paths
  9. root_dir = r"c:/Users/lanfr144/Documents/DOPRO1/Antigravity/Food"
  10. md_content = md_content.replace("file:///" + root_dir + "/docs/", "")
  11. md_content = md_content.replace("file:///" + root_dir + "/", "../")
  12. user_css = """
  13. * {
  14. color: #1a1a1a !important;
  15. }
  16. body {
  17. font-family: 'Helvetica', 'Arial', sans-serif !important;
  18. color: #1a1a1a !important;
  19. background-color: #ffffff !important;
  20. }
  21. h1, h2, h3, h4, h5, h6, h1 *, h2 *, h3 *, h4 *, h5 *, h6 * {
  22. color: #000000 !important;
  23. margin-top: 18px !important;
  24. margin-bottom: 8px !important;
  25. }
  26. pre {
  27. background-color: #f8f9fa !important;
  28. border: 1px solid #e9ecef !important;
  29. padding: 2px !important;
  30. margin-top: 8px !important;
  31. margin-bottom: 16px !important;
  32. border-radius: 3px !important;
  33. font-family: 'Courier New', 'Courier', monospace !important;
  34. font-size: 10pt !important;
  35. white-space: pre-wrap !important;
  36. word-break: break-all !important;
  37. }
  38. pre code, pre * {
  39. font-family: 'Courier New', 'Courier', monospace !important;
  40. font-size: 10pt !important;
  41. color: #212529 !important;
  42. background-color: #f8f9fa !important;
  43. white-space: pre-wrap !important;
  44. word-break: break-all !important;
  45. }
  46. code {
  47. font-family: 'Courier New', 'Courier', monospace !important;
  48. font-size: 10pt !important;
  49. color: #b02a37 !important;
  50. background-color: #f8f9fa !important;
  51. padding: 2px 4px !important;
  52. border-radius: 3px !important;
  53. white-space: pre-wrap !important;
  54. word-break: break-all !important;
  55. }
  56. a, a * {
  57. color: #0d6efd !important;
  58. }
  59. blockquote, blockquote * {
  60. color: #555555 !important;
  61. border-left: 4px solid #ccc !important;
  62. padding-left: 10px !important;
  63. }
  64. table, tr, td, th, table * {
  65. color: #1a1a1a !important;
  66. border-color: #cccccc !important;
  67. }
  68. th {
  69. background-color: #f2f2f2 !important;
  70. }
  71. ul, li {
  72. list-style-type: disc !important;
  73. }
  74. """
  75. pdf = MarkdownPdf()
  76. section = Section(md_content, paper_size="A4", root=docs_dir)
  77. pdf.add_section(section, user_css=user_css)
  78. doc = pdf._make_doc()
  79. total_pages = len(doc)
  80. print("Before drawing, Page 2 text length:", len(doc[1].get_text()))
  81. image_path = os.path.join(docs_dir, 'am.png')
  82. doc_title = "Local Food AI - Detailed Installation and Deployment Guide"
  83. r_text = "DOPRO1"
  84. l_footer = "lanfr144"
  85. fontsize = 8
  86. fontname = "helv"
  87. for page_idx in range(total_pages):
  88. page = doc[page_idx]
  89. width = page.rect.width
  90. height = page.rect.height
  91. if os.path.exists(image_path):
  92. image_rect = fitz.Rect(48, 12, 48 + 16, 12 + 16)
  93. page.insert_image(image_rect, filename=image_path)
  94. m_width = fitz.get_text_length(doc_title, fontname=fontname, fontsize=fontsize)
  95. r_width = fitz.get_text_length(r_text, fontname=fontname, fontsize=fontsize)
  96. footer_text = f"Page {page_idx + 1} of {total_pages}"
  97. f_width = fitz.get_text_length(footer_text, fontname=fontname, fontsize=fontsize)
  98. page.insert_text(fitz.Point((width - m_width) / 2, 22), doc_title, fontname=fontname, fontsize=fontsize, color=(0.5, 0.5, 0.5))
  99. page.insert_text(fitz.Point(width - 48 - r_width, 22), r_text, fontname=fontname, fontsize=fontsize, color=(0.5, 0.5, 0.5))
  100. page.insert_text(fitz.Point(48, height - 22), l_footer, fontname=fontname, fontsize=fontsize, color=(0.5, 0.5, 0.5))
  101. page.insert_text(fitz.Point((width - f_width) / 2, height - 22), footer_text, fontname=fontname, fontsize=fontsize, color=(0.5, 0.5, 0.5))
  102. print("After drawing (in memory), Page 2 text length:", len(doc[1].get_text()))
  103. doc.save("scratch/test_both.pdf", clean=True, garbage=3, deflate=True)
  104. doc.close()
  105. doc2 = fitz.open("scratch/test_both.pdf")
  106. print("After saving, Page 2 text length:", len(doc2[1].get_text()))
  107. doc2.close()