Apache FOP set dynamic DPI

Viewed 68

I want to Apache FOP 2.3 convert XML+XSL to a PNG image. I want to be able to set DPI dynamically for every image. Below, I have a working example, I set dpi via FOUserAgent. My question is: Is this the conventional way how you set DPI via Apache FOP? Or it must be set in a different way?

public OutputStream transformXSL2Image(String xmlString, String xslString, String docsPerPage, String defaultTransformer, String startPackCount, String totalPackCount, int dpi) throws Exception {
        
        File file = new File("out.png");
        OutputStream fileOut = new BufferedOutputStream(new FileOutputStream(file));
        
        // Set some FOP settings
        FOUserAgent userAgent = getFopFactory().newFOUserAgent();
        userAgent.setProducer("Apache FOP");
        userAgent.setCreator("API");
        // This is how I set DPI
        userAgent.setTargetResolution(dpi);

        Fop fop = getFopFactory().newFop(MimeConstants.MIME_PNG, userAgent, fileOut);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(xslString);
        
        Result result = new SAXResult(fop.getDefaultHandler());
        transformer.transform(xmlString, result);
        return fileOut;
    }

private FopFactory getFopFactory() {
    if (fopFactory == null) {
        try {
            fopFactory = FopFactory.newInstance(new File("resources/fop_config.xml").toURI());
        } catch (Exception ex) {
            throw ex;
        }
    }
    return factory;
}

Thanks for any help!

0 Answers
Related