aspose file tools*
The moose likes JSP and the fly likes JSP to RTF renders as text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP to RTF renders as text" Watch "JSP to RTF renders as text" New topic
Author

JSP to RTF renders as text

ken monroe
Greenhorn

Joined: Apr 15, 2005
Posts: 5
Hello!

I'm trying to use JSP to send content in RTF format and have it opened automatically by MS Word. The content I'm sending opens in Word as text, not as rendered RTF. My JSP page has the following:

<%@ page contentType="application/rtf;charset=utf-8" %>
<% response.setHeader("Content-Disposition","attachment; filename=" + "Letter.rtf" ); %>
{RTF content here}

I've also tried contentType="text/rtf" with the same results. I've used this same code to successfully go directly to Word using just:
<% response.setHeader("Content-Disposition","attachment; filename=" + "Letter.doc" ); %>

I want to go to RTF because I'm using a Word template with a background letterhead and it's easier to put my dynamic content in a RTF file.

Ideas??

Thx
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
ken monroe
Greenhorn

Joined: Apr 15, 2005
Posts: 5
Thx for the idea. That gave me the same results - text in MS Word.

After more poking around, I think the problem is related to a bunch of blank lines that occur before the opening "{\rtf1\ansi" of the RTF content.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

RTF is very very picky about whitespace and other formatting. You need to be sure that there is no extraneous whitespace before or within your rtf stream.

This is something that is hard to control with JSP. Have you considered using a servlet instead?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ken monroe
Greenhorn

Joined: Apr 15, 2005
Posts: 5
Looks like I might have to go the servlet route. Now, if I just knew how to go from a JSP to a servlet...

Thx
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Servlets are easy. Don't let them daunt you. Besides, we all hang out in the Servlets forum too!

And... it'll be good for you. I always recommend getting Servlets under your belt before delving into JSPs, so it will make your understanding of the fundamentals behind JSP that much stronger.
[ July 13, 2005: Message edited by: Bear Bibeault ]
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Hi Ken,

First, I second using a servlet. I actually think you will find it easier than doing this in a JSP page...

Second, I am in the design phase of a prroject that will contain similar functionality. Instead of using RTF, have you considered XML? It is relatively straightforward. Here is a fairly recent article describing the process. I am also considering using JACOB, but only because my application will need to take advantage of MS Word's Mail Merge feature. If you just want to create dynamic documents, I don't think JACOB would be worth the trouble.


“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

From that blog:

You all know that Microsoft Word (and other Office applications) can load and save XML


Actually, no I didn't. I haven't read the rest of that blog, but good gracious, dealing with XML would be so much easier than with RTF! (And I have the scars to prove it -- how did you think I know how picky RTF is about whitespace?)

Thanks for posting this Paul. It certainly bears further investigation.

But to point of the post, anytime you are emitting non-text output, or text output that is not white-space agnostic, avoid the use of JSP in favor of Servlets.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

Oh drat. So much for my enthusiasm. Apparently this requires Office 2003 or later which has only minor penetration in the market, making this unsuitable for use on the general web.

Sigh.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Bear Bibeault:
Oh drat. So much for my enthusiasm. Apparently this requires Office 2003 or later which has only minor penetration in the market, making this unsuitable for use on the general web.

Sigh.


Yep. Office 2005 is supposed to default save to an XML based file format. No more binary .doc files and should have a much smaller filesize on the disk. About time MS get caught up with technology being used today.


GenRocket - Experts at Building Test Data
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Gregg Bolinger:


Yep. Office 2005 is supposed to default save to an XML based file format. No more binary .doc files and should have a much smaller filesize on the disk. About time MS get caught up with technology being used today.


That's very Open Office of them.
Paul Bourdeaux
Ranch Hand

Joined: May 24, 2004
Posts: 783
Apparently this requires Office 2003 or later
Admittedly, I didn't realize that either...

Upon a more careful reading, it is apparent that you need Word 2003 or later to save in XML, but I can't tell if older versions of Word can open the generated documents or not. Perhaps someone out there with a pre-2003 version of Word can go to the working example in the article and let me know if they could open the document or not...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP to RTF renders as text