How to find out the dimensionality of print paper in javascript

落花浮王杯 提交于 2020-01-14 03:11:09

问题


I am generating reports in HTML formats, and want to print them automatically using Qt (qt has a webkit based browser, and one can render its contents to a PDF file). I want to position the elements of the report depending on the target page size. For that i need to access the size of the paper (preferably in pixels).

Since i am using webkit, it has all the latest html5 and css3 features.

is there some api available to access printed page properties inside javascript. for example, would be nice to write: window.print_paper.width or window.print_paper.height

Thank you in advance


回答1:


What you're asking for is backwards from the standards-track @page spec. What CSS3 paged media (@page) allows you to do is specify an orientation and page size that your document should be printed on - not query your system for the currently selected orientation and page size. Even the CSS3 media query stuff won't allow you to do this, to the best of my knowledge.




回答2:


it's not possible. Javascript can't access to your printer to detect paper size. You can't even use pixel measurement because it's relative to the screen so you have to use other units like pt, inches, centimeters




回答3:


Use the Accept-Language request header to determine if the user is in a Letter-using locale, otherwise assume A4. Assume portrait if that suits your users. Then sniff the browser to guess at the margins (IE9 = =19.05mm, FF5 = 12.7mm, IE6 = 7.5mm, Chrome = ~10mm).




回答4:


You could get the contents size via QWebFrame::contentsSize() and the make it available to the JavaScript world, e.g. using QWebFrame::addToJavaScriptWindowObject().



来源:https://stackoverflow.com/questions/4236711/how-to-find-out-the-dimensionality-of-print-paper-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!