views:

61

answers:

4

Hi all, How can I detect the number of printing page will be on a form using JavaScript?

I need to get the number of pages that a form would be so I can stick a watermark per page to be printed.

Thanks.

+3  A: 

AFAIK, it's just not possible. There's no way plain JavaScript can interact with your printer to determine settings, page numbers, or anything, without an ActiveX control or other plug-in.

You can, in some browsers, use CSS to specify where pages will break and keep track of page numbers that way, but you have to rely on the users not to zoom the page or increase the text size of the page before they print.

Cory Larson
+2  A: 

I don't think that can be done (except, as marcgg points out, with CSS3, but that is not widely enough in use yet).

You could give the browser some pointers by inserting elements with the page-break-after or page-break-before attribute, but that will not give you total certainty about what way your pages get printed in.

Depending on your layout, you may be able to work something out by placing an absolutely positioned image in relation to an element that has page-break-before: always. I've never tried that, though, and you would have to play around with it to see whether it's any good.

Background images are obviously not an option, as they are excluded from printing by default in all browsers I know.

I think if you want to get reliable watermarks, you will have to resort to generating PDF files, for example using the PHP-Based fpdf library.

Pekka
+4  A: 

I'd recommend that you read this article by A List Apart

@page front-matter :left {
  @bottom-left {
    content: counter(page, lower-roman);
  }
}

(this uses CSS3)

marcgg
+1, great to know for future use.
Pekka
+3  A: 

Considering that even somewhat standard paper size differ (letter vs. A4), this is not really feasible. I usually generate a PDF when I need this kind of control.

RedFilter