xmlgraphics-fop-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jon Kofal <jonko...@yahoo.com>
Subject Re: Embeding Fonts into a PDF
Date Thu, 12 Sep 2002 15:36:22 GMT
On the server-side I have a solution which involves putting all TTF and
XML files in the WAR, and using the javax.servlet.temp.dir to write
them, since the FOP API requires File Objects.

The command-line options are nice for testing, but I found the examples
particularly useless for Servlet coding.


Jon K


    /** Path to the font.ttf and FOP font.xml and FOP userconfig.xml
files */
    private static String fontResourcePath = "/fonts/";

  /**
   * Initializes client-specifiic fonts.
   * 1. Reads FOP font files from the JAR and writes them to the disk
   * 2. then updates the userconfig.xml with the file paths just
written
   * 3. writes the usersconfig.xml to disk
   * 4. initializes the FOP Options class with the userconfig.xml from
disk.
   *
   * NOTE: FOP requires DISK paths to make this work :-(
   */
    private final void initFonts() {
        //failure of the fonts is non-critical, as long as we have
System Fonts we produce something
        //wrapping this block and ignoring Exceptions makes it
non-fatal
        try {
            //first fix a common path bug of no ending slash
            if ( ! fontResourcePath.endsWith("/")
                fontResourcePath = fontResourcePath.concat("/");

            //load in the userconfig.xml as an XMLDocument so we can
tweak the contents
            URL url = this.getClass().getResource(this.fontResourcePath
+ "FOPFontConfiguration.xml");
            XMLDocument userConfig = new XMLDocument(url);

            //tweak the contents of the userconfig.xml
            //for each //font Element
            //  read in each metrics-file Attribute Value
            //  read from the jar/war the corresponding Resource
            //  write the Resource to the tempDir
            //  update the Attribute Value
            //  read in each embed-file Attribute Value
            //  read from the jar/war the corresponding Resource
            //  write the Resource to the tempDir
            //  update the Attribute Value

            Iterator fontElements =
userConfig.getElements("//font").iterator();
            while (fontElements.hasNext()) {
                Element fontElement = (Element)fontElements.next();
                try {
                    Attribute metricsFileAttribute =
fontElement.getAttribute("metrics-file");
                    String xmlFileName =
metricsFileAttribute.getValue();
                    File xmlTempFile = new File(tempDir,xmlFileName);
                    BufferedOutputStream xmlFileOut = new
BufferedOutputStream(new FileOutputStream(xmlTempFile));
                    url =
this.getClass().getResource(this.fontResourcePath + xmlFileName);
                    byte[] xmlBytes = new
byte[url.openConnection().getContentLength()];
                    BufferedInputStream xmlIn = new
BufferedInputStream(url.openStream());
                    xmlIn.read(xmlBytes);
                    xmlIn.close();
                    xmlFileOut.write(xmlBytes);
                    xmlFileOut.close();
                   
metricsFileAttribute.setValue("file:///"+xmlTempFile.getAbsolutePath());

                    Attribute embedFileAttribute =
fontElement.getAttribute("embed-file");
                    String ttfFileName = embedFileAttribute.getValue();
                    File ttfTempFile = new File(tempDir,ttfFileName);
                    BufferedOutputStream ttfFileOut = new
BufferedOutputStream(new FileOutputStream(ttfTempFile));
                    url =
this.getClass().getResource(this.fontResourcePath + ttfFileName);
                    
                    //initialize the Font with AWT
                   
Font.createFont(Font.TRUETYPE_FONT,this.getClass().getResourceAsStream(this.fontResourcePath
+ ttfFileName));

                    byte[] ttfBytes = new
byte[url.openConnection().getContentLength()];
                    BufferedInputStream ttfIn = new
BufferedInputStream(url.openStream());
                    ttfIn.read(ttfBytes);
                    ttfIn.close();
                    ttfFileOut.write(ttfBytes);
                    ttfFileOut.close();
                   
embedFileAttribute.setValue(ttfTempFile.getAbsolutePath());
                } catch (Exception ex) {
                    System.err.println("Could not load Font "+
userConfig.toString(fontElement));
                }
            }
            //save the file to the tempDir, as FOP Options requires a
File Object
            byte[] bytes = userConfig.toByteArray();
            File userConfigFile = new File(tempDir,"userconfig.xml");
            BufferedOutputStream fileOut = new BufferedOutputStream(new
FileOutputStream(userConfigFile));
            fileOut.write(bytes);
            fileOut.close();
            //init fonts with FOP
            org.apache.fop.apps.Options options = new
Options(userConfigFile);
        } catch (Exception e) {
            //failure of the FOP fonts is non-critical, as long as we
have System Fonts we produce something
            System.err.println("Could not initialize fonts");
        }
    }




--- "Brunner, Konrad" <konrad.brunner@eds.com> wrote:
> Hy experts
> 
> Does somebody out there knows, how to embed used fonts into the pdf
> file?
> 
> Mit freundlichen Grüssen
> Konrad Brunner
> Consultant CAD/PDM
> 
> EDS
> PLM Solutions
> Tel :	(+41) 01 / 755 72 63
> Fax :	(+41) 01 / 755 72 70
> Email: mailto:konrad.brunner@eds.com
> 
> Postanschrift:
> Unigraphics Solutions AG
> Ruetistrasse 19
> CH-8952 Schlieren
> 
> Besuchen Sie unsere Website:
> http://www.edsplmsolutions.ch/ oder http://www.eds.com/
> 
> 


__________________________________________________
Do you Yahoo!?
Yahoo! News - Today's headlines
http://news.yahoo.com

Mime
View raw message