Use page counter style and value in PDF page numbers
Is the interaction between the page counter and bookmark-level index part of the standard?
I have a document that starts on page 31. I can use @page:first { counter-reset: page 31; } and content: counter(page) works correctly. However, the page numbers in the sidebar TOC still count from 1.
Do I have any control over that?
Do I have any control over that?
You currently don’t, but there’s hope.
There’s a way to change the page numbers directly in the PDF document. This way, your first page would have number 31, and the TOC would be updated accordingly. The PDF specification document itself uses this feature to have Roman numerals at the beginning, and start at "1" on page 9.
PDF readers have the responsability to handle these numbers correctly, but the ones I’ve tested (pdf.js and Evince) handle them correctly.
When Cairo is gone (good news coming really, really, really soon), we’ll be able to implement this feature by hacking the PDF with our bare hands.
I see what you mean. My PDF viewer seems to display it correctly.
I didn't know you were phasing out Cairo.Is it too focused on interactive rendering and treats PDF as too much of an afterthought? I'll be watching this space for updates!

I didn't know you were phasing out Cairo.Is it too focused on interactive rendering and treats PDF as too much of an afterthought?
Cairo is an incredible library, but we’ll always miss a low-level access to the PDF internals for a lot of features (like this one). We’ve also had to live with many small bugs for a looooong time, because you can wait for years (!) before a new version is released.
(And we sometimes dream of removing all the non-Python dependencies. But that’s just a dream.)
If anyone would like to work on this, it should now be possible as we have our own PDF generator. Just ask, I’ll be happy to discuss on this topic!
If anyone would like to work on this, it should now be possible as we have our own PDF generator. Just ask, I’ll be happy to discuss on this topic!
Oh hi! It's been a while. Did you remove the Cairo dependency as you'd planned to? Congratulation on the milestone!
Oh hi! It's been a while. Did you remove the Cairo dependency as you'd planned to? Congratulation on the milestone!
Thanks! It’s actually been done in 2021 and already gave us the opportunity to add a few features such as PDF/A, PDF/UA, PDF tagging, smaller PDFs, custom image optimization, and more…
I don’t know how hard it would be to add this feature now, but I think that we have all we need to do so!
While we're here, I have a related question. I have a document, similar to the one in the screenshot, in that it has front matter and main body. Front matter has one numbering system, and it rests to 1 at the beginning of main text. Assuming this is possible at all, can you point me to an example of how to do it?
Assuming this is possible at all, can you point me to an example of how to do it?
I think that you want something like this:
<html>
<style>
h1 { break-before: page }
.front { page: front }
main h1:first-child { page: main }
@page {
@bottom-center {
content: counter(page);
}
}
@page front {
@bottom-center {
content: counter(page, lower-roman);
}
}
@page main {
counter-reset: page 1;
}
</style>
<body>
<section class="front">
<h1>title 1</h1>
<h1>title 2</h1>
</section>
<main>
<h1>title 3</h1>
<h1>title 4</h1>
</main>
</body>
</html>
There’s no way in CSS to change the numbering of the bookmarks though.