mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
PDF Output: Allow use of _SECTION_ in header/footer templates
This commit is contained in:
parent
6201e2a19b
commit
a688a8018c
@ -794,6 +794,16 @@ template::
|
|||||||
This will display the title at the left and the author at the right, in a font
|
This will display the title at the left and the author at the right, in a font
|
||||||
size smaller than the main text.
|
size smaller than the main text.
|
||||||
|
|
||||||
|
Finally, you can also use the current section in templates, as shown below::
|
||||||
|
|
||||||
|
<p style="text-align:right">_SECTION_</p>
|
||||||
|
|
||||||
|
_SECTION_ is replaced by whatever the name of the current section is. These
|
||||||
|
names are taken from the metadata Table of Contents in the document (the PDF
|
||||||
|
Outline). If the document has no table of contents then it will be replaced by
|
||||||
|
empty text. If a single PDF page has multiple sections, the first section on
|
||||||
|
the page will be used.
|
||||||
|
|
||||||
.. 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 Page Setup section of the
|
bottom margins to large enough values, under the Page Setup section of the
|
||||||
conversion dialog.
|
conversion dialog.
|
||||||
|
Binary file not shown.
@ -104,11 +104,11 @@ class PDFOutput(OutputFormatPlugin):
|
|||||||
'specify a footer template, it will take precedence '
|
'specify a footer template, it will take precedence '
|
||||||
'over this option.')),
|
'over this option.')),
|
||||||
OptionRecommendation(name='pdf_footer_template', recommended_value=None,
|
OptionRecommendation(name='pdf_footer_template', recommended_value=None,
|
||||||
help=_('An HTML template used to generate footers on every page.'
|
help=_('An HTML template used to generate %s on every page.'
|
||||||
' The strings _PAGENUM_, _TITLE_ and _AUTHOR_ will be replaced by their current values.')),
|
' The strings _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_ will be replaced by their current values.')%_('footers')),
|
||||||
OptionRecommendation(name='pdf_header_template', recommended_value=None,
|
OptionRecommendation(name='pdf_header_template', recommended_value=None,
|
||||||
help=_('An HTML template used to generate headers on every page.'
|
help=_('An HTML template used to generate %s on every page.'
|
||||||
' The strings _PAGENUM_, _TITLE_ and _AUTHOR_ will be replaced by their current values.')),
|
' The strings _PAGENUM_, _TITLE_, _AUTHOR_ and _SECTION_ will be replaced by their current values.')%_('headers')),
|
||||||
])
|
])
|
||||||
|
|
||||||
def convert(self, oeb_book, output_path, input_plugin, opts, log):
|
def convert(self, oeb_book, output_path, input_plugin, opts, log):
|
||||||
|
@ -216,10 +216,11 @@ class PagedDisplay
|
|||||||
this.hf_style.innerHTML = "#pdf_page_header_#{ this.hf_uuid } .#{ cls }, #pdf_page_footer_#{ this.hf_uuid } .#{ cls } { display: none }"
|
this.hf_style.innerHTML = "#pdf_page_header_#{ this.hf_uuid } .#{ cls }, #pdf_page_footer_#{ this.hf_uuid } .#{ cls } { display: none }"
|
||||||
title = py_bridge.title()
|
title = py_bridge.title()
|
||||||
author = py_bridge.author()
|
author = py_bridge.author()
|
||||||
|
section = py_bridge.section()
|
||||||
if this.header != null
|
if this.header != null
|
||||||
this.header.innerHTML = this.header_template.replace(/_PAGENUM_/g, pagenum+"").replace(/_TITLE_/g, title+"").replace(/_AUTHOR_/g, author+"")
|
this.header.innerHTML = this.header_template.replace(/_PAGENUM_/g, pagenum+"").replace(/_TITLE_/g, title+"").replace(/_AUTHOR_/g, author+"").replace(/_SECTION_/g, section+"")
|
||||||
if this.footer != null
|
if this.footer != null
|
||||||
this.footer.innerHTML = this.footer_template.replace(/_PAGENUM_/g, pagenum+"").replace(/_TITLE_/g, title+"").replace(/_AUTHOR_/g, author+"")
|
this.footer.innerHTML = this.footer_template.replace(/_PAGENUM_/g, pagenum+"").replace(/_TITLE_/g, title+"").replace(/_AUTHOR_/g, author+"").replace(/_SECTION_/g, section+"")
|
||||||
|
|
||||||
fit_images: () ->
|
fit_images: () ->
|
||||||
# Ensure no images are wider than the available width in a column. Note
|
# Ensure no images are wider than the available width in a column. Note
|
||||||
|
@ -138,6 +138,10 @@ class PDFWriter(QObject):
|
|||||||
def author(self):
|
def author(self):
|
||||||
return self.doc_author
|
return self.doc_author
|
||||||
|
|
||||||
|
@pyqtSlot(result=unicode)
|
||||||
|
def section(self):
|
||||||
|
return self.current_section
|
||||||
|
|
||||||
def __init__(self, opts, log, cover_data=None, toc=None):
|
def __init__(self, opts, log, cover_data=None, toc=None):
|
||||||
from calibre.gui2 import is_ok_to_use_qt
|
from calibre.gui2 import is_ok_to_use_qt
|
||||||
if not is_ok_to_use_qt():
|
if not is_ok_to_use_qt():
|
||||||
@ -162,6 +166,7 @@ class PDFWriter(QObject):
|
|||||||
self.view.page().mainFrame().setScrollBarPolicy(x,
|
self.view.page().mainFrame().setScrollBarPolicy(x,
|
||||||
Qt.ScrollBarAlwaysOff)
|
Qt.ScrollBarAlwaysOff)
|
||||||
self.report_progress = lambda x, y: x
|
self.report_progress = lambda x, y: x
|
||||||
|
self.current_section = ''
|
||||||
|
|
||||||
def dump(self, items, out_stream, pdf_metadata):
|
def dump(self, items, out_stream, pdf_metadata):
|
||||||
opts = self.opts
|
opts = self.opts
|
||||||
@ -287,6 +292,25 @@ class PDFWriter(QObject):
|
|||||||
self.loop.processEvents(self.loop.ExcludeUserInputEvents)
|
self.loop.processEvents(self.loop.ExcludeUserInputEvents)
|
||||||
evaljs('document.getElementById("MathJax_Message").style.display="none";')
|
evaljs('document.getElementById("MathJax_Message").style.display="none";')
|
||||||
|
|
||||||
|
def get_sections(self, anchor_map):
|
||||||
|
sections = {}
|
||||||
|
ci = os.path.abspath(os.path.normcase(self.current_item))
|
||||||
|
if self.toc is not None:
|
||||||
|
for toc in self.toc.flat():
|
||||||
|
path = toc.abspath or None
|
||||||
|
frag = toc.fragment or None
|
||||||
|
if path is None:
|
||||||
|
continue
|
||||||
|
path = os.path.abspath(os.path.normcase(path))
|
||||||
|
if path == ci:
|
||||||
|
col = 0
|
||||||
|
if frag and frag in anchor_map:
|
||||||
|
col = anchor_map[frag]['column']
|
||||||
|
if col not in sections:
|
||||||
|
sections[col] = toc.text or _('Untitled')
|
||||||
|
|
||||||
|
return sections
|
||||||
|
|
||||||
def do_paged_render(self):
|
def do_paged_render(self):
|
||||||
if self.paged_js is None:
|
if self.paged_js is None:
|
||||||
import uuid
|
import uuid
|
||||||
@ -321,6 +345,8 @@ class PDFWriter(QObject):
|
|||||||
amap = self.bridge_value
|
amap = self.bridge_value
|
||||||
if not isinstance(amap, dict):
|
if not isinstance(amap, dict):
|
||||||
amap = {'links':[], 'anchors':{}} # Some javascript error occurred
|
amap = {'links':[], 'anchors':{}} # Some javascript error occurred
|
||||||
|
sections = self.get_sections(amap['anchors'])
|
||||||
|
col = 0
|
||||||
|
|
||||||
if self.header:
|
if self.header:
|
||||||
self.bridge_value = self.header
|
self.bridge_value = self.header
|
||||||
@ -335,6 +361,8 @@ class PDFWriter(QObject):
|
|||||||
|
|
||||||
mf = self.view.page().mainFrame()
|
mf = self.view.page().mainFrame()
|
||||||
while True:
|
while True:
|
||||||
|
if col in sections:
|
||||||
|
self.current_section = sections[col]
|
||||||
self.doc.init_page()
|
self.doc.init_page()
|
||||||
if self.header or self.footer:
|
if self.header or self.footer:
|
||||||
evaljs('paged_display.update_header_footer(%d)'%self.current_page_num)
|
evaljs('paged_display.update_header_footer(%d)'%self.current_page_num)
|
||||||
@ -348,8 +376,10 @@ class PDFWriter(QObject):
|
|||||||
evaljs('window.scrollTo(%d, 0); paged_display.position_header_footer();'%nsl[0])
|
evaljs('window.scrollTo(%d, 0); paged_display.position_header_footer();'%nsl[0])
|
||||||
if self.doc.errors_occurred:
|
if self.doc.errors_occurred:
|
||||||
break
|
break
|
||||||
|
col += 1
|
||||||
|
|
||||||
if not self.doc.errors_occurred:
|
if not self.doc.errors_occurred:
|
||||||
self.doc.add_links(self.current_item, start_page, amap['links'],
|
self.doc.add_links(self.current_item, start_page, amap['links'],
|
||||||
amap['anchors'])
|
amap['anchors'])
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user