Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Any way to export a JSP page to a word document?

 
Gautam Ry
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Is there any way to export a JSP page to a word document?

Thanks and Regards
Gautam
 
Ram Para
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try adding these lines at the top of JSP.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="application/msword; charset=windows-1252" %>


 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a higher quality approach, consider creating RTF instead of HTML - it's not a complicated format to generate.
 
Gautam Ry
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
thanks for your useful reply. Now, I can export the jsp file to a word document.

Regards
Gautam
 
Gautam Ry
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Now, I can export. But a new problem is appearing. The tool bar option is not appearing at all in word document while user open the document without saving.
Any idea??

regards
Gautam
 
Ram Para
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you opening the doc in the browser.
just check that ?
 
Jai babu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai,

Everything working fine. But one problem is the images are referring to the server path. So if i open the saved word document in another system without internet then the images are not displaying. Can anyone please how to overcome this.

thanks
Jai
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes it a more interesting question. If you save the word document as XML, it will throw the images into the XML as binary data. Then, all the images will be self contained in one document, and you can allow the user to download it.
 
Jai babu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your reply. I am not sure that i understand your approach was correct. When the save as dialog box was opened i saved the file as .xml file. But the same problem persist if i open that .XML file in word without internet.


I had the following line in my JSP page.

<%@ page contentType="application/msword;charset=utf-8" %>

If i open that page in my browser a save as dialog box will appear to save the file as word document. It is working fine till this as i expected to save the JSP page as word document.

The problem is, i had the css reference as
<link href="http://www.domain.com/sampleapp/css/sample.css" rel="stylesheet" type="text/css"/>
and the image as image tag with src pointing to my server path where the application is running.

Until if i have the internet connected the document shows all the tables well formatted as in css and the image is getting displayed. If i disconnect the internet the tables are not formatted and the image is not getting displayed. This is because the word document cannot able to connect to the css reference and image path in the server.

So how to embed the images inside the word document while saving the file from JSP as mentioned above so that i can see the images in word document eventhough internet was disconnected.


Thanks
Jai
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was answering about a straight download. I see now that you want a generated jsp page downloaded as a Word Doc. This approach can still help you.

I'm guessing that you have a link to allow the user to "download this page as word doc" and you've stuck the content-type tag in there and found that your images and formatting are lost.

You see that your images can be converted to binary and embedded in the page. So instead of src="server/images/aPicture.jpg"> you use


where the steam of characters is your image converted into binary.
That should make your images carry through.

for the formatting, you either pull your stylesheet directly into the document instead of linking to it, or, if that isn't good enough you look at the XML tags that Microsoft word uses to do the styling:

That means nothing to a server or a browser, but the MS Word at the client will know what to do with it.
Microsoft Word does the work of setting the style with its own dialect of XML. in your generated jsp, get rid of your stylesheet and replace it with Microsoft's XML, as seen in the document you've saved as XML.
instead of generating html tags with class names that correspond to a stylesheet, generate Microsoft XML tags.
 
Jai babu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim,

Once again thanks for your reply. I had done exactly what you said. I converted the image as byte array and given to src in the img tag.
in my JSP report page i had set image tag as< img src="sample.jsp">. In sample.jsp page i had the following code.

<%@ page import="java.io.*,java.net.*" %>
<%@page contentType="image/gif" %><%
OutputStream o = response.getOutputStream();
URL hp = new URL("http", "localhost", 8084, "/security/img/healthentic_logo_very_small.gif");
URLConnection hpCon = hp.openConnection();
InputStream is = hpCon.getInputStream();
byte[] buf = new byte[32 * 1024]; // 32k buffer
int nRead = 0;
while( (nRead=is.read(buf)) != -1 ) {
o.write(buf, 0, nRead);
}

o.flush();
o.close();// *important* to ensure no more jsp output
return;
%>

When i view the report in browser the page shows the image reading from the sample.jsp page as byte array. But when i save it as word document with help of content-type="application/msword" the word document doesn't show the image as it refers only to the broken path../../appname/sample.jsp which is not available to the document.

If my flow is correct can you please tell what had went wrong. Or if it is not possible by setting through JSP content-type then what is the way i have to implement to save the JSP report page as word document. The report page has huge data.

Thanks
Jai
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!