如何从classpath加载ICC配置文件? [英] How to load ICC profile from classpath?

查看:62
本文介绍了如何从classpath加载ICC配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当尝试使用Jasper Reports生成符合PDF/A标准的PDF时,我得到了

While trying to generate PDF that conforms to PDF/A standard using Jasper Reports, I got

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

通过配置

<property name="net.sf.jasperreports.export.pdfa.icc.profile.path" value="/stuff/myicc.icc"/>

但是我需要将icc配置文件与其余的WAR捆绑在一起,就像报表所嵌入的字体一样.如何才能做到这一点?我看起来像

but I need to bundle the icc profile with rest of the WAR just like the fonts the report embeds. How can this be done? I'm looking something like

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

但这是行不通的,icc配置文件相对于报告本身也不起作用.我可以将InputStream作为参数传递给PDFGenerator吗?

But this did not work, nor did icc profile relative to the report itself. Can I pass the InputStream as a parameter to the PDFGenerator?

推荐答案

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

We ended up creating our own extension to handle classpath loading to make it more centralized.

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

The code to implement classpath extension is here:

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

After that the only thing left to do is to add jasperreports_extension.properties file that contains net.sf.jasperreports.extension.registry.factory.classpathresourcereader=com.somepackage.ClasspathExtensionsRegistryFactory

这使人们可以像这样配置出口商:

This enables one to conf the exporter like this:

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

这篇关于如何从classpath加载ICC配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆