All Downloads are FREE. Search and download functionalities are using the official Maven repository.

grails.doc.PdfBuilder.groovy Maven / Gradle / Ivy

There is a newer version: 6.2.2
Show newest version
/* Copyright 2004-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package grails.doc

import org.jsoup.parser.Parser

import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory

import org.jsoup.Jsoup
import org.w3c.dom.Document
import org.xhtmlrenderer.pdf.ITextRenderer

class PdfBuilder {

    private static final String LIVE_DOC_SITE = 'http://grails.org'

    static void build(String baseDir, String styleDir = null) {
        build basedir: baseDir
    }

    /**
     * Builds a PDF file from the manual's single.html file.

* The following directories are assumed to exist:

    *
  • $basedir/guide/single.html
  • *
  • $basedir/guide/css/
  • *
  • $basedir/guide/img/
  • *
* * The {@code options} map should have the following key/value pairs
    *
  • basedir = points to the root directory that contains the generated manual required
  • *
*/ static void build(Map options) { File baseDir = new File(options.basedir).canonicalFile File guideDir = new File(baseDir, "guide") File htmlFile = new File(guideDir, "single.html") File outputFile = new File(guideDir, "single.pdf") String xml = createXml(htmlFile, baseDir.absolutePath) createPdf xml, outputFile, guideDir } static String createXml(File htmlFile, String base) { String xml = htmlFile.getText("UTF-8") // fix inner anchors xml = xml.replaceAll('', pdfCss() + '') if(debugPdf) { File before = new File(htmlFile.absolutePath + '.before.xml') before.setText(xml, 'UTF-8') if(result != xml) { File after = new File(htmlFile.absolutePath + '.after.xml') after.setText(result, 'UTF-8') } } result } private static String removeCssLink(String htmlString) { String output String str = htmlString int index = str.indexOf('') + '/>'.length(), end.size()) output } static String removeCssLinks(String html) { String str = html for (;;) { int index = str.indexOf(' pre, code { font-size: 10px; } .toc-item { margin-bottom: 2px; } .toc-item strong { margin-right: 2px; } .contribute-btn, #navigation, #ref-button, #toggle-col1 { display: none; } .paragraph, table, h2, h3, h4, h5, h6, li, pre, code { width: 595px; } """ } static Document createDocument(String xml) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance() dbf.validating = false dbf.setFeature "http://apache.org/xml/features/nonvalidating/load-external-dtd", false dbf.setFeature "http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false DocumentBuilder builder = dbf.newDocumentBuilder() builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8"))) } static void createPdfWithDocument(Document doc, File outputFile, File urlBase) { ITextRenderer renderer = new ITextRenderer() renderer.setDocument(doc, urlBase.toURI().toString()) OutputStream outputStream try { outputStream = new FileOutputStream(outputFile) renderer.layout() renderer.createPDF(outputStream) } finally { outputStream?.close() } } static void createPdf(String xml, File outputFile, File urlBase) { Document doc = createDocument(xml) createPdfWithDocument(doc, outputFile, urlBase) } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy