java - PDF 中的 HTML 嵌入图像

标签 java birt

我使用 BIRT 创建了一个报告,一些动态字段是来自后端系统的 HTML 值,但是当我尝试生成带有 HTML 嵌入图像的 PDF 报告时,它会显示:“此报告项的资源不是可达。”而不是图像。

是否有任何解决方案可以在 PDF 中渲染嵌入图像?它在 HTML 中运行良好。

最佳答案

我找到了一个解决方案,如何在 PDF 中渲染嵌入 HTML 内容的图像。问题出在 BIRT 库 ResourceLocatorWrapper 类上。我改变了方法:

public byte[] findResource(ModuleHandle design, String fileName, int fileType, Map appContext);

现在它可以成功显示 PDF 中的嵌入图像和其余 HTML 内容。不需要将其添加到 BIRT jar 库中,您可以像我一样在项目中的 org.eclipse.birt.report.engine.util 包下添加此类,并且它应该可以正常工作。

package org.eclipse.birt.report.engine.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.birt.report.engine.i18n.MessageConstants;
import org.eclipse.birt.report.model.api.ModuleHandle;

import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.pdf.codec.Base64;

public class ResourceLocatorWrapper {

    private HashMap<URL, byte[]> cache;
    private static final byte[] DUMMY_BYTES = new byte[0];
    private static final String RESOURCE_BUNDLE = "org.eclipse.birt.report.engine.i18n.Messages";
    protected static Logger logger = Logger.getLogger(ResourceLocatorWrapper.class.getName(), RESOURCE_BUNDLE);

    public ResourceLocatorWrapper() {
        cache = new HashMap<URL, byte[]>();
    }

    public void dispose() {
        synchronized (cache) {
            cache.clear();
            cache = null;
        }
    }

    /**
     * 
     * @param fileName
     * @param fileType
     * @param appContext
     * @return
     */
    public byte[] findResource(ModuleHandle design, String fileName, int fileType, Map appContext) {

        if (fileName.startsWith("data:")) {
            final String base64Data = fileName.substring(fileName.indexOf(",") + 1);
            try {
                return Base64.decode(base64Data);
            } catch (Exception e) {
                throw new ExceptionConverter(e);
            }
        } else {
            URL url = design.findResource(fileName, fileType, appContext);
            if (url == null) {
                logger.log(Level.WARNING, MessageConstants.RESOURCE_NOT_ACCESSIBLE, fileName);
                return DUMMY_BYTES;
            }
            return findResource(url);
        }
    }

    /**
     * Finds a resource from the given URL. If the URL is not accessible, it
     * will return a 0-size byte array.
     */
    public byte[] findResource(URL url) {
        System.out.println("findResource(URL url)");
        if (url == null) {
            return DUMMY_BYTES;
        }
        synchronized (cache) {
            if (cache == null) {
                return DUMMY_BYTES;
            }
            byte[] inBytes = cache.get(url);
            if (inBytes == null) {
                try {
                    InputStream in = url.openStream();
                    inBytes = getByteArrayFromInputStream(in);
                    in.close();
                    cache.put(url, inBytes);
                } catch (IOException e) {
                    logger.log(Level.WARNING, MessageConstants.RESOURCE_NOT_ACCESSIBLE, url.toExternalForm());
                    cache.put(url, DUMMY_BYTES);
                    return DUMMY_BYTES;
                }
            }
            return inBytes;
        }
    }

    private byte[] getByteArrayFromInputStream(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int size = in.read(buffer);
        while (size != -1) {
            out.write(buffer, 0, size);
            size = in.read(buffer);
        }
        buffer = out.toByteArray();
        out.close();
        return buffer;
    }

}

关于java - PDF 中的 HTML 嵌入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32116995/

相关文章:

java - png 图像的内容类型为 null

eclipse - 新手 BIRT - 布局 - 地址等字段并排

java - 将基于 Java 的配置转换为基于 Spring XML 的配置

linux - BIRT .xlsx 文件在 Linux 环境中损坏,而在 Windows 环境中运行良好

java - 使用 Turnkey Linux 在 Windows 环境中托管 Java Web 服务?

java - 使用NIO解码器会导致读取整个文件吗?

java - Spring MVC Controller 响应作为静态内容缓存在浏览器中

java - JMonkey 中的库问题

java - Eclipse 和 BIRT 错误 - 在 Web 查看器中查看报告

rest - 所有构建类型的最后 10 天的 TEAMCITY REST API 列表