2 # -*- coding: utf-8 -*-
4 # Copyright © 2009,2010 Łukasz Rekucki
6 # This file is part of WL2PDF
8 # WL2PDF is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # WL2PDF is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with WL2PDF. If not, see <http://www.gnu.org/licenses/>.
21 from __future__ import with_statement
27 from java.lang import *
29 from net.sf.saxon import TransformerFactoryImpl as TransformerFactory
30 from javax.xml.transform import Transformer
32 from javax.xml.transform import Source
33 from javax.xml.transform import Result
34 from javax.xml.transform.stream import StreamSource, StreamResult
35 from javax.xml.transform.sax import SAXResult
37 from org.apache.fop.fo import ValidationException
38 from net.sf.saxon.trans import XPathException
40 from org.apache.fop.apps import *;
42 RUNTIME_PATH = os.path.abspath(os.path.join(\
43 os.path.dirname(__file__), '..', 'runtime'))
45 CONFIG_PATH = os.path.join(RUNTIME_PATH, 'fop-config.xml')
47 fop_factory = FopFactory.newInstance()
48 fop_factory.setUserConfig(File(CONFIG_PATH));
50 xfrm_factory = TransformerFactory()
52 wl2fo_tmplt = xfrm_factory.newTemplates(StreamSource(\
53 File(os.path.join(RUNTIME_PATH, "xslt", "wl2fo.xslt"))))
55 normalize_tmplt = xfrm_factory.newTemplates(StreamSource(\
56 File(os.path.join(RUNTIME_PATH, "xslt", "normalize.xslt"))))
59 def process_file(filename):
61 base, ext = os.path.splitext(filename)
62 print "Transforming %s (%s)... " % (base, os.path.abspath(filename)) ,
64 source = File(filename)
66 if not source.canRead():
67 print "can't read source. :("
70 dest = File(base + '.pdf')
71 dest_stream = FileOutputStream(dest)
72 agent = fop_factory.newFOUserAgent()
74 # configure user agent & factory
75 fop = fop_factory.newFop(MimeConstants.MIME_PDF, agent, dest_stream)
78 normalize_xfrm = xfrm_factory.newTransformerHandler(normalize_tmplt)
79 wl2fo_xfrm = xfrm_factory.newTransformerHandler(wl2fo_tmplt)
80 normalize_xfrm.setResult(SAXResult(wl2fo_xfrm))
81 wl2fo_xfrm.setResult(SAXResult(fop.getDefaultHandler()))
84 filtered = ByteArrayOutputStream()
86 # pre-fetch and prepare
87 with open(filename, 'rb') as input_file:
88 for line in input_file:
89 if line.endswith('/\n'):
90 filtered.write(line[:-2] + '<br />\n')
94 source = ByteArrayInputStream(filtered.toByteArray())
96 xfrm = xfrm_factory.newTransformer()
98 xfrm.transform(StreamSource(source), SAXResult(normalize_xfrm));
99 except (XPathException, ValidationException), exc:
100 print "exception: %s" % exc
105 # print some stuff for debuging
110 Usage: book2pdf.py file [file...]"""
112 if __name__ == '__main__':
113 print "WLML To PDF converter (c) 2009 Łukasz Rekucki. GPLv3 License."
115 if len(sys.argv) == 1:
118 for filename in sys.argv[1:]:
119 process_file(filename)