1/* Copyright 2004-2005 the original author or authors.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15package grails.doc
16
17import javax.xml.parsers.DocumentBuilder
18import javax.xml.parsers.DocumentBuilderFactory
19
20import org.w3c.dom.Document
21import org.xhtmlrenderer.pdf.ITextRenderer
22
23class PdfBuilder {
24
25    private static final String LIVE_DOC_SITE = 'http://grails.org'
26
27    static void build(String baseDir, String grailsHome) {
28        baseDir = new File(baseDir).canonicalPath
29        build basedir: baseDir, home: grailsHome
30    }
31
32    /**
33     * Builds a PDF file from the manual's single.html file.<p>
34     * The following directories are assumed to exist:<ul>
35     * <li> $basedir/manual/guide/single.html</li>
36     * <li> $basedir/manual/css/</li>
37     * <li> $basedir/manual/img/</li>
38     * <li> $home/src/$tool/docs/style</li>
39     * </ul>
40     *
41     * The {@code options} map should have the following key/value pairs<ul>
42     * <li>basedir = points to the root directory that contains the generated manual <b>required</b></li>
43     * <li>home = points to the tool home, e.g, $grailsHome <b>required</b></li>
44     * <li>tool = name of the tool. default <tt>grails</tt></li>
45     * </ul>
46     */
47    static void build(Map options) {
48        String baseDir = new File(options.basedir).canonicalPath
49        String home = options.home
50        String tool = options.tool ?: 'grails'
51
52        File htmlFile = new File("${baseDir}/manual/guide/single.html")
53        File outputFile = new File("${baseDir}/manual/guide/single.pdf")
54        File homeFile = new File(home).canonicalFile
55        String urlBase = "file://${homeFile.absolutePath}/src/${tool}/docs/style"
56
57        String xml = createXml(htmlFile, "${baseDir}/manual")
58        createPdf xml, outputFile, urlBase
59    }
60
61    private static String createXml(File htmlFile, String base) {
62        String xml = htmlFile.text
63
64        // tweak main css so it doesn't get ignored
65        xml = xml.replace('media="screen"', 'media="print"')
66
67        // fix inner anchors
68        xml = xml.replaceAll('<a href="../guide/single.html', '<a href="')
69        // fix image refs to absolute paths
70        xml = xml.replaceAll('src="../img/', "src=\"file://${base}/img/")
71
72        // convert tabs to spaces otherwise they only take up one space
73        xml = xml.replaceAll('\t', '    ')
74        xml
75    }
76
77    private static void createPdf(String xml, File outputFile, String urlBase) {
78        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
79        Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes()))
80
81        ITextRenderer renderer = new ITextRenderer()
82        renderer.setDocument(doc, urlBase + '/dummy')
83
84        OutputStream outputStream
85        try {
86            outputStream = new FileOutputStream(outputFile)
87            renderer.layout()
88            renderer.createPDF(outputStream)
89        }
90        finally {
91            outputStream?.close()
92        }
93    }
94}
95