Obsah

XML+XSL-FO+PDF

FOP

http://xmlgraphics.apache.org/fop/1.1/

Čeština

Zdroj: http://www.kosek.cz/sw/fop/

Do fop-1.1/conf/fop.xconf element fonts vložit <auto-detect/>.

Podopora dělení slov

http://sourceforge.net/projects/offo/files/latest/download?source=dlp

fop-hyph.jar vložit do fop-1.1/lib

České TTF fonty

http://corefonts.sourceforge.net/

Instalace

Stažení a rozbalení FOP

shell# mkdir fop; cd fop
shell# wget -c -t 0 ftp://mirror.hosting90.cz/apache/xmlgraphics/fop/binaries/fop-1.1-bin.tar.gz
shell# tar xvzf fop-1.1-bin.tar.gz

Dělení českých slov

Stáhnout: http://sourceforge.net/projects/offo/files/latest/download?source=dlp

shell# unzip offo-hyphenation-binary_v2.0.zip
shell# cp ./offo-hyphenation-binary/fop-hyph.jar ./fop-1.1/lib

Stažení TTF fontů

shell# mkdir fonts-cab

Stáhnout: http://sourceforge.net/projects/corefonts/files/the%20fonts/final/

Extrahování TTF fontů z CAB

shell# yum install cabextract.x86_64
shell# cabextract ./fonts-cab/*
shell# mkdir fonts
shell# cp ./fonts-cab/*.TTF ./fonts

Vytvoření metric pro TTF fonty

http://xmlgraphics.apache.org/fop/1.1/fonts.html

create-ttf-metrics.sh
#!/bin/bash
#
# Create TrueType Font Metrics
#
# Author: Jiří Blažek (blazek@isn.cz)
#
 
# FOP path
FOP_PATH="./fop-1.1"
 
# True Type Fonts path
TTF_PATH="./fonts"
 
# extension from
EXT_FROM="ttf"
 
# extension to
EXT_TO="xml"
 
# rename files from upper to lower
for TTF_FILE in $(ls $TTF_PATH/*.TTF); do
  mv $TTF_FILE $(echo $TTF_FILE | tr '[:upper:]' '[:lower:]');
done
 
# Create TrueType Font Metrics
for TTF_FILE in $(ls $TTF_PATH/*.ttf); do
  XML_FILE=$(echo $TTF_FILE | sed "s/\(.*\.\)$EXT_FROM/\1$EXT_TO/");
 
  java -cp $FOP_PATH/build/fop.jar:$FOP_PATH/lib/avalon-framework-4.2.0.jar:$FOP_PATH/lib/commons-logging-1.0.4.jar:$FOP_PATH/lib/commons-io-1.3.1.jar:$FOP_PATH/lib/commons-logging-1.0.4.jar:$FOP_PATH/lib/xmlgraphics-commons-1.5.jar org.apache.fop.fonts.apps.TTFReader $TTF_FILE $XML_FILE;
done
shell# chmod 755 create-ttf-metrics.sh
shell# ./create-ttf-metrics.sh

Přesun fontů do FOP

shell# mv fonts fop-1.1

Konfigurace fop.xconf

http://xmlgraphics.apache.org/fop/1.1/configuration.html

./fop-1.1/conf/fop.xconf
<font-base>../fonts</font-base>
...
<fonts>
  <font metrics-file="arial.xml" kerning="yes" embed-url="arial.ttf">
    <font-triplet name="Arial" style="normal" weight="normal"/>
    <font-triplet name="sans-serif" style="normal" weight="normal"/>
  </font>
  <font metrics-file="ariali.xml" kerning="yes" embed-url="ariali.ttf">
    <font-triplet name="Arial" style="italic" weight="normal"/>
    <font-triplet name="sans-serif" style="italic" weight="normal"/>
  </font>
  <font metrics-file="arialbi.xml" kerning="yes" embed-url="arialbi.ttf">
    <font-triplet name="Arial" style="italic" weight="bold"/>
    <font-triplet name="sans-serif" style="italic" weight="bold"/>
  </font>
  <font metrics-file="arialbd.xml" kerning="yes" embed-url="arialbd.ttf">
    <font-triplet name="Arial" style="normal" weight="bold"/>
    <font-triplet name="sans-serif" style="normal" weight="bold"/>
  </font>
  <auto-detect/>
</fonts>

Spuštění

shell# ./fop-1.1/fop -c ./fop-1.1/conf/fop.xconf -xml ./xml/test.xml ./xsl/test.xsl -pdf ./pdf/test.pdf

JAVA FOP class

FopGen.java
package test;
 
import org.apache.log4j.Logger;
import java.io.File;
import java.io.OutputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Source;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.sax.SAXResult;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
 
/**
 *
 * @author blazek
 */
public class FopGen {
 
    static Logger logger = Logger.getLogger(FopGen.class);
    private String xconf;
    private String fontBase;
    private String xmlInput;
    private String xsltInput;
    private String pdfOutput;
 
    public FopGen() {
    }
 
    public FopGen(String xconf, String fontBase, String xmlInput, String xsltInput, String pdfOutput) {
        this.xconf = xconf;
        this.fontBase = fontBase;
        this.xmlInput = xmlInput;
        this.xsltInput = xsltInput;
        this.pdfOutput = pdfOutput;
    }
 
    public String getXconf() {
        return xconf;
    }
 
    public void setXconf(String xconf) {
        this.xconf = xconf;
    }
 
    public String getFontBase() {
        return fontBase;
    }
 
    public void setFontBase(String fontBase) {
        this.fontBase = fontBase;
    }
 
    public String getXmlInput() {
        return xmlInput;
    }
 
    public void setXmlInput(String xmlInput) {
        this.xmlInput = xmlInput;
    }
 
    public String getXsltInput() {
        return xsltInput;
    }
 
    public void setXsltInput(String xsltInput) {
        this.xsltInput = xsltInput;
    }
 
    public String getPdfOutput() {
        return pdfOutput;
    }
 
    public void setPdfOutput(String pdfOutput) {
        this.pdfOutput = pdfOutput;
    }        
 
    public boolean createPDF() {
 
        try {
            // configure fopFactory as desired
            FopFactory fopFactory = FopFactory.newInstance();
 
            // configure fonts
            fopFactory.setUserConfig(this.xconf);
            fopFactory.getFontManager().setFontBaseURL("file://" + this.fontBase);
            FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
 
            // Setup output
            OutputStream out = new java.io.FileOutputStream(new File(this.pdfOutput));
            out = new java.io.BufferedOutputStream(out);
 
            try {
                // Construct fop with desired output format
                Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);
 
                // Setup XSLT
                TransformerFactory factory = TransformerFactory.newInstance();
                Transformer transformer = factory.newTransformer(new StreamSource(new File(this.xsltInput)));
 
                // Setup input for XSLT transformation
                Source src = new StreamSource(new File(this.xmlInput));
 
                // Resulting SAX events (the generated FO) must be piped through to FOP
                Result res = new SAXResult(fop.getDefaultHandler());
 
                // Start XSLT transformation and FOP processing
                transformer.transform(src, res);
            } finally {
                out.close();
            }
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
 
        return true;
    }
}