Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print page number while printing html in new Chrome?

I need print page number when printing html in Chrome Version 52.0.2743.116 m (64-bit). However I haven't found the answer yet.

I have already tried this:

@page {
    @bottom-right {
        content: counter(page) " of " counter(pages);
    }
}

This didn't work at all.

Then I have found this Q&A:


For this answer we are not using @page, which is a pure CSS answer, but work in FireFox 20+ versions. Here is the link of an example. The CSS is:

#content {
    display: table;
 }
#pageFooter {
    display: table-footer-group;
 }

#pageFooter:after {
    counter-increment: page;
    content: counter(page);
 }

And the HTML code is:

 <div id="content">
     <div id="pageFooter">Page </div>
     multi-page content here...
 </div>

Unfortunately, this works only in FireFox 20+ versions and Chrome Version 35.0.1916.153

Downgrade isn't an answer. Can you help me, please?

like image 624
Natalia Avatar asked Aug 24 '16 10:08

Natalia


People also ask

How do I print page numbers in Google Chrome?

On any website in Chrome, hit Ctrl+P on your keyboard to bring up the print dialog box.

How do I put page numbers in HTML?

If you are looking to add page numbers when printing under Chrome/Chromium, one easy solution is to use Paged. js. This JS library takes your HTML/CSS and cuts it into pages, ready to print as a book, that you will preview in your browser. It makes the @page and most the CSS3 specifications work for Chrome.

How do I print page numbers?

If you want page numbers to print in the header or footer of the form when users print it, use AutoText codes in the Insert Header and Insert Footer dialog boxes. On the View menu, click Header and Footer. Click the Print Settings tab. Under Headers and Footers, click Header or Footer.


1 Answers

This set up works for me, you just need an element at the end of each page to append a page counter onto.

Code:

HTML:

body {
  counter-reset: section;
}

@media print {
  .page {
    page-break-after: always;
  }
  .page .pageEnd::after {
    counter-increment: section;
    /* Increment the section counter*/
    content: "Page " counter(section) " ";
    /* Display the counter */
  }
}
<div class="page">
  <h2>Here is page 1</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas nulla accusantium, veritatis quidem voluptas ex, atque suscipit incidunt, dolore sunt reiciendis cum obcaecati, iste necessitatibus doloribus cumque facere beatae. Ratione?</p>
  <div class="pageEnd"></div>
</div>
<div class="page">
  <h2>Here is page 2</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas nulla accusantium, veritatis quidem voluptas ex, atque suscipit incidunt, dolore sunt reiciendis cum obcaecati, iste necessitatibus doloribus cumque facere beatae. Ratione?</p>
  <div class="pageEnd"></div>
</div>
<div class="page">
  <h2>Here is page 3</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas nulla accusantium, veritatis quidem voluptas ex, atque suscipit incidunt, dolore sunt reiciendis cum obcaecati, iste necessitatibus doloribus cumque facere beatae. Ratione?</p>
  <div class="pageEnd"></div>
</div>
<div class="page">
  <h2>Here is page 4</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas nulla accusantium, veritatis quidem voluptas ex, atque suscipit incidunt, dolore sunt reiciendis cum obcaecati, iste necessitatibus doloribus cumque facere beatae. Ratione?</p>
  <div class="pageEnd"></div>
</div>
<div class="page">
  <h2>Here is page 5</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas nulla accusantium, veritatis quidem voluptas ex, atque suscipit incidunt, dolore sunt reiciendis cum obcaecati, iste necessitatibus doloribus cumque facere beatae. Ratione?</p>
  <div class="pageEnd"></div>
</div>
<div class="page">
  <h2>Here is page 6</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas nulla accusantium, veritatis quidem voluptas ex, atque suscipit incidunt, dolore sunt reiciendis cum obcaecati, iste necessitatibus doloribus cumque facere beatae. Ratione?</p>
  <div class="pageEnd"></div>
</div>
<div class="page">
  <h2>Here is page 7</h2>
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quas nulla accusantium, veritatis quidem voluptas ex, atque suscipit incidunt, dolore sunt reiciendis cum obcaecati, iste necessitatibus doloribus cumque facere beatae. Ratione?</p>
  <div class="pageEnd"></div>
</div>
like image 53
Mark Wilkins Avatar answered Oct 07 '22 21:10

Mark Wilkins