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

iframe and printing issues

P: 9
Hi,

I have an excel spreadsheet that is displayed within an iframe.

I print the sheet displayed by using the function:

function printExcel(){

window.excelFrame.focus();
window.excelFrame.print();

}

This does the job BUT the sheet is printed out over 4 pages in a larger font than had I printed the page from excel itself. The original excel sheet is set up so that the contents of the sheet fit all onto one page.

Is there anything I can do to control the print area of the contents of the iframe?
Does anyone have any suggestions?

Thanks in advance.
Sep 11 '08 #1
Share this Question
Share on Google+
16 Replies


acoder
Expert Mod 15k+
P: 16,027
If the Excel sheet is not contained within an iframe, does it print how you want it to?
Sep 11 '08 #2

P: 9
Yes - when the excel is not in the iframe it prints just the one page. When I print it in the iframe it prints the contents of the sheet over 4 pages.
Sep 14 '08 #3

acoder
Expert Mod 15k+
P: 16,027
What code are you using for displaying the Excel sheet? You may need to use a print stylesheet to control the printing.
Sep 14 '08 #4

P: 9
I simply have an iframe called "excelFrame" and one button called Print. When the user clicks the button --> onClick="printExcel()" -->

function printExcel(){

window.excelFrame.focus();
window.excelFrame.print();

}

The excel sheet prints but it prints the sheet our over 4 pages. If I print directly from the original file using excel, the sheet prints out nicely on one page.

How can I control the printing area etc. of the iframe using stylesheets?
Sep 15 '08 #5

P: 9
By the way, here is all the code - obviously fileName is the address of the excel file on the local machine:

[HTML]<% String fileName = request.getParameter("fileName"); %>

<script type='text/javascript'>

function printExcel(){
window.excelFrame.focus();
window.excelFrame.print();
}

</script>

<HTML>
<BODY>
<input type="button" id="print" onClick="printExcel()" value="Print">
<iframe id="excelFrame" src="<%=fileName%>" width="90%" height="80%"></iframe>
</BODY>
</HTML>[/HTML]
Sep 15 '08 #6

acoder
Expert Mod 15k+
P: 16,027
See Going to Print. You can try setting the font size in the print stylesheet.
Sep 15 '08 #7

P: 9
I tried this:

[HTML]<link rel="stylesheet" href="print.css" type="text/css" media="print" />

<div id="printDiv" name="printDiv">
<iframe class="tborder" id="excelFrame" src="<%=fileName%>" width="90%" height="80%" frameborder="0"></iframe>
</div>
[/HTML]
And then in my print.css file:

Expand|Select|Wrap|Line Numbers
  1. #printDiv {  font-size: 9pt; }
  2.  
But this does not seem to have any affect whatsoever.

I'm not even sure why logically it would make sense to set the font-size. My excel spreadsheet contains all sorts of fonts and even images. So how would font-size affect those?
If I had instead of an excel, an image loaded into the iframe would setting the font size in the iframe make a difference to the size of the image when it is printed from the iframe?
Sep 15 '08 #8

omerbutt
100+
P: 638
I tried this:

<link rel="stylesheet" href="print.css" type="text/css" media="print" />

<div id="printDiv" name="printDiv">
<iframe class="tborder" id="excelFrame" src="<%=fileName%>" width="90%" height="80%" frameborder="0"></iframe>
</div>

And then in my print.css file:

#printDiv { font-size: 9pt; }

But this does not seem to have any affect whatsoever.

I'm not even sure why logically it would make sense to set the font-size. My excel spreadsheet contains all sorts of fonts and even images. So how would font-size affect those?
If I had instead of an excel, an image loaded into the iframe would setting the font size in the iframe make a difference to the size of the image when it is printed from the iframe?
when working with printing the document you have to keep these two things in mind
1. the pixel should be avoided
2.points should be preffered
WHY ? because there is not any such thing like PIXELS in the world of printing / printers they take up or read every thing in points
and similarly there is nothing like POINTS in the world of computer screen they read up every thing in pixels
regards,
Omer
Sep 15 '08 #9

P: 9
I'm confused - that is what I did - I set the font-size to 9 points...
Sep 15 '08 #10

P: 9
When I check out Print Preview in Excel of the document, I can see that it is scaled down 54% to be printable on one page. Is their something equivalent that I can do to the document when it is loaded into the iframe?
Sep 15 '08 #11

acoder
Expert Mod 15k+
P: 16,027
I'm not even sure why logically it would make sense to set the font-size. My excel spreadsheet contains all sorts of fonts and even images. So how would font-size affect those?
The reason why I suggested font size was that you mentioned in your original post that the font size was larger in the print out from the iframe. The reason your code had no effect was that an iframe is a separate document, so the stylesheet would have to be inside the iframe. I guess it's not as simple as I had originally thought which I had assumed based on your original post.
If I had instead of an excel, an image loaded into the iframe would setting the font size in the iframe make a difference to the size of the image when it is printed from the iframe?
No, it wouldn't. Do the images come out the right size when printed over 4 pages?
Sep 15 '08 #12

acoder
Expert Mod 15k+
P: 16,027
When I check out Print Preview in Excel of the document, I can see that it is scaled down 54% to be printable on one page. Is their something equivalent that I can do to the document when it is loaded into the iframe?
Not with JavaScript, but if your target is only one specific browser for a controlled user base, you could consider using a browser-specific solution. If not, you would have to include the stylesheet within the iframe.
Sep 15 '08 #13

P: 9
I am only going to be using IE - what sort of possible browser-specific solution is there?

By the way I tried to include a stylesheet within the iframe but setting the font size didn't change the output in any way either. I thought I would have to set the dimensions if it were possible of the "content" somehow because I am assuming that the iframe treats the excel sheet like it would an image and just displays the true size of it.
Sep 15 '08 #14

acoder
Expert Mod 15k+
P: 16,027
You can use ActiveX. There's an ActiveX control called Meadroid that you may find useful.

If you go with the HTML/CSS route, you could point the src to an HTML page which contains the Excel sheet contained within a div of the right size. I haven't tried it, so wouldn't know if it works.
Sep 15 '08 #15

P: 9
I checked out the ActiveX control called Meadroid. It seems that it only works with html files that are loaded into iframes and not actual documents. When I linked to my excel sheet it did not recognise it and all it gave me to print was a blank page (even though the excel appeared in iframe in the browser).

I have now given up with the idea of displaying the excel in an iframe. Instead I have created a button that when clicked will open up a new window and display the excel there. This way when using the print option from the browser menu, the sheet prints out exactly the way it should.

For those who may be interested, this is all I placed in the jsp/html file to get the excel to load up in the window:

<%
String fileName = request.getParameter("fileName");
%>

<meta HTTP-EQUIV="Content-Type" CONTENT="application/vnd.ms-excel"; />
<meta http-equiv="refresh" content="10;URL=<%=fileName%>"> </meta>

Meanwhile, thanks to acoder who has tried to help me with my problem.

Over and out.
Sep 16 '08 #16

acoder
Expert Mod 15k+
P: 16,027
A much simpler solution. There's probably something out there that someone knows that can get the iframe printing to work as desired, but in the meantime, I guess this will have to do. Thanks for posting.
Sep 16 '08 #17

Post your reply

Sign in to post your reply or Sign up for a free account.