XMLtoPDFBook is a tool that allows you to create simple PDF ebooks from XML content. It is a part of the xtpodf toolkit.
You can now create PDF ebooks with XMLtoPDFBook, that include a Table of Contents (TOC).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | # XMLtoPDFBook2.py
# A program to convert a book in XML text format to a PDF book.
# Uses xtopdf and ReportLab.
# Author: Vasudev Ram - http://www.dancingbison.com
# Version: v0.2
#--------------------------------------------------------------------
# imports
import sys
import os
import string
import time
from PDFWriter import PDFWriter
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
#--------------------------------------------------------------------
# global variables
sysargv = None
#--------------------------------------------------------------------
def debug(message):
sys.stderr.write(message + "\n")
#--------------------------------------------------------------------
def get_xml_filename(sysargv):
return sysargv[1]
#--------------------------------------------------------------------
def get_pdf_filename(sysargv):
return sysargv[2]
#--------------------------------------------------------------------
def XMLtoPDFBook():
debug("Entered XMLtoPDFBook()")
global sysargv
# Get command-line arguments.
xml_filename = get_xml_filename(sysargv)
debug("xml_filename: " + xml_filename)
pdf_filename = get_pdf_filename(sysargv)
debug("pdf_filename: " + pdf_filename)
# Parse the XML file.
try:
tree = ET.ElementTree(file=xml_filename)
debug("tree = " + repr(tree))
except Exception:
sys.stderr.write("Error: caught exception in ET.ElementTree(file)")
sys.exit(1)
# Get the tree root.
root = tree.getroot()
debug("root.tag = " + root.tag)
if root.tag != "book":
debug("Error: Root tag is not 'book'")
sys.exit(1)
# Initialize the table of contents list.
toc = []
# Initialize the chapters list.
chapters = []
# Traverse the tree, extracting needed data into variables.
debug("-" * 60)
for root_child in root:
if root_child.tag != "chapter":
debug("Error: root_child tag is not 'chapter'")
sys.exit(1)
chapter = root_child
#debug(chapter.text)
chapters.append(chapter.text)
try:
chapter_name = chapter.attrib['name']
except KeyError:
chapter_name = ""
toc.append(chapter_name)
debug("-" * 60)
# Create and set some fields of a PDFWriter.
pw = PDFWriter(pdf_filename)
pw.setFont("Courier", 12)
pw.setFooter("Generated by XMLtoPDFBook. Copyright 2013 Vasudev Ram")
# Write the TOC.
pw.setHeader("Table of Contents")
chapter_num = 0
debug("Chapter names")
for chapter_name in toc:
debug(chapter_name)
chapter_num += 1
pw.writeLine(str(chapter_num) + ": " + chapter_name)
pw.savePage()
# Write the chapters.
chapter_num = 0
for chapter in chapters:
chapter_num += 1
pw.setHeader("Chapter " + str(chapter_num) + ": " + toc[chapter_num - 1])
lines = chapter.split("\n")
for line in lines:
pw.writeLine(line)
pw.savePage()
pw.close()
debug("Exiting XMLtoPDFBook()")
def main():
debug("Entered main()")
global sysargv
sysargv = sys.argv
# Check for right number of arguments.
if len(sysargv) != 3:
sys.exit(1)
XMLtoPDFBook()
debug("Exiting main()")
#--------------------------------------------------------------------
if __name__ == "__main__":
try:
main()
except Exception, e:
sys.stderr.write("Error: caught Exception" + str(e))
sys.exit(1)
#--------------------------------------------------------------------
|
More details are available here:
http://jugad2.blogspot.in/2013/12/added-table-of-contents-feature-to.html
This allows you to include a Table of Contents (TOC) at the beginning of the PDF ebooks you create with XMLtoPDFBook.
XMLtoPDFBook is a part of the xtopdf toolkit: