opening a client application from a server?

C

ccc31807

This relates to the post I made last week about working with
WordProcessingML. That part turned out to be easy -- as easy as
writing a CGI script and dynamically updating the HTML from a
database. WordProcessingML is no different from XHTML and does pretty
good at creating Word documents.

Here's the question: The client has a requirement that the users
create a Word document on a web enabled app and open it in the client
browser. I can create the word document and open it on the server
(using this command -- exec("c:/Program Files/Microsoft Office/
OFFICE12/winword.exe", "c:/TEST1.DOC")) but I can't get Word to open
the file on the client. I can write the .doc file to a directory and
give the client to the directory over the internet, but that doesn't
meet the requirement.

I've tried creating hyperlink like <a href="test.doc">Here is your
Word document</a> but that doesn't work either.

I'm about to tell my client that I can't meet this requirement ...
I've been trying since Monday and have about given up. Any ideas?

Thanks, CC.
 
T

Tad J McClellan

ccc31807 said:
Here's the question: The client has a requirement that the users
create a Word document on a web enabled app and open it in the client
browser. I can create the word document and open it on the server
(using this command -- exec("c:/Program Files/Microsoft Office/
OFFICE12/winword.exe", "c:/TEST1.DOC")) but I can't get Word to open
the file on the client.


If I'm understanding you correctly, then you do not have a Perl problem.

If you send the appropriate (application/word?) Content-Type, then a
client on Windows, with Word installed, should be able to open it
no problem.
 
S

smallpond

If I'm understanding you correctly, then you do not have a Perl problem.

If you send the appropriate (application/word?) Content-Type, then a
client on Windows, with Word installed, should be able to open it
no problem.

Here's an example showing the headers which IE opens fine:

wget -S http://www.ahrq.gov/QUAL/diabqguide.doc
--17:36:37-- http://www.ahrq.gov/QUAL/diabqguide.doc
Resolving www.ahrq.gov... 77.67.91.107
Connecting to www.ahrq.gov|77.67.91.107|:80... connected.
HTTP request sent, awaiting response...
HTTP/1.0 200 OK
Content-Length: 1694208
Content-Type: application/msword
Last-Modified: Fri, 30 May 2008 17:20:13 GMT
Accept-Ranges: bytes
ETag: "10bb8b6b79c2c81:6b7"
Server: Microsoft-IIS/6.0
p3p: CP="NOI ADM DEV PSAi COM NAV OUR OTR STP IND DEM"
X-Powered-By: ASP.NET
Date: Thu, 07 May 2009 21:36:37 GMT
Connection: keep-alive
Length: 1694208 (1.6M) [application/msword]
Saving to: `diabqguide.doc.1'
 
C

ccc31807

If I'm understanding you correctly, then you do not have a Perl problem.

It's very possible that it's an Apache problem, but maybe not. I can
invoke Word and open the .doc file in an ordinary Perl script, like
this: eval("c:/.../winword.exe" "c:/.../wordprocessingml.doc")

However, it doesn't work in a CGI script, and frankly I don't know
enough about Windows to troubleshoot it. I'm meeting with my client
tomorrow morning, and either I'll get it to work before then or I'll
suggest some alternatives.
If you send the appropriate (application/word?) Content-Type, then a
client on Windows, with Word installed, should be able to open it
no problem.

My job entails producing a lot of reports in various formats (csv,
pdf, txt, etc) and I have a lot of experience giving users access to
these formats. Producing the Word document with WPML was easy, but now
I'm stuck with producing files that I can't seen to open. Ignorance
isn't bliss!

CC
 
J

Jürgen Exner

ccc31807 said:
It's very possible that it's an Apache problem, but maybe not.

No, it's not an Apache problem, either. It's an HTTP problem.
I can
invoke Word and open the .doc file in an ordinary Perl script, like
this: eval("c:/.../winword.exe" "c:/.../wordprocessingml.doc")

However, it doesn't work in a CGI script, and frankly I don't know

Oh yes, it does work in a CGI script. Given proper permissions the
script will/should happily invoke Word ---- on the server! Most likely
not what you had in mind.
enough about Windows to troubleshoot it.

Has nothing to do with Windows but everything with how HTTP works.
I'm meeting with my client
tomorrow morning, and either I'll get it to work before then or I'll
suggest some alternatives.


My job entails producing a lot of reports in various formats (csv,
pdf, txt, etc) and I have a lot of experience giving users access to
these formats. Producing the Word document with WPML was easy, but now
I'm stuck with producing files that I can't seen to open. Ignorance
isn't bliss!

So, what Content-Type did you try?

jue
 
C

ccc31807

jue, thanks for your comments.

No, it's not an Apache problem, either. It's an HTTP problem.

This is what I can't understand. I import a big glob of data from a
database into my script, generate many csv files, or txt files, or pdf
files, or xml file, etc., and generate links. The browsers, IE but FF
as well will happily open a csv file in Excel (assuming that the
client is a Windows machine with Office installed), open a pdf file in
Acrobat, open a txt file in the browser window, and so on.

I ASSUMED(!) that a doc file would work the same way, that is, open
natively in Word on a machine running Office. It doesn't, and I don't
know enough to understand why it doesn't.
Oh yes, it does work in a CGI script. Given proper permissions the
script will/should happily invoke Word ---- on the server! Most likely
not what you had in mind.

Right. What I had in mind was writing an OUTFILE to a directory and
creating a link to the file. I frequently create HTML files
dynamically from a script (similar to what I suppose eBay, Amazon,
Google, and all the rest do) and the links work flawlessly. Invoking
Word on the server is useless, you are right about that.
Has nothing to do with Windows but everything with how HTTP works.
So, what Content-Type did you try?

I didn't. When I produce csv, pdf, etc., I don't specify any Content-
Type. I just create the files and they open the way they should. XML
is very picky, and Word demands a PERFECT(!) XML file to open,
otherwise it throws an error, and I suspect that an attempt to
incorporate a Content-Type element in the XML would create a problem.
Besides, I don't understand why a Content-Type is necessary.

I don't mind admitting ignorance, but I'm not stupid, and I do learn
-- slowly but surely.

CC
 
A

A. Sinan Unur

It's very possible that it's an Apache problem, but maybe not. I can
invoke Word and open the .doc file in an ordinary Perl script, like
this: eval("c:/.../winword.exe" "c:/.../wordprocessingml.doc")

What is this new wave of people thinking that a web server can invoke a
specific application on a client computer?

I am simply blown by this failure to grasp the most basic elements of
how things fit together.
However, it doesn't work in a CGI script, and frankly I don't know
enough about Windows to troubleshoot it.

You do not need to know anything about Windows.
My job entails producing a lot of reports in various formats (csv,
pdf, txt, etc) and I have a lot of experience giving users access to
these formats.

How is a Word document different from any of those formats? Tad has just
told you how to solve your problem.

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
J

Jürgen Exner

ccc31807 said:
jue, thanks for your comments.



This is what I can't understand. I import a big glob of data from a
database into my script, generate many csv files, or txt files, or pdf
files, or xml file, etc., and generate links. The browsers, IE but FF
as well will happily open a csv file in Excel (assuming that the
client is a Windows machine with Office installed), open a pdf file in
Acrobat, open a txt file in the browser window, and so on.

Which IMNSHO is a major mistake of the browser. The browser should never
assume a file type based on the last 3 characters of a file name.
I ASSUMED(!) that a doc file would work the same way, that is, open
natively in Word on a machine running Office. It doesn't, and I don't
know enough to understand why it doesn't.


I didn't. When I produce csv, pdf, etc., I don't specify any Content-
Type. I just create the files and they open the way they should. XML
is very picky, and Word demands a PERFECT(!) XML file to open,
otherwise it throws an error, and I suspect that an attempt to
incorporate a Content-Type element in the XML would create a problem.

You don't incorporate it into the XML, you set the Content-Type in the
HTTP response header just as Tad explained already.
And no, it really has nothing to do with Perl.
Besides, I don't understand why a Content-Type is necessary.

Because it tells the browser what to do with that particular HTTP
response.

jue
 
C

ccc31807

I am simply blown by this failure ...
                   ^
                  away

*Sigh*

Reminds me of a very crude example of how capitalization or the lack
thereof can change the meaning, as in, "I helped my Uncle Jack off a
horse."

CC
 
J

Jürgen Exner

Jürgen Exner said:
ccc31807 said:
jue, thanks for your comments.



This is what I can't understand. I import a big glob of data from a
database into my script, generate many csv files, or txt files, or pdf
files, or xml file, etc., and generate links. The browsers, IE but FF
as well will happily open a csv file in Excel (assuming that the
client is a Windows machine with Office installed), open a pdf file in
Acrobat, open a txt file in the browser window, and so on.

Which IMNSHO is a major mistake of the browser. The browser should never
assume a file type based on the last 3 characters of a file name. [...]
Besides, I don't understand why a Content-Type is necessary.

Because it tells the browser what to do with that particular HTTP
response.

Following up on my own post:

You may want to check the configuration of your brower(s).
In Firefox see "Options" -> "Applications".

For me "Microsoft Word Document" is set to "Always ask" and that's
exactly what my Firefox does, I just tested it. Uploading a FooBar.doc
file to a web site, accessing the corresponding URL in Firefox and a
dialog pops up asking what Firefox should do with that file, open it, in
which application, or saving it.

jue
 
C

ccc31807

What is this new wave of people thinking that a web server can invoke a
specific application on a client computer?

It's not the web server that invokes the application on the client.
The client requests a file, the server sends the file if it's there,
and the client is responsible for finding the correct application to
open the file. You can request all sorts of documents from a web
server: gif, jpg, wav, avi, mpeg, mp3, txt, html, xml, pdf, rtf, and
so on. All the web server does is deliver the file. It's up to the
client to do something with it.
How is a Word document different from any of those formats? Tad has just
told you how to solve your problem.

Okay, maybe I am dense. Let's say I produce the following file like
this, which will place a file named 'outfile.xml' in the current
directory:

open OUTFILE, '>', 'outfile.xml';
print OUTFILE qq(<?xml version="1.0" encoding="utf-8" ?>
<document>
<name>
<first>Barack</first>
<middle>H.</middle>
<last>Obama</last>
</name>
<occupation>President</occupation>
</document>);
close OUTFILE;

If I create a hyperlink on an HTML page, like
<a href="outfile.xml">XML DOC</a>
a user can click the link, and the web server will return outfile.xml
to the browser.

If I give the file an xls extension, on a Windows machine running IE
the file will open in Excel.

I just don't understand where I should specify the content type. BTW,
I have done something like this every day for years, sometimes
producing as many as several hundred files, and I've never, not once,
specified a content type. Either I've been real, real lucky, or I
don't need to specify a content type.

I'm not trying to be abusive or ugly or insulting or obstinate. I just
don't understand, and I'm asking for help.

Thanks, CC.
 
J

Jürgen Exner

ccc31807 said:
It's not the web server that invokes the application on the client.
The client requests a file, the server sends the file if it's there,
and the client is responsible for finding the correct application to
open the file. You can request all sorts of documents from a web
server: gif, jpg, wav, avi, mpeg, mp3, txt, html, xml, pdf, rtf, and
so on. All the web server does is deliver the file. It's up to the
client to do something with it.

And exactly there was the big misunderstanding. Everyone else was
assuming you were talking about an embedded document.

Actually Tad's solution won't work with those plain files, just like
mine and Sinan's won't.

As you said yourself, it is a client configuration thing, the browser
must be configured to open MS Word.
In FireFox it's "Options" -> "Applications", in other browsers it's
probably some other location.

jue
 
A

A. Sinan Unur

It's not the web server that invokes the application on the client.

Exactly. However, you snipped what you said in your post. My response
above was directed to the following nonsensical remark by you:

1. What is eval doing there?
2. You are mentioning this in the context of a CGI script running
on a web server.
Okay, maybe I am dense. Let's say I produce the following file like
this, which will place a file named 'outfile.xml' in the current
directory:

Are you going to create a file for every request?
open OUTFILE, '>', 'outfile.xml';
print OUTFILE qq(<?xml version="1.0" encoding="utf-8" ?>
<document>
<name>
<first>Barack</first>
<middle>H.</middle>
<last>Obama</last>
</name>
<occupation>President</occupation>
</document>);
close OUTFILE;

If I create a hyperlink on an HTML page, like
<a href="outfile.xml">XML DOC</a>
a user can click the link, and the web server will return outfile.xml
to the browser.

This is just silly. Just provide the appropriate response headers
and content in your response.

But then, you are the one keeps insisting that writing code without a
plan or design to 'get things done' is the right strategy.

Now, you want others to help you make money because you haven't
done your homework and you are up against a deadline.

Well ...

What you are asking would be implemented in exactly the same way
in all languages. Your question is not about Perl.

Here is your fish. Enjoy.

#!/usr/bin/perl

use strict;
use warnings;

use CGI::Simple;

my $cgi = CGI::Simple->new;

print $cgi->header(
-type => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
-attachment => 'report.docx',
),
<<EO_DOCX;
<?xml version="1.0"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
<w:body>
<w:p>
<w:r>
<w:t>Hello, World.</w:t>
</w:r>
</w:p>
</w:body>

</w:wordDocument>
EO_DOCX





--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
T

Tad J McClellan

Let's say I produce the following file like
this,


How the file is produced in not relevant, so I snipped it.

If I create a hyperlink on an HTML page, like
<a href="outfile.xml">XML DOC</a>
a user can click the link, and the web server will return outfile.xml
to the browser.

If I give the file an xls extension, on a Windows machine running IE
the file will open in Excel.


Sounds like your server is not configured to know what content
type goes with an extension of .doc.

I just don't understand where I should specify the content type.


In the HTTP headers, specifically in a header named "Content-Type".

If you have a CGI program where you've slurped in the contents of
the Word file into $doc, then this should do it:

print "Content-Type: application/msword\n\n";
print $doc;

If it is a static link like in your example above, then you must
have your server configured to send the correct Content-Type header.

My apache, for instance, uses the /etc/mime.types file
to associate a file extension with a content-type.

grep msword /etc/mime.types
finds:
application/msword doc

If the CGI approach above works, yet the link approach does not work,
then you can be pretty sure that the root cause of your problem has to
do with web server configuration.

It most certainly has nothing to do with Perl...
 
C

ccc31807

And exactly there was the big misunderstanding. Everyone else was
assuming you were talking about an embedded document.

This is my fault. I should have been clearer. The requirement is to
produce a Word document merging data obtained as a result of querying
a database with a template, so that the user can open the file in Word
and edit it if necessary. I'm using the browser as the interface to
the database, and the client wants to use a hyperlink to allow the
user to open the document.
As you said yourself, it is a client configuration thing, the browser
must be configured to open MS Word.
In FireFox it's "Options" -> "Applications", in other browsers it's
probably some other location.

Thanks for the suggestion. I'll try it in the morning.

CC.
 
C

ccc31807

1. What is eval doing there?

Sorry for the confusion. This is actually from an old script that
opened Word for another purpose, and I (perhaps confusedly)
resurrected it as a desperate measure.
2. You are mentioning this in the context of a CGI script running
   on a web server.

Yes, see my reply to jue.
But then, you are the one keeps insisting that writing code without a
plan or design to 'get things done' is the right strategy.

I don't recall ever saying this. I have stated that I use ad hoc
methods for short scripts less than about 50 lines or so. but this is
different from just throwing code on the wall to see if any sticks.
Once you write the same code several hundred times, you don't need to
plan it -- you can do it in your sleep.
Now, you want others to help you make money because you haven't
done your homework and you are up against a deadline.

Hey, any port in a storm. ;-)
Here is your fish. Enjoy.

Which defeats the purpose. I'm /NOT/ writing a script to produce an
output file for a web server. I /AM/ producing the output file to
write to the file system. The 'eval' point you made above came from a
script that (1) queried a database, (2) parsed the data and stuffed it
into a hash, (3) created the outfile from the data, (4) invoked Word,
and (5) exited. It wasn't a CGI script and didn't use a browser for an
interface, but was run from the command line and didn't involve a web
server.

Anyway, Sinan, thanks for your help. I haven't run your code, but am
curious if it will actually work in Word 2007. So far, I haven't
gotten Word to open a document produced with WPML with a docx
extension, but Word 2007 works fine with a plain doc extension.

CC.
 
A

A. Sinan Unur

Which defeats the purpose. I'm /NOT/ writing a script to produce an
output file for a web server.

Huh? Are you playing games here?

The subject line says:

Re: opening a client application from a server?

In addition, your original post states clearly:
(e-mail address removed):


I /AM/ producing the output file to write to the file system.

Which file system? Where do you think your client is going to save the
file after s/he has edited it?
I haven't run your code, but am curious if it will actually work in
Word 2007.

I don't know. I have Word 2003.

And, of course, I tested the script before posting it.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
J

Jürgen Exner

l v said:
If you are dynamically creating the document, you don't need to write it
to the file system and generate a link in a web page for the user to
click on. You can simply send the document directly to the client
browser *if* you send the proper html content type.

Thank you, I think you very nicely explained the basic assumption
everyone was making.
One minor point: it would be a content type in the HTTP header and just
to make absolutely sure: it would not be set to "HTML".

jue
 
A

A. Sinan Unur

....
....

Not to put words in what Sinan and Jurgen are saying.

If you are dynamically creating the document, you don't need to write
it to the file system and generate a link in a web page for the user
to click on. You can simply send the document directly to the client
browser *if* you send the proper html content type.

Minor nit:

s/html content type/content type/

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top