如何从类路径加载ICC配置文件?

3

在使用Jasper Reports生成符合PDF/A标准的PDF时,我遇到了问题:

net.sf.jasperreports.engine.util.JRPdfaIccProfileNotFoundException. 

这可以通过配置轻松解决。
<property name="net.sf.jasperreports.export.pdfa.icc.profile.path" value="/stuff/myicc.icc"/>

但是我需要将ICC配置文件与其余WAR捆绑在一起,就像报告嵌入字体一样。应该怎么做?我正在寻找类似于

的东西。
<property name="net.sf.jasperreports.export.pdfa.icc.profile.path" value="classpath:/jasper/someicc.icc"/>

但是这并没有起作用,与报告本身相关的icc配置文件也是如此。我可以将InputStream作为参数传递给PDFGenerator吗?

2个回答

1
如果您正在从Java代码进行导出,可以使用 getClassLoader().getResource() 来获取文件的绝对路径。请注意,本方法只适用于类路径下的文件。 JRPdfExporter示例
JRPdfExporter exporter = new JRPdfExporter();
//... your input and output
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
URL path = this.getClass().getClassLoader().getResource("jasper/someicc.icc");
configuration.setIccProfilePath(path.getFile());
//other settings
exporter.setConfiguration(configuration);
exporter.exportReport();

0

我们最终创建了自己的扩展来处理类路径加载,使其更加集中化。

实现类路径扩展的代码在这里:

package com.somepackage;

import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import net.sf.jasperreports.engine.JRPropertiesMap;
import net.sf.jasperreports.extensions.ExtensionsRegistry;
import net.sf.jasperreports.extensions.ExtensionsRegistryFactory;
import net.sf.jasperreports.repo.InputStreamResource;
import net.sf.jasperreports.repo.RepositoryService;
import net.sf.jasperreports.repo.Resource;

/**
 * JasperReports extension factory that enables to read from classpath. Only tries to resolve URLs that start with "classpath:".
 */
public class ClasspathExtensionsRegistryFactory implements ExtensionsRegistryFactory {

    @Override
    public ExtensionsRegistry createRegistry(String registryId, JRPropertiesMap properties) {
        return extFactory;
    }

    private static final ClasspathRepositoryService service = new ClasspathRepositoryService();
    private static final List<ClasspathRepositoryService> services = Collections.singletonList(service);
    private static final ExtensionsRegistry extFactory = new ExtensionsRegistry() {
        @SuppressWarnings("unchecked")
        @Override
        public <T> List<T> getExtensions(Class<T> extensionType) {
                        if (RepositoryService.class.equals(extensionType)) {
                            return (List<T>) services;
                        }
                        return null;
                }
    };

    /**
     * RepositoryService that reads resources from classpath.
     */
    public static class ClasspathRepositoryService implements RepositoryService {

        private static final String CLASSPATH_PREFIX = "classpath:";
        private static final int CLASSPATH_PREFIX_LENGTH = CLASSPATH_PREFIX.length();

        @Override
        public Resource getResource(String uri) {
            return getResource(uri, InputStreamResource.class);
        }

        @Override
        public void saveResource(String uri, Resource resource) {
            //No-op
        }

        @SuppressWarnings("unchecked")
        @Override
        public <K extends Resource> K getResource(String uri, Class<K> resourceType) {
            if (resourceType != null && InputStreamResource.class.equals(resourceType) && uri != null && uri.trim().startsWith(CLASSPATH_PREFIX)) {
                InputStream is = this.getClass().getResourceAsStream(uri.trim().substring(CLASSPATH_PREFIX_LENGTH));
                if (is != null) {
                    InputStreamResource isr = new InputStreamResource();
                    isr.setInputStream(is);
                    isr.setName(uri);
                    return (K)isr;
                }
            }
            return null;
        }

    }

}

接下来要做的就是添加jasperreports_extension.properties文件,其中包含以下内容:

net.sf.jasperreports.extension.registry.factory.classpathresourcereader=com.somepackage.ClasspathExtensionsRegistryFactory

这样可以像这样配置导出器:

SimplePdfExporterConfiguration pec = new SimplePdfExporterConfiguration();
.
.
.
pec.setIccProfilePath("classpath:/somefolder/iccprofile.icc");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接