sfwaer
sfwaer

Reputation: 71

Apache FOP ValidationException: Invalid property encountered on "fo:inline": font-variant-ligatures

I am using Apache FOP to convert FO files to PDF. During the conversion, I encountered the exception below:

javax.xml.transform.TransformerException: org.apache.fop.fo.ValidationException: Invalid property encountered on "fo:inline": font-variant-ligatures (See position 1222:1124)

The FO file is generated using XSL-FO, a markup language for formatted objects, used in our case to turn XHTML into valid FO blocks. However, some HTML style properties and attributes are not supported by Apache FOP but they do not block the PDF generation (the font-variant-ligatures style property in my case).

How can I ignore the exception and continue to generate my PDF without considering the invalid property?

Upvotes: 2

Views: 1766

Answers (2)

sfwaer
sfwaer

Reputation: 71

To ignore the "invalidProperty" exception or any other event when validating FO (see more: https://xmlgraphics.apache.org/fop/2.3/events.html), you must:

1- First, create an event listener that will intercept this event. For this you have to use the org.apache.fop.events.EventListener interface and override the processEvent method by describing the behavior of the FOP Transformer when encountering the exception.

For example; you can create a listener that changes the exception level to WARNING for invalidProperty exceptions so that it does not block the PDF generation.

private static class InvalidPropertyEventListener implements EventListener {

    /**
     * Continues processing even if an <code>invalidProperty</code> runtimeException was thrown
     * during FO to PDF transformation.
     * <p>
     * Descends the event's severity level to WARN to avoid the exception throwing.
     *
     * @param event The event to be processed.
     */
    public void processEvent(Event event) {
        if ("org.apache.fop.fo.FOValidationEventProducer.invalidProperty".equals(event.getEventID())) {
            event.setSeverity(EventSeverity.WARN);
        }
    }
}

2- Next, you have to register the event listener with FOP, do get the EventBroadcaster which is associated with the user agent (FOUserAgent) and add it there:

// Adding FOP eventListeners
FOUserAgent userAgent = Factory.getInstance().getFactory().newFOUserAgent();
userAgent.getEventBroadcaster().addEventListener(getInvalidPropertyEventListener());
Fop fop = Factory.getInstance().getFactory().newFop(MimeConstants.MIME_PDF, userAgent, output);

// Transform the FO to PDF
Result res = new SAXResult(fop.getDefaultHandler());
Source src = new StreamSource(foSource);
Transformer transformer = TRANSFACTORY.newTransformer();
transformer.transform(src, res);

NB: This is done separately for each processing run, i.e. for each new user agent.

Upvotes: 2

Tony Graham
Tony Graham

Reputation: 8068

FOP is required to complain about the non-XSL-FO attributes. The XSL 1.1 Recommendation (https://www.w3.org/TR/xsl11/#xsl-namespace) includes:

It is an error for an element from the XSL namespace to have attributes with expanded-names that have null namespace URIs (i.e., attributes with unprefixed names) other than attributes defined in this document.

However, you might get fewer messages if you add the -r command-line parameter for "relaxed/less strict validation (where available)". (See https://xmlgraphics.apache.org/fop/2.3/running.html.)

Upvotes: 1

Related Questions