| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- import fitz
- import os
- doc = fitz.open()
- page = doc.new_page()
- width = page.rect.width
- height = page.rect.height
- doc_title = "Local Food AI - Detailed Installation and Deployment Guide"
- r_text = "DOPRO1"
- l_footer = "lanfr144"
- footer_text = "Page 1 of 1"
- fontsize = 8
- fontname = "helv"
- image_path = r"c:/Users/lanfr144/Documents/DOPRO1/Antigravity/Food/docs/am.png"
- try:
- # Test drawing image
- if os.path.exists(image_path):
- image_rect = fitz.Rect(48, 12, 48 + 16, 12 + 16)
- page.insert_image(image_rect, filename=image_path)
- print("Image inserted successfully!")
- else:
- print("Image file not found!")
-
- # Calculate widths
- m_width = fitz.get_text_length(doc_title, fontname=fontname, fontsize=fontsize)
- r_width = fitz.get_text_length(r_text, fontname=fontname, fontsize=fontsize)
- f_width = fitz.get_text_length(footer_text, fontname=fontname, fontsize=fontsize)
-
- # 1. Middle header (centered)
- page.insert_text(
- fitz.Point((width - m_width) / 2, 22),
- doc_title,
- fontname=fontname,
- fontsize=fontsize,
- color=(0.5, 0.5, 0.5)
- )
-
- # 2. Right header
- page.insert_text(
- fitz.Point(width - 48 - r_width, 22),
- r_text,
- fontname=fontname,
- fontsize=fontsize,
- color=(0.5, 0.5, 0.5)
- )
-
- # 3. Left footer
- page.insert_text(
- fitz.Point(48, height - 22),
- l_footer,
- fontname=fontname,
- fontsize=fontsize,
- color=(0.5, 0.5, 0.5)
- )
-
- # 4. Middle footer (centered)
- page.insert_text(
- fitz.Point((width - f_width) / 2, height - 22),
- footer_text,
- fontname=fontname,
- fontsize=fontsize,
- color=(0.5, 0.5, 0.5)
- )
-
- doc.save("scratch/test_header_footer.pdf", clean=True, garbage=3, deflate=True)
- doc.close()
- print("PDF saved successfully!")
- except Exception as e:
- print("Error:", e)
|