Setting page margins

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Setting page margins

Post by tristanlee85 »

I figure this is something that is setup through browser/OS settings, but is there any way to change the page margins of a webpage using PHP, HTML, etc.? I want a page to print starting at almost the very top of the page. Here is how I have my HTMl currently set:

Code: Select all

<body leftmargin="0px" topmargin="0px" marginwidth="0px" marginheight="0px">
It helped a little bit, but are there any other methods?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You have little control over it since the browser and OS have final say as to what margins are used. If you generated a PDF, it's a little bit easier to control, but again, the reader and OS can alter it outside of your own control.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Why don't you use CSS?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Oren wrote:Why don't you use CSS?
Yep, CSS can have different settings for printers than it does for screens, and projectors.

Code: Select all

<style type="text/css" media="print">
body {
    padding: 0;
    margin: 0;
}
</style>
However, the printer will most likely add a small margin anyway... I doubt you can avoid that, even with PDF most home-printers have some sort of minimum margin.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

d11wtq wrote:However, the printer will most likely add a small margin anyway... I doubt you can avoid that, even with PDF most home-printers have some sort of minimum margin.
Well, it is possible. There are printers that do that.

P.S I'm not sure he was talking about printing the page on a sheet of paper.
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

Well, when I print this page out, there is about 3/4" white space on the top and botom of the page and I was wanting to fill that space of possible. Yes, I'm printing out the page to the printer.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I think this will have more to do with the users' browser page setup than how you use CSS. If you set your page setup margins to .25" across the board you will get smaller edges on the paper when printing than if they were set to 1". And I don't think you can write code that will change a user's browser settings (though I could be wrong about that).
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Most home-printers have some limitations and cannot print on the edge of the paper. Try changing the printer's settings and play with it.
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

That would seem like a security issue there. The CSS helped a little bit though. With the default settings on my Linux machine (Firefox) and on Windows (Firefox and IE), they printed out fine.
tristanlee85
Forum Contributor
Posts: 172
Joined: Fri Dec 19, 2003 7:28 am

Post by tristanlee85 »

My only problem is that I have the page set to the max. Meaning, when it prints out, it's using all the space possible because the page is that long. Also, it can only be one page so that's why Im worried about it. This is going to be used by multiple users so I don't want to make them have to change printer settings just to print it. I think I'm good though. the CSS added a little more room to play with and it's all on 1 page now.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If printing is critical, generate a PDF. It's far easier to control than getting a web page to look right when printed from the multitude of browsers and OS's out there.
Post Reply