Skip to content

Commit

Permalink
PDF Output: Enable using javascript inside header and footer templates
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Mar 23, 2014
1 parent bbc7535 commit 1fa5a5a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion manual/conversion.rst
Expand Up @@ -840,7 +840,7 @@ template::
This will display the title at the left and the author at the right, in a font
size smaller than the main text.

Finally, you can also use the current section in templates, as shown below::
You can also use the current section in templates, as shown below::

<p style="text-align:right">_SECTION_</p>

Expand All @@ -850,6 +850,12 @@ 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.

You can even use javascript inside the header and footer templates, for
example, the following template will cause page numbers to start at 4 instead
of 1::

<p id="pagenum" style="text-align:center;"></p><script>document.getElementById("pagenum").innerHTML = "" + (_PAGENUM_ + 3)</script>

.. 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
conversion dialog.
Expand Down
Binary file modified resources/compiled_coffeescript.zip
Binary file not shown.
6 changes: 6 additions & 0 deletions src/calibre/ebooks/oeb/display/paged.coffee
Expand Up @@ -8,6 +8,10 @@

log = window.calibre_utils.log

runscripts = (parent) ->
for script in parent.getElementsByTagName('script')
eval(script.text || script.textContent || script.innerHTML || '')

class PagedDisplay
# This class is a namespace to expose functions via the
# window.paged_display object. The most important functions are:
Expand Down Expand Up @@ -238,8 +242,10 @@ class PagedDisplay
section = py_bridge.section()
if this.header != null
this.header.innerHTML = this.header_template.replace(/_PAGENUM_/g, pagenum+"").replace(/_TITLE_/g, title+"").replace(/_AUTHOR_/g, author+"").replace(/_SECTION_/g, section+"")
runscripts(this.header)
if this.footer != null
this.footer.innerHTML = this.footer_template.replace(/_PAGENUM_/g, pagenum+"").replace(/_TITLE_/g, title+"").replace(/_AUTHOR_/g, author+"").replace(/_SECTION_/g, section+"")
runscripts(this.footer)

fit_images: () ->
# Ensure no images are wider than the available width in a column. Note
Expand Down

0 comments on commit 1fa5a5a

Please sign in to comment.