13

I need to know if I can modify content on the last page with the :last selector.

I'm not sure if it exists, I see it being used in other stackoverflow answers like this: Footer on last printed page. But I can't find it in the documentation and it does not work when I try to use it.

I'm trying to clear the content on the footer of my last page like this:

@page {
        @bottom-right {
            content: "Please turn over";
        }
}

@page :last {
        @bottom-right {
            content: none;
        }
}

It works when I use it with the :firstselector. How can I get the effect for the last page?

I'm using Weasyprint to print PDF files.

Community
  • 1
  • 1
Niel
  • 1,856
  • 2
  • 23
  • 45

2 Answers2

9

This can be achieved using named pages.

Create an element on the last page (or use an existing one that will appear on the last page) and assign it a last-page class.

Example below:

HTML

<div class="last-page"></div> <!-- Or add this class to an existing element that appears on the last page -->

CSS

.last-page {
    page: last_page;
    page-break-before: always; /* Use if your last page is blank, else omit. */
}

@page {
   @bottom-right {
       content: "Please turn over";
    }
}

@page last_page {
    @bottom-right {
        content: none;
    }
}

Tested with Weasyprint - worked a charm.

bagage
  • 1,094
  • 1
  • 21
  • 44
Matt Doyle
  • 867
  • 8
  • 12
  • 1
    It seems plausible, but this doesn't seem to work when you try to print a page out on Chrome browser. – r1987 Feb 13 '20 at 07:06
  • 2
    You're a genius. Works fine for producing PDF at least, displays fine in-browser on FF & Chrome (recent versions at least) – logicOnAbstractions Mar 15 '22 at 13:04
  • Thanks for the solution. This is really helpful. but still I have a problem. I applied the same in my code and it generates an empty last page which can't be avoided. Can you please give any clue? Thanks – Chanaka NZ Oct 27 '22 at 00:24
4

Based on the CSS3 Page docs it appears the :last pseudo-class was removed (or never included).

It might be possible to target the last page using the :blank pseudo-class if you can force a page break at the end of your document. This might have unwanted effects on other blank pages though.

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
  • Thanks for clearing that up. I thought of using `:blank` as an option as well. – Niel Feb 09 '17 at 12:39
  • Trying to think of some other options - something like `:right + :not(:left)` but that would need testing to see if it works. – Brett DeWoody Feb 09 '17 at 12:44
  • It kind of makes sense. Clearly a PDF printer knows where a document starts and its current position. The last page depends on all the content before it, unless explicitly set manually. I don't know of a way to set "document ends here", but I'd imagine any software attempting should first render the whole document to determine the last page and then retrieve and restyle this page to achieve this. – G_V Jun 18 '19 at 07:59
  • @G_V so when you print number of pages on each page it is not confusing driver, but when you need set last page's property - it confusing. Excellent. – Anton Trishenkov Nov 18 '19 at 18:36