By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,501 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,501 IT Pros & Developers. It's quick & easy.

printing an html table using javascripting

P: n/a
I have code which generates html table on the fly and fills it up with
data. i have to add a print button so that i am able to print the
output in html table to the printer. Also, is it possible to print it
to an excel file?

Nov 13 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
_printing_ to an excel file?
you could post a tab, comma or json formatted string to an ultra simple
server side script that just sends it straight back, with an excel
mimetype
<?php
header('Content-Type: application/vnd.ms-excel');
header('Content-Length: '.strlen($_POST['javascript_csv_string']));
echo $_POST['javascript_csv_string'];
exit;
?>
obviously this would allow people to post stuff to your webserver which
would then appear to be downloaded from you, so you would have to
implement some checking.

As for doing it straight from javascript, unless it is possible to use
an XHR call, inject client side data instead of a server response
payload, and setting the right headers. Perhaps, Ive never tried it.

however it is possible with an iframe, using this code
<html>
<head></head>
<body>
<iframe src="" id="file" name="file"></iframe>
<script type="text/javascript">
document.getElementById('file').src =
"data:application/vnd.ms-excel;base64,MSwyLDMsNA==";
</script>
</body>
</html>

you could use application/csv or text/csv instead of
application/vnd.ms-excel but then it isnt automatically opened as
required. Basically its a quick and dirty "yes you can" but how clean
you could get it, my opinion would be to try to use the header settings
power of XHR, together with some trickery to load the return call of
the XHR with data thats already hanging around, and have XHR do a call
that gets intercepted anyway.

Erm.... the fly in the ointment to all this, the Multi-$B company
Microsoft, who squeezed a few more millions into their bank account by
not bothering to implement the data url. sorry - this trick only works
in compliant browsers like Safari/Firefox/Opera and so on... IE7? hmm
let me guess.

Nov 13 '06 #2

P: n/a
oh I should have said, you will need a javascript implementation of
base64 so you can encode the csv string generated by js,
in this case
1,2,3,4
is
MSwyLDMsNA==
in base64

there are license free base64_encoders around for js.
Ive tested this on linux too, using the vnd.ms-excel forces an open
into openoffice if installed. As I say the only spanner in the works is
IE.

Nov 13 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.