test_header_footer.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import fitz
  2. import os
  3. doc = fitz.open()
  4. page = doc.new_page()
  5. width = page.rect.width
  6. height = page.rect.height
  7. doc_title = "Local Food AI - Detailed Installation and Deployment Guide"
  8. r_text = "DOPRO1"
  9. l_footer = "lanfr144"
  10. footer_text = "Page 1 of 1"
  11. fontsize = 8
  12. fontname = "helv"
  13. image_path = r"c:/Users/lanfr144/Documents/DOPRO1/Antigravity/Food/docs/am.png"
  14. try:
  15. # Test drawing image
  16. if os.path.exists(image_path):
  17. image_rect = fitz.Rect(48, 12, 48 + 16, 12 + 16)
  18. page.insert_image(image_rect, filename=image_path)
  19. print("Image inserted successfully!")
  20. else:
  21. print("Image file not found!")
  22. # Calculate widths
  23. m_width = fitz.get_text_length(doc_title, fontname=fontname, fontsize=fontsize)
  24. r_width = fitz.get_text_length(r_text, fontname=fontname, fontsize=fontsize)
  25. f_width = fitz.get_text_length(footer_text, fontname=fontname, fontsize=fontsize)
  26. # 1. Middle header (centered)
  27. page.insert_text(
  28. fitz.Point((width - m_width) / 2, 22),
  29. doc_title,
  30. fontname=fontname,
  31. fontsize=fontsize,
  32. color=(0.5, 0.5, 0.5)
  33. )
  34. # 2. Right header
  35. page.insert_text(
  36. fitz.Point(width - 48 - r_width, 22),
  37. r_text,
  38. fontname=fontname,
  39. fontsize=fontsize,
  40. color=(0.5, 0.5, 0.5)
  41. )
  42. # 3. Left footer
  43. page.insert_text(
  44. fitz.Point(48, height - 22),
  45. l_footer,
  46. fontname=fontname,
  47. fontsize=fontsize,
  48. color=(0.5, 0.5, 0.5)
  49. )
  50. # 4. Middle footer (centered)
  51. page.insert_text(
  52. fitz.Point((width - f_width) / 2, height - 22),
  53. footer_text,
  54. fontname=fontname,
  55. fontsize=fontsize,
  56. color=(0.5, 0.5, 0.5)
  57. )
  58. doc.save("scratch/test_header_footer.pdf", clean=True, garbage=3, deflate=True)
  59. doc.close()
  60. print("PDF saved successfully!")
  61. except Exception as e:
  62. print("Error:", e)