1

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

PleaseStand
  • 31,641
  • 6
  • 68
  • 95
tzador
  • 2,535
  • 4
  • 31
  • 37

4 Answers4

3

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.

Michael Mullany
  • 30,283
  • 6
  • 81
  • 105
  • Any update here? Just to clarify: Your answer means that it is not possible at all to get the default print paper size (neither via CSS nor via JavaScript). Is it at least possible to make an educated guess? – Martin Thoma Sep 06 '17 at 16:00
  • 1
    No, it's impossible. You can make an educated guess of A4 vs letter by sniffing language - but that's about it. – Michael Mullany Sep 07 '17 at 02:07
  • 1
    And looking at the browsers language is a pretty bad approach as the United States, Canada, Mexico are using letter size and pretty much any other country is using A4. So English is relatively mixed. – Martin Thoma Sep 07 '17 at 06:42
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

2

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).

Community
  • 1
  • 1
Oliver Bock
  • 4,829
  • 5
  • 38
  • 62
0

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

Ariya Hidayat
  • 12,523
  • 3
  • 46
  • 39