window.print的字体大小 [英] font size for window.print
问题描述
我使用window.print打印gridview内容,但我无法更改字体大小,这是我使用的代码:
function printGrid(){
var gridData = document.getElementById('<%= gvList.ClientID%>');
var windowUrl ='';
//为gridview设置打印文档名
var uniqueName = new Date();
var windowName ='Print_'+ uniqueName.getTime();
var prtWindow = window.open(windowUrl,windowName,'left = 0,top = 0,right = 0,bottom = 0,width = screen.width,height = screen.height,margin = 0,0, 0,0' );
prtWindow.document.write('< html>< head>< font size =5> TITLE< / font>< / head>');
prtWindow.document.write('< body style =background:none!important; font-size:10pt!important>');
prtWindow.document.write(gridData.outerHTML);
prtWindow.document.write('< / body>< / html>');
prtWindow.document.close();
prtWindow.focus();
prtWindow.print();
prtWindow.close();
}
此代码有什么问题?
方法是错误。没有font
这样的元素可用作head
的子元素。但即使你创建了一个特殊的打印视图页面,其中包含正确设置的字体,但它仍然有效。
我会告诉你什么是文明的方法。理想情况下,您甚至不应该创建字符串的特殊打印表示。相反,您可以将CSS与@media元素一起使用:
http://www.w3 .org / TR / CSS2 / media.html [ ^ ],
http://www.w3schools.com/css/css_mediatypes .asp [ ^ ],>
https://developer.mozilla.org/en-US/docs/CSS/ @media [ ^ ]。
这样,你可以为不同的媒体提供相同元素的不同CSS风格,例如print和screen:
@ media print {
体 { font-size : 10pt}
}
@ media screen {
正文 { font-size : 13px}
}
@ media screen , print {
body { line-height : 1.2}
}
此外,即使是内容也可能不同。有很多元素不适合打印版本。例如,单击打印版本中的按钮或复选框是没用的。您可以通过CSS属性隐藏打印版本中的所有不需要的元素,例如可见性和/或显示。
另请参阅我过去的答案:如何减少页面大小通过打印机打印一些结果时的代码? [ ^ ]。
-SA
i use a window.print to print gridview content, but i can't change the font size, here is the code i used:
function printGrid() { var gridData = document.getElementById('<%= gvList.ClientID %>'); var windowUrl = ' '; //set print document name for gridview var uniqueName = new Date(); var windowName = 'Print_' + uniqueName.getTime(); var prtWindow = window.open(windowUrl, windowName, 'left=0,top=0,right=0,bottom=0,width=screen.width,height=screen.height,margin=0,0,0,0'); prtWindow.document.write('<html><head><font size="5">TITLE</font></head>'); prtWindow.document.write('<body style="background:none !important; font-size:10pt !important">'); prtWindow.document.write(gridData.outerHTML); prtWindow.document.write('</body></html>'); prtWindow.document.close(); prtWindow.focus(); prtWindow.print(); prtWindow.close(); }
anything wrong in this code?
The approach is wrong. There is no such element asfont
to be used as a child element ofhead
. But even of you created a special "print view" page with properly set fonts and it worked, it still would be wrong.
I'll tell you what is the civilized approach. Ideally, you should not even create a special print representation of a string. Instead, you can use CSS with @media elements:
http://www.w3.org/TR/CSS2/media.html[^],
http://www.w3schools.com/css/css_mediatypes.asp[^],
https://developer.mozilla.org/en-US/docs/CSS/@media[^].
This way, you can provide different CSS style of the same elements for different media, such as "print" and "screen":
@media print { body { font-size: 10pt } } @media screen { body { font-size: 13px } } @media screen, print { body { line-height: 1.2 } }
Moreover, even content can be different. There are a lot of elements not suitable for print version. For example, it's useless to "click" on a button or a check box in the printed version. You can hide all unwanted elements from the print version through CSS properties, such as "visibility" and/or "display".
See also my past answer: How to reduce the page size in the code when printing some results through printer ?[^].
—SA
这篇关于window.print的字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!