Submitting an SVG image to a Servlet - java

I'm working on some reports on a Java web application. One of the requirements I have is to be able to export the report data as PDF. The reports are made up of several elements, mostly HTML tables and SVG charts courtesy of Highcharts.
Highcharts has a built-in function that exports their graphics to PDF, but I need a document that contains the other HTML data as well, so there's no other choice but to write my own code. Since the Highcharts graphics are created on the client-side, I need to submit their SVG output to the server in order to be able to include the images on the PDF document.
My first, perhaps naive, approach, was to have a form with a hidden input such as:
<form id="fileExport" method="POST" action="servlet/FileExportServlet">
<input type="hidden" id="svgParam" name="svgParam" />
</form>
And then I would set the hidden input's value to the graphic's svg code like this:
$("div#getPDF").live("click", function()
{
//the chart svg data is inside a div with class highcharts-container
//I copy the svg to the hidden input that I will submit
$("#svgParam").val($(".highcharts-container").html());
//submit the form with the hidden input
$("#fileExport").submit();
});
The problem I'm facing, is that apparently the SVG data is too large for the hidden input's value, so when it reaches the server it is truncated. I'm submitting the form in this fashion because I don't wan't to refresh the page in order to have the download start.
I was thinking that perhaps I could encode the SVG element as a Data URI, but I suppose that it wouldn't prevent truncation either, although it would produce a shorter string most of the time.
Does anyone know a way to transfer the SVG data back to the server? (Preferably allowing some other parameters as well)
Thanks

If your form is using the POST action, data will not get truncated.
Having said that, using a text camp to send binary data is unsettling. I would try either:
a) Sending it as a file attachment (but then probably your user would need to set the field value).
b) Sending it directly to your server (for example, using CURL), separately from your HTML
c) At the very least, keep using the hidden field but at least using encode64 with the data.

Related

How to convert HtmlPage into Html string in HtmlUnit

I want to convert a page into a real HTML string, with <html>, <body>, etc..., not XML. I only see the asXml() function, which often changes many things in the structure.
Also note that I've performed modifications to the page after fetching it and I want those modifications to be present in the output as well.
How can I do that? Thank you so much.
So let me check if I got it right:
You fetched a page
You performed modifications to the page (EG: modifying nodes in it)
You want a valid HTML page containing the previous modifications as a String
page.asXml() will not help. This will return a valid XML file as a String rather than a valid HTML file.
page.getWebResponse().getContentAsString() will not help either. This will returned the response that the server gave you as it is (without any modification that you have made).
There is no other method that would return a string with a valid HTML String.
However, you could try using page.save(file). That would save the page the modified page to a file as HTML. Sadly, I don't think there is a method that receives an OutputStream so you're most likely to have to save the file to a file system and then get it back.
Probably, you could take a look at the HTMLUnit source and see how that method is implemented. Maybe adding your own save method is not that complex :)

How can I send a newsletter with xPages content?

I have some content displayed using computed fields inside a repeat in my xpage.
I now need to be able to send out a newsletter (by email) every week with the content of this repeat. The content can be both plain text and html
My site is also translated into different languages so I need the code to be able to specify the language and return the content in that language.
I am thinking about creating a scheduled lotusscript or java agent that somehow read the content of the repeat. is this possible? if so, some sample code to get me started would be great
edit: the content is only available to logged in users
thanks
Thomas
Use a java agent, and instead of going to the content natively, do a web page open and open the page as if in a browser, then process the result. (you could make a special version of the web page that hides all extraneous content as well if you wanted)
How is the data for the repeat evaluated? Can it be translated in to a lotusscript database.search?
If so then it would be best to forget about the actual xPage and concentrate on working out how to get the same data via LotusScript and then write your scheduled agent to loop through the document collection and generate the email that way.
Looking to the Xpage would generate a lot of extra work, you need to be authenticated as the user ( if the data in the repeat is different from one user to the next ) to get the exact same data that this particular user would see and then you have to parse the page to extract the data.
If you have a complicated enough newsletter that you want to do an Xpage and not build the html yourself in the agent, what you could do is build a single xpage that changes what's rendered based on a special query string, then in your agent get the html from a URLConnection and pass the html into the body of your email.
You could build the URL based on a view that shows documents with today's date.
I would solve this by giving the user a teaser on what to read and give them a link to the full content.
You should check out Weihang Chens (my colleague) article about rendering an xPage as Mime and sending it as a mail.
http://www.bleedyellow.com/blogs/weihang/entry/render_a_xpages_programmtically_and_send_it_as_a_mail?lang=en_us
We got this working in house and it is very convenient.
He describes 3 different approaches to the problem.

Java - Insert Byte Array/ImageIcon directly into HTML

I'm trying to take a byte array directly from the DB and put it into a tooltip that takes HTML. This is for consistency as text needs to be included that will match the other text-only tooltips.
I'm ideally trying to bypass the step of saving the image to a file before loading it into an img src tag. Is there a way to load a byte array (could easily be converted into Image or ImageIcon beforehand) straight into HTML?
Make an image-serving servlet, map it to an appropriate URL and just dump the image data from the database.
Edit: by the way you can have the browser cache the images with this approach, but you to do it manually.
You need to convert the bytes to base64 encoding somehow (either on the java side or the html/javascript side). And then you can write something like This:
<img src="data:image/gif;base64,R0lGODlhUAAPAKIAAAsLav///88PD9WqsYmApmZmZtZfYmdakyH5BAQUAP8ALAA AAABQAA8AAAPbWLrc/jDKSVe4OOvNu/9gqARDSRBHegyGMahqO4R0bQcjIQ8E4BMCQc930JluyGRmdAAcdiigMLVrpTYWy5FKM1IQe+Mp+L4rphz+qIOBAUYeCY4p2tGrJZeH9y79mZsawFoaIRxF3JyiYxuHiMGb5KTkpFvZj4ZbYeCiXaiKBwnxh4fnt9e3ktgZyHhrChinONs3cFAShFF2JhvCZlG5uchYNun5eedRxMAF15XEFRXgZWWdciuM8GCmdSQ84lLQY5R14wDB5Lyon4ubwS7jx9NcV9/j5+g4JADs=" alt="" width="80" height="15" />
Note that n this case, even though you save a request to fetch the image, the image itself is not cached for future use.
You may look at this:
http://www.websiteoptimization.com/speed/tweak/inline-images/
For a workaround.

Simple custom tag parsing in java

I have a client that wants to insert videos, images, form elements, etc. into his text while also keeping html elements that tinymce generates.
One thing thing that came to mind is to create special tags that lets him do this, and then use a transformation engine that takes the input -> output.
So for a video tag, it could inject the necessary javascript to make the video player (like a youtube-like player), and for an image, it just makes an image tag and for a form element, it creates an input tag.
For the forms, I thought using ${name} would be alright. Name would be unique identifier for the value that the program could use. He's just have to make sure he didn't duplicate them.
I guess for images and video, I could use BB Code-style tags like [IMG] and [VIDEO].
Is there anything that already does stuff like this in the java space, or do I have to code it from scratch?
A good customizable parser that can transform BBCodes or you're own tags in for example HTML:
http://kefir-bb.sourceforge.net/

How to get AJAX generated HTML text?

AJAX is a very powerful tool so I am struggling with it :-).
Is there any way or API(in java) so that I can get the HTML code which is generated by AJAX?
Generally, AJAX make use of inner HTML code and hence this inner HTML code is missing when I look into the page source of a page.
e.g click here
Just see the section OTHER NEWS. The content is populated by AJAX. When I look into the page source the code is not there.
I need this HTML code through a java program. How can I get it?
To have a Java application use the content received via AJAX, you need to first find the URLs from where the content is getting called from. In case this it would be http://itm2083.com/get_wwo_content.php?featureGroupId=8355&featureDisplayLimit=1&sponsorName=vortalx&wwoDivCounter=5&domainUrlForWWo=http://item2083.com/&featureImgDisplay=FLAG_TRUE&featureGroupImageWidthLimit=200&featureGroupDefaultImageUrl1=http://wwo.itmftp.com/75x75.gif&featureGroupDefaultImageUrl2=http://wwo.itmftp.com/75x75.gif&featureGroupDefaultImageUrl3=http://wwo.itmftp.com/75x75.gif
The featureGroupId= parameter has 5 IDs: 8355, 8359, 8367, 8369, 8429. Use these to pull the content from the Other News box.
The featureDisplayLimit= parameter determines how much content is pulled from the server.
If you want the nice HTML as well, the Java app will have to recreate it, as the HTML rendered on the site is created by JavaScript code.

Categories

Resources