Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP to RTF renders as text

 
ken monroe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:
<%@ page contentType="application/msword;charset=utf-8" %>
 
ken monroe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
ken monroe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic