首页 > 解决方案 > Apache FOP 设置动态 DPI

问题描述

我想 Apache FOP 2.3 将 XML+XSL 转换为 PNG 图像。我希望能够为每个图像动态设置 DPI。下面,我有一个工作示例,我通过 FOUserAgent 设置 dpi。我的问题是:这是通过 Apache FOP 设置 DPI 的传统方式吗?或者它必须以不同的方式设置?

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;
}

谢谢你的帮助!

标签: javaapache-fopdpi

解决方案


推荐阅读