PDF Output: Add _WIDTH_PIXELS_ and _HEIGHT_PIXELS_ variables to know the width and height of the header/footer area in templates

This commit is contained in:
Kovid Goyal 2024-10-11 14:39:22 +05:30
parent c6527e830f
commit 864a759ff6
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 12 additions and 4 deletions

View File

@ -917,6 +917,8 @@ footers, documented below:
level section level section
* ``_TOP_LEVEL_SECTION_PAGENUM_`` - the page number of the current page * ``_TOP_LEVEL_SECTION_PAGENUM_`` - the page number of the current page
within the current top level section within the current top level section
* ``_WIDTH_PIXELS_`` - the width of the header/footer area in pixels
* ``_HEIGHT_PIXELS_`` - the height of the header/footer area in pixels
.. note:: When adding headers and footers make sure you set the page top and .. note:: When adding headers and footers make sure you set the page top and
bottom margins to large enough values, under the :guilabel:`PDF Output` bottom margins to large enough values, under the :guilabel:`PDF Output`

View File

@ -924,6 +924,7 @@ def add_header_footer(manager, opts, pdf_doc, container, page_number_display_map
body.attrib.pop('id', None) body.attrib.pop('id', None)
body.set('style', reset_css) body.set('style', reset_css)
job = job_for_name(container, name, Margins(0, 0, 0, 0), page_layout) job = job_for_name(container, name, Margins(0, 0, 0, 0), page_layout)
page_layout = job[1]
def m(tag_name, text=None, style=None, **attrs): def m(tag_name, text=None, style=None, **attrs):
ans = root.makeelement(XHTML(tag_name), **attrs) ans = root.makeelement(XHTML(tag_name), **attrs)
@ -1008,6 +1009,9 @@ def add_header_footer(manager, opts, pdf_doc, container, page_number_display_map
toplevel_toc_map = stack_to_map(create_toc_stack(tc())) toplevel_toc_map = stack_to_map(create_toc_stack(tc()))
toplevel_pagenum_map, toplevel_pages_map = page_counts_map(tc()) toplevel_pagenum_map, toplevel_pages_map = page_counts_map(tc())
dpi = 96 # dont know how to query Qt for this, seems to be the same on all platforms
def pt_to_px(pt): return int(pt * dpi / 72)
def create_container(page_num, margins): def create_container(page_num, margins):
style = { style = {
'page-break-inside': 'avoid', 'page-break-inside': 'avoid',
@ -1025,11 +1029,11 @@ def add_header_footer(manager, opts, pdf_doc, container, page_number_display_map
'overflow': 'hidden', 'overflow': 'hidden',
'background-color': 'unset', 'background-color': 'unset',
} }
ans = m('div', style=style, id=f'p{page_num}') ans = m('div', style=style, id=f'p{page_num}')
return ans return ans
def format_template(template, page_num, height): def format_template(template, page_num, height, margins):
div_width_px = pt_to_px(page_layout.paintRectPoints().width() - margins.left - margins.right)
template = template.replace('_TOP_LEVEL_SECTION_PAGES_', str(toplevel_pagenum_map[page_num - 1])) template = template.replace('_TOP_LEVEL_SECTION_PAGES_', str(toplevel_pagenum_map[page_num - 1]))
template = template.replace('_TOP_LEVEL_SECTION_PAGENUM_', str(toplevel_pages_map[page_num - 1])) template = template.replace('_TOP_LEVEL_SECTION_PAGENUM_', str(toplevel_pages_map[page_num - 1]))
template = template.replace('_TOTAL_PAGES_', str(pages_in_doc)) template = template.replace('_TOTAL_PAGES_', str(pages_in_doc))
@ -1038,6 +1042,8 @@ def add_header_footer(manager, opts, pdf_doc, container, page_number_display_map
template = template.replace('_AUTHOR_', prepare_string_for_xml(pdf_metadata.author, True)) template = template.replace('_AUTHOR_', prepare_string_for_xml(pdf_metadata.author, True))
template = template.replace('_TOP_LEVEL_SECTION_', prepare_string_for_xml(toplevel_toc_map[page_num - 1])) template = template.replace('_TOP_LEVEL_SECTION_', prepare_string_for_xml(toplevel_toc_map[page_num - 1]))
template = template.replace('_SECTION_', prepare_string_for_xml(page_toc_map[page_num - 1])) template = template.replace('_SECTION_', prepare_string_for_xml(page_toc_map[page_num - 1]))
template = template.replace('_WIDTH_PIXELS_', str(div_width_px))
template = template.replace('_HEIGHT_PIXELS_', str(pt_to_px(height)))
troot = parse(template, namespace_elements=True) troot = parse(template, namespace_elements=True)
ans = last_tag(troot)[0] ans = last_tag(troot)[0]
style = ans.get('style') or '' style = ans.get('style') or ''
@ -1060,9 +1066,9 @@ def add_header_footer(manager, opts, pdf_doc, container, page_number_display_map
div = create_container(page_num, margins) div = create_container(page_num, margins)
body.append(div) body.append(div)
if header_template: if header_template:
div.append(format_template(header_template, page_num, margins.top)) div.append(format_template(header_template, page_num, margins.top, margins))
if footer_template: if footer_template:
div.append(format_template(footer_template, page_num, margins.bottom)) div.append(format_template(footer_template, page_num, margins.bottom, margins))
container.commit() container.commit()
# print(container.raw_data(name)) # print(container.raw_data(name))