Embedding svg images in html output #1624
-
I'm trying to get the html output format to already have all the images embedded into it. By default BIRT renders images and charts in temporary files, but since we run reports on a separate system it would be much easier if we only needed to download the single output file, also to avoid problems with file paths etc. if (outputType.equals(IRenderOption.OUTPUT_FORMAT_HTML)) {
HTMLRenderOption options = new HTMLRenderOption();
options.setOutputFormat(IRenderOption.OUTPUT_FORMAT_HTML);
options.setOutputFileName(outputFile);
options.setSupportedImageFormats("SVG,PNG,JPG,JPEG");
options.setEmbeddable(true);
options.setImageHandler(new HTMLServerImageHandler() {
@Override
protected String handleImage(IImage image, Object context, String prefix, boolean needMap) {
String imageString = Base64.getEncoder().encodeToString(IOUtils.toByteArray(image.getImageStream()));
String extension = image.getExtension().replace(".", "");
return MessageFormat.format("data:image/{0};base64,{1}", extension, imageString);
}
});
task.setRenderOption(options);
} It works fine with .png files, and although I know I can set the charts' output format to png, there are some images we use in the report that are in .svg format and are not displayed. Instead, when I open the output file in the browser it requests to download the .svg images, which is obviously pointless as there's no reference to them in the html Is it possible to embed them as .svg or do we need to convert them to .png? I see in the ouput file that BIRT creates an |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I'm not using HTML output, so this is just a guess: Maybe the |
Beta Was this translation helpful? Give feedback.
-
Nevermind, I was very close to the right solution, I just overlooked that I needed to put the MIME type of the image in the result and not just the extension. With this as the content of the String imageString = Base64.getEncoder().encodeToString(IOUtils.toByteArray(image.getImageStream()));
return MessageFormat.format("data:{0};base64,{1}", image.getMimeType(), imageString); |
Beta Was this translation helpful? Give feedback.
Nevermind, I was very close to the right solution, I just overlooked that I needed to put the MIME type of the image in the result and not just the extension. With this as the content of the
handleImage
function it's now all working properly :)