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.dirname(__file__))
44 CONFIG_PATH = os.path.join(RUNTIME_PATH, 'fop-config.xml')
46 fop_factory = FopFactory.newInstance()
47 fop_factory.setUserConfig(File(CONFIG_PATH));
49 xfrm_factory = TransformerFactory()
51 wl2fo_tmplt = xfrm_factory.newTemplates(StreamSource(\
52 File(os.path.join(RUNTIME_PATH, "xslt", "wl2fo.xslt"))))
54 normalize_tmplt = xfrm_factory.newTemplates(StreamSource(\
55 File(os.path.join(RUNTIME_PATH, "xslt", "normalize.xslt"))))
58 def process_file(filename):
60 base, ext = os.path.splitext(filename)
61 print "Transforming %s (%s)... " % (base, os.path.abspath(filename)) ,
63 source = File(filename)
65 if not source.canRead():
66 print "can't read source. :("
69 dest = File(base + '.pdf')
70 dest_stream = FileOutputStream(dest)
71 agent = fop_factory.newFOUserAgent()
73 # configure user agent & factory
74 fop = fop_factory.newFop(MimeConstants.MIME_PDF, agent, dest_stream)
77 normalize_xfrm = xfrm_factory.newTransformerHandler(normalize_tmplt)
78 wl2fo_xfrm = xfrm_factory.newTransformerHandler(wl2fo_tmplt)
79 normalize_xfrm.setResult(SAXResult(wl2fo_xfrm))
80 wl2fo_xfrm.setResult(SAXResult(fop.getDefaultHandler()))
83 filtered = ByteArrayOutputStream()
85 # pre-fetch and prepare
86 with open(filename, 'rb') as input_file:
87 for line in input_file:
88 if line.endswith('/\n'):
89 filtered.write(line[:-2] + '<br />\n')
93 source = ByteArrayInputStream(filtered.toByteArray())
95 xfrm = xfrm_factory.newTransformer()
97 xfrm.transform(StreamSource(source), SAXResult(normalize_xfrm));
98 except (XPathException, ValidationException), exc:
99 print "exception: %s" % exc
104 # print some stuff for debuging
109 Usage: book2pdf.py file [file...]"""
111 if __name__ == '__main__':
112 print "WLML To PDF converter. Copyright © Łukasz Rekucki under GPLv3 License."
114 if len(sys.argv) == 1:
117 for filename in sys.argv[1:]:
118 process_file(filename)