PDF Serializer
The pdf serializer takes fo xml events as input. By using the
FOP project it creates pdf out of the sax events.
This serializer is optional and requires the fop package
in the lib directory when building cocoon 2. However,
the distribution includes this package already.
-
Name : fo2pdf
-
Class: org.apache.cocoon.serialization.FOPSerializer
-
Cacheable: yes.
FOP and Embedding Fonts
Dynamically generating a pdf file (with embeded fonts) in Cocoon
is basically 8 steps:
-
Create the font(s) metric file(s).
-
Create a custom configuration file for FOP (Cocoons PDF renderer)
which tells it what fonts are available and where to find them.
-
Create your xml (left as an exercise for the reader ;)
-
Create your xslt (again, up to you)
-
In the sitemap, tell the fo2pdf serializer where your custom
configuration file is located.
-
Add a match for your pdf (I'm sure you can do the rest...).
-
Start Cocoon.
-
Request your pdf.
Easy yeah? OK. Step-by-step...
Create the font(s) metric file(s).
 | All java calls have nothing else in the
classpath OR ext directory also, instructions which have wrapped
should be entered as one single instruction. |
The instruction to generate a font metric file is:
Windows:
 |  |  |
 |
java -cp fop-0.20.3rc.jar;xercesImpl-2.0.0.jar;xml-apis.jar \
org.apache.fop.fonts.apps.TTFReader \
%PATH_TO_FONT% %PATH_TO_METRICS_DIR%\%FONT_NAME%.xml
|  |
 |  |  |
Unix:
 |  |  |
 |
java -cp fop-0.20.3rc.jar;xercesImpl-2.0.0.jar;xml-apis.jar \
org.apache.fop.fonts.apps.TTFReader \
$PATH_TO_FONT $PATH_TO_METRICS_DIR/$FONT_NAME.xml
|  |
 |  |  |
 | These jars are not all in the same directory. Either provide
path information (fop is in optional, the other two in core) or
copy them into the same directory. |
For the sake of this tutorial, I'm going to be using windows,
converting the Arial family of fonts and storing the metrics files in
the location D:\fop-fonts .
My ttf files are located in C:\WINNT\Fonts . If you are
running on linux/windows 9x/windows ME please alter as
appropriate.
 | I normally use Cygwin; a unix shell
environment which runs on windows. If I slip some unix into here,
please excuse me (although I'd welcome the feedback...). |
Generating the Arial metrics
Start a command session (as appropriate to your env) then change
to Cocoon libs directory.
create the metrics directory (D:\fop-fonts)
create the metrics for arial.ttf, arialb.ttf, arialbi.ttf,
ariali.ttf
 |  |  |
 |
$ java -cp optional\fop-0.20.3rc.jar;core\xercesImpl-2.0.0.jar;core\xml-apis.jar \
org.apache.fop.fonts.apps.TTFReader \
C:\WINNT\Fonts\arial.ttf D:\fop-fonts\arial.ttf.xml
$ java -cp optional\fop-0.20.3rc.jar;core\xercesImpl-2.0.0.jar;core\xml-apis.jar \
org.apache.fop.fonts.apps.TTFReader \
C:\WINNT\Fonts\arialb.ttf D:\fop-fonts\arialb.ttf.xml
$ java -cp optional\fop-0.20.3rc.jar;core\xercesImpl-2.0.0.jar;core\xml-apis.jar \
org.apache.fop.fonts.apps.TTFReader \
C:\WINNT\Fonts\arialbi.ttf D:\fop-fonts\arialbi.ttf.xml
$ java -cp optional\fop-0.20.3rc.jar;core\xercesImpl-2.0.0.jar;core\xml-apis.jar \
org.apache.fop.fonts.apps.TTFReader \
C:\WINNT\Fonts\ariali.ttf D:\fop-fonts\ariali.ttf.xml
|  |
 |  |  |
If everything went to plan, you should now have the metrics for
the Arial fonts in your fop-fonts directory.
Create a custom configuration file
I normally store this with the metrics file in the fop-fonts
directory (called config.xml (ensure there's not a font called
config ;)) although I fully qualify all the filenames just incase I
move it ;)
I also find it useful to retain the .ttf as it is also
possible to add other types of fonts (if you want to read the FOP
docs) and the ttf tells me where to locate the font.
 |  |  |
 |
<configuration>
<fonts>
<font metrics-file="D:/fop-fonts/arial.ttf.xml"
kerning="yes" embed-file="C:/WINNT/Fonts/arial.ttf">
<font-triplet name="Arial" style="normal" weight="normal"/>
<font-triplet name="ArialMT" style="normal" weight="normal"/>
</font>
<font metrics-file="D:/fop-fonts/arialb.ttf.xml"
kerning="yes" embed-file="C:/WINNT/Fonts/arialb.ttf">
<font-triplet name="Arial" style="normal" weight="bold"/>
<font-triplet name="ArialMT" style="normal" weight="bold"/>
</font>
<font metrics-file="D:/fop-fonts/arialbi.ttf.xml"
kerning="yes" embed-file="C:/WINNT/Fonts/arialbi.ttf">
<font-triplet name="Arial" style="italic" weight="bold"/>
<font-triplet name="ArialMT" style="italic" weight="bold"/>
</font>
<font metrics-file="D:/fop-fonts/ariali.ttf.xml"
kerning="yes" embed-file="C:/WINNT/Fonts/ariali.ttf">
<font-triplet name="Arial" style="italic" weight="normal"/>
<font-triplet name="ArialMT" style="italic" weight="normal"/>
</font>
</fonts>
</configuration>
|  |
 |  |  |
There are other things you can add to this file, look at the FOP
documentation for further information.
If you are wondering why each font has been added twice it's to do
with the font lookup. If the font is specified as 'Arial' and the
weight is 'bold' then FOP searches for a
<font-triplet/> which matches then uses
the parent <font/> tag to get the actual
font information. If the font is specified as 'ArialMT' (it's proper
name) it will still work. Think of it as an alias capability.
Sitemap and fo2pdf serializer.
All that remains is to tell the serializer where your config file is
located. Find the line in your sitemap which looks
like:
 |  |  |
 |
<map:serializer name="fo2pdf"
src="org.apache.cocoon.serialization.FOPSerializer"
mime-type="application/pdf"/>
|  |
 |  |  |
and replace it with...
 |  |  |
 |
<map:serializer name="fo2pdf"
src="org.apache.cocoon.serialization.FOPSerializer"
mime-type="application/pdf">
<user-config src="D:/fop-fonts/config.xml"/>
</map:serializer>
|  |
 |  |  |
And that's it. Oh, one final thing to remember: the cache isn't aware
of your config file; always delete your cache-dir
after modifying your config file.
|