tags:

views:

375

answers:

6

What is the CSS code to do a page break when printing? Or better yet, is there some print CSS framework like JQuery is to web page DOM manipulation? I need to print web pages with control like Adobe PDF, but without using a third party plug-in. Prints need to be precise, like controlling what pixels hit the top of each page, (if we want to put our logo on each page header for example.

Any ideas?

+10  A: 

page-break-before: always;

page-break-after: always;

sea36
+5  A: 

You can try to force a page break using the page-break-before or page-break-after properties.

Pixel-perfect print control will be very, very hard to achieve. For example, each browser adds a footer and header section that you cannot influence and only the user can turn off. Browsers disregard background images, and tend to set margins as they see fit.

For a rundown on how to style HTML for print, this Smashing Magazine article may provide some pointers: Printing the Web: Solutions and Techniques

Pekka
+1  A: 

The w3schools.com has a handy CSS Reference. Since most of us cannot remember many uncommon css properties, hope this is useful for you.

XUE Can
+2  A: 

Browser support for printing is very weak, at best. For example, getting something like a page header image to work is going to be pretty rough; I don't know of any browsers that even support the "@page" CSS declaration, let alone the running content features of CSS3.

The FlyingSaucer PDF toolkit for Java, on the other hand, does a remarkable job of transforming XHTML into PDFs (especially given that it's free). We use it with Freemarker to template the "page", but it handles images, running header/footer text, even intra-document links.

Pointy
+1  A: 

You cannot get pixel-level print precision from a browser. Your only option is PDF or similar 3rd party document format. However, if you just want to ensure a logo is on the top of a page vs the bottom of the next, you can use the page-break-before, page-break-after properties

Andrew
+1  A: 

I'm not sure what server language you're using but I've had great success with wkhtmltopdf and wicked_pdf for Ruby on Rails.

Beerlington