How to create a PDF file with iText + XMLWorker from a servlet using a custom font?

Playing with iText / XMLWorker samples (mostly this one ), I could easily write simple applications that can create PDF files from HTML using my own additional fonts, but as soon as I try to use my stuff in some kind of web code service, I ended up with exceptions such as:

Table 'name' does not exist in file:/C:/work/MyServer/target/classes/fonts/My%20Font.ttf
ExceptionConverter: com.itextpdf.text.DocumentException: Table 'name' does not exist in file:/C:/work/MyServer/target/classes/fonts/My%20Font.ttf

... which seems to indicate that in the context of the web service, the font file could not be loaded as expected. Here is most of my code:

public HtmlRenderer(final String css, final String[] fontPaths) {
    // fontPaths = {
    // "/fonts/My Font.ttf",
    // "/fonts/My Other Font.ttf",
    // ...
    // };

    // CSS
    cssResolver = new StyleAttrCSSResolver();
    if (css != null) {
        final CssFile cssFile = XMLWorkerHelper.getCSS(new ByteArrayInputStream(css.getBytes()));
        cssResolver.addCss(cssFile);
    }

    // HTML
    XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
    if (fontPaths != null) {
        for (final String fontPath : fontPaths) {
            final String path = this.getClass().getResource(fontPath).toExternalForm();
            fontProvider.register(path);
        }
    }
    CssAppliers cssAppliers = new CssAppliersImpl(fontProvider);
    htmlContext = new HtmlPipelineContext(cssAppliers);
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
    ...

Should I rely on things like:

  • Extract fonts from resources in temporary files (see here )
  • factory (. )

!

+2
1

, , : Maven ( ):

    <resources>
        <resource>
            <filtering>true</filtering>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**/*.ttf</exclude>
            </excludes>
        </resource>
        <resource>
            <filtering>false</filtering>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.ttf</include>
            </includes>
        </resource>
        ...
    </resources>
+2

All Articles