Any stylesheet rules that are declared in a specific media section are only applied to that media. Hence, if you set margins in a print media, it will only affect the page when printing (and something like print-preview). So you don't have to programmatically change them at time of printing, just set them when you send the page to the client under a media section for print.
Note that these aren't printer page margins, they're just like margins for how the content is displayed in a browser, but instead of a window, it's paper. If you're trying to increase margins, that will probably work fine, but decreasing likely won't. There may be client-side scripting ways to change formal page margins, but I don't know how. I think that's beyond what CSS can do (though I could be wrong, anyone to support or refute?).
________________________________________
Andrew