Set Printing Preferences from JS

So... after all day of looking, I think that I found the answer to my own question. In short, the answer is that JS and/or CSS will not allow you to override the default page setup of the client's browser.

Here is what I tried. If you create a simple HTML file with and empty head, and a body that only contains the text "Test", you will be able to see what I tested.

  1. Print Style Sheets cannot solve your problem. Setting the margin on the body to 0px 0px 0px 0px is not the same thing as clicking File > Page Setup and setting all of your margins to 0. Despite what Print Style Sheeters would like you to believe, they are different. Try it for yourself. This is why Print Style Sheets do not solve your problem.

  2. JS cannot solve your problem. Can you image if every page that you visited modified your local JS properties? To allow each page to have access to your local print setups would be a security breach, and is not allowed. Because of this, JS cannot solve your problem.

I would love to have someone respond here and let me know that I am wrong. Otherwise... this sucks... and it needs to happen. I have a ton of old users... and getting them to set the margins in their page setup is a pain. Also the customer don't want us to refactor the page so that they don't need to. I am in-between a rock and a hard spot.


You can set page orientation and margin with CSS:

@page {
   size: landscape;
   margin: 10%
}

You can also set a footer at the bottom of each page by using the <tfoot> element.

EDIT: As pointed out by Beejamin, this is a half-way house. You can't really change printer preferences (those that appear in the Print Dialog when clicking Print). Also, IE is severely lacking in support for @page selector, (had a look around to see if IE9 supported it and couldn't find anything). As an alternative you could always try applying it to the body in your print stylesheet.

body {
   size: landscape;
   margin: 10%
}

Although off my head I'm not sure how effective this is.

Tags:

Javascript