Converting svg images to base64 interactivity not working? - java

I am generating one birt report output in html format and I have one svg image on which there is some interactivity is defined but while converting that image to base64 and showing in html it gives me error, my html is coming from backend dynamically.
Error I am getting is as below:
Uncaught DOMException: Blocked a frame with origin "null" from accessing a cross-origin frame.
at isIPadIPhone (data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW
and I am converting file as below:
"data:image/svg+xml;base64," + new String(Base64.encodeBase64(Files.readAllBytes(file.toPath())));
How can we resolve this issue please help me out. I cannot use "Access-Control-Allow-Origin":"*" as it is a web application security will get affected.

Related

Java - Render images in pdf from html sources

We are currently using Jasper report to generate pdf. But recently we come to problem where we need to render HTML code in pdf. And HTML code contains images. Images sources can be both http URL or base64 string.
How can we solve the problem? Or which library can we use to solve the problem?

Display SVG image with Foundation Image component

I am trying to use the Image component in CQ5.6 to include an SVG image on a page. I can get the component to use the SVG by dragging the image from the content finder to the component, but this also causes an error in the log:
Caused by: javax.imageio.IIOException: No decoder available to load the image
at com.day.image.Layer.<init>(Layer.java:786)
at com.day.image.Layer.<init>(Layer.java:588)
at com.day.image.Layer.<init>(Layer.java:511)
at com.day.cq.commons.ImageHelper.createLayer(ImageHelper.java:173)
at com.day.cq.commons.ImageResource.getLayer(ImageResource.java:569)
at libs.foundation.components.parbase.img_GET.writeLayer(img_GET.java:72)
at com.day.cq.wcm.commons.AbstractImageServlet.doGet(AbstractImageServlet.java:80)
at org.apache.sling.api.servlets.SlingSafeMethodsServlet.mayService(SlingSafeMethodsServlet.java:268)
at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:344)
at org.apache.sling.api.servlets.SlingSafeMethodsServlet.service(SlingSafeMethodsServlet.java:375)
at org.apache.sling.scripting.java.impl.ServletWrapper.service(ServletWrapper.java:126)
at org.apache.sling.scripting.java.impl.JavaScriptEngineFactory.callServlet(JavaScriptEngineFactory.java:211)
... 88 more
I've made several attempts to add support for SVGs by including SVG implementations of the javax.imageio, but have yet to succeed. The closest that I have come to success was using Batik, in which case CQ5 seemed to get stuck in an infinite loop at Layer.java:769-781.
Has anybody managed to make the OOTB Image component work with SVG images?
I ran into the same problem. I tried to download the file manually from CRX and opening that in my default image viewer. Unfortunately, my default image viewer also gave the same error.
For me the problem was with uploading part of that image. My InputStream passed in JCRUtil.put skipped some bytes to write because I used TikaInputStream before writting for validating the mediaType of the uploaded file.

Download SVG images from website with Java

I'm trying to download some images from a website. I've been using Jsoup to do some scraping and have successfully downloaded images given a url before but the images on this website are in svg format. There is no link to a location where the svg file is located, the image is embedded in svg tags. I have seen Batik used for converting svg files to other image formats but I don't have the svg file available.
Is there any way to do this? Would appreciate any guidance. Thank you.
Typically an SVG image is not a file, but rather it is included in the response body of the GET request from a browser. What you can do to test this is to download a REST client, POSTMAN if you're using Chrome, and issue a GET request to the url of the svg. The response will be the SVG image. Thinking now in terms of Java, you may have to do some parsing in your code to grab just the actual svg element because the website may return extra junk wrapping the embedded svg.
I have used Batik and I think it's not a good solution for many reasons for what you're trying to do. In the past I ended up writing Java code that executed a 3rd party program for image conversion. It was basically a Command class that wrapped the execution of phantomjs. Download phantomjs, and use the rasterize.js file in the examples folder to achieve quick and easy image conversion from .svg to .png or .jpg. At the command line, the command for phantomjs is something like:
phantomjs rasterize.js C:\sourceImage.svg C:\outputImage.png
If you are doing image manipulation, I did it a lot using ImageMagick as phantomjs is only good for rendering svg to a rasterized image format.
In your case what you want to do is for every svg image at the url, GET the svg, parse it into a String, write that String to a file, then do something like:
String command = "C:\\phantomjs\\phantomjs.exe C:\\phantomjs\\rasterize.js C:\\source.svg C:\\output.png"
Process process = Runtime.getRuntime().exec(command);
Obviously make your code more general, replacing the values in the command string with resusable variables.
If this is in the context of a commercial platform, you can install phantomjs and your java app on a single server, and then just connect this app via REST endpoints to your svg finder app that gets the images. When your svg finder app gets an image, have it parse it, format it, then POST it to the phantomjs server for rendering and uploading/storage.
Just save the part of the HTML file between the <svg> tags (including the <svg>). Give it a .svg extension. You should then be able to open it in a browser, or pass it to Batik, ImageMagick or some other converter.

How to generate an image from a snippet of HTML code (as it is interpreted by the browser) on fly at runtime

Can anyone guide how to generate image from input html code by Java API or Jquery library?
otherwise, how can I make a screenshot of a snippet of HTML code as it is interpreted by the browser?
for example :
if I have this HTML code :
<h1>Logo</h1><img src="http://blog.stackoverflow.com/wp-content/uploads/stackoverflow-logo-300.png">
I will generate an image which includes a screenshot of this code as it's interpreted by the browser
See: rendering html to png (server-side)
And as you want a Java API, Html2Image is the best solution for you.

Generate a PDF and then download as JPEG or image file

I am using iText to generate PDF and is working fine, and I can also download it via browser as PDF. However, is it possible for java or iText to convert it to JPEG or any IMAGE file and allow users to download the image file.
response.setContentType("application/pdf; charset=utf-8");
Merely changing the contentType to image/jpg is not possible. I am continuously looking for answer but struggling to find one.
Any idea would be a lot of help
I dont know more about iText. But using PDFBox we can convert pdf document into images.
After splitting you can push images to response.
Here some reference links :
http://pdfbox.apache.org/commandlineutilities/PDFToImage.html
Converting a PDF into multiple JPGs with iText or other
http://www.javatpoint.com/example-to-display-image-using-servlet
you can use iText only for generating a pdf nothing else. see the link http://itextpdf.com/itext.php . see this to convert a pdf to image. See this link as well for clearer understanding with an example.

Categories

Resources