- Saved searches
- Use saved searches to filter your results more quickly
- CSS counter for total pages? #1636
- CSS counter for total pages? #1636
- Comments
- Advanced HTML to PDF API features
- Turning header repeating off
- Forcing page breaks
- Rendering page numbers
- Rendering HTML elements in the page margins
- Element location
- Positioning margin elements
- You made it!
- HTML2PDF.js Add Page Numbers at Top in PDF Using JavaScript
- HTML2PDF.js Page Numbering Source Code
- index.html
- Output Screenshot
- Related posts:
- Leave a Comment Cancel reply
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CSS counter for total pages? #1636
CSS counter for total pages? #1636
Comments
Read some suggestions on using:
footer .pagenum:before
To get the current page number. Is it possible to get the total number of pages through css as well, to show something like Page 4 of 5 ?
The text was updated successfully, but these errors were encountered:
https://codepen.io/Bhupinderkumar/pen/gKzKGw here is pen you can see the total page number at the bottom of the page
Here is way to do it, while still being able to use CSS on the paging information.
The current page number can be generated via CSS features, as documented in samples, but the total page count must be injected after the rendering happened. To do that we use an arbitrary placeholder, DOMPDF_PAGE_COUNT_PLACEHOLDER , in our HTML template and replace that string in the entire rendered PDF, juste before it get outputted.
> html> head> title>footitle> style> footer < /* Place the footer at the bottom of each page */ position: fixed; left: 0; right: 0; bottom: 0; /* Any other appropriate styling */ color: #4f82d6; font-weight: bold; > /* Show current page number via CSS counter feature */ .page-number:before < content: counter(page); > style> head> body> footer> Page span class pl-s">page-number">span> of DOMPDF_PAGE_COUNT_PLACEHOLDER footer> Very long content spanning multiple pages here . body> html>
function htmlToPdf(string $html): string < $dompdf = new Dompdf(); $dompdf->loadHtml($html); $dompdf->setPaper('A4'); $dompdf->render(); injectPageCount($dompdf); return $dompdf->output(); > /** * Replace a predefined placeholder with the total page count in the whole PDF document * * @param Dompdf $dompdf */ function injectPageCount(Dompdf $dompdf): void < /** @var CPDF $canvas */ $canvas = $dompdf->getCanvas(); $pdf = $canvas->get_cpdf(); foreach ($pdf->objects as &$o) < if ($o['t'] === 'contents') < $o['c'] = str_replace('DOMPDF_PAGE_COUNT_PLACEHOLDER', $canvas->get_page_count(), $o['c']); > > >
Advanced HTML to PDF API features
Say you have an HTML table with 100 rows. When you render a long table like this in a browser, the table content can expand forever. In the browser world, you don’t need to worry about pages at all—which page your table rows land on, how many can fit on a page, etc.
Render this same table in a PDF, however, and you’ll need to deal with page breaks. If a table is longer than a single page, or starts half way down a page, rows will need to be broken up and rendered across multiple pages.
Turns out a page full of table rows without header context isn’t very user friendly. For example, if you printed out the following, its second page would be a little confusing without the headers. What is that 3rd column again?
Ideally we’d render the table headers on each page that shows the table’s rows, then someone doesn’t need to look at a previous page to understand the headers.
Table header repeating like this is supported and is turned on by default! All you have to do is make sure you put your header th in a thead element:
Qty Description Price Subtotal 2 Blue large widgets $15.00 $30.00
Then the headers render again on pages that get the overflow rows:
Repeating also works with table footers when a tfoot element is used:
. 2 Blue large widgets $15.00 $30.00 Table Footer
Here’s the header and footer repeating:
All the table context, all the time!
Turning header repeating off
While table header repeating is on by default, you can turn it off with a custom CSS rule -fs-table-paginate :
table.no-header-repeat -fs-table-paginate: none; >
Or you can disable it by simply not using thead , tbody , and tfoot elements.
Forcing page breaks
Sometimes you need a piece of content to always start on its own page, no matter the content that came before it. You can force page breaks with the page-break-before: always rule on any element. It will create a new page, then set the element with the page-break-before CSS rule as the first element of that page.
Here’s a simple example with three pages:
Lonely Page 1 content class="new-page">Page 2 content class="new-page">Page 3 content
.new-page page-break-before: always; >
Rendering page numbers
Page numbers are a fixture in most PDFs. Often PDFs will have a Page 2 of 3 style numbering scheme on each page.
You achieve this by injecting page numbers into any element by using a bit of special CSS. You target the content of your chosen element’s ::after pseudo-element with a special directive:
class="page-container"> Page class="page"> of class="pages">
.page-container .page::after content: counter(page); > .page-container .pages::after content: counter(pages); >
The targeted elements can be on any page you’d like. But it’s likely most important to render the page number information on each page, in one of the margins, which brings us to our next section.
Rendering HTML elements in the page margins
Many PDFs have content in the margins on every page. For example, page numbers, your company name, phone number, etc. The HTML renderer allows rendering anything your heart desires in the margins by way of a «running» element.
You render an element in the body like any other element, define the element as a «running» element, then tell each page to use that element in one of the defined margin areas.
Here’s an example that uses the page number example above and renders it in the bottom right margin on every page.
class="page-container"> Page class="page"> . content.
.page-container /* Define this element as a running element called "pageContainer" */ position: running(pageContainer); > @page /* Use any of these locations to place your margin elements: @top-left, @top-left-corner @top-center @top-right, @top-right-corner @bottom-left, @bottom-left-corner @bottom-center @bottom-right, @bottom-right-corner */ @bottom-right /* Reference "pageContainer" to be the content for the bottom right page margin area */ content: element(pageContainer); > >
The output using the page breaks and applying a little styling to the page numbers:
Element location
A «running» element’s location in the HTML determines which pages it shows on. If you want it in a margin element on all pages, just make sure to place the «running» element before all non-running HTML elements in the HTML code. For example, place all your margin elements that show on all pages right after the element.
To see this behavior in action, using the page number and page break examples above, we can start the page numbering on the 2nd page by placing the running pageContainer element on the second page.
Lonely Page 1 content class="new-page">Page 2 content class="page-container"> Page class="page"> class="new-page">Page 3 content
.page-container /* Define the this element as a running element called "pageContainer" */ position: running(pageContainer); > .new-page page-break-before: always; > @page @bottom-right /* Reference "pageContainer" to be the content for the bottom right page margin area */ content: element(pageContainer); > >
Here’s the output. Notice there is no Page 1 on the first page:
Positioning margin elements
Margin elements may not render exactly where you want them to. It’s possible to place them exactly where you want with margin-top .
class="margin-content"> Margin Content
.margin-content position: running(marginContent); /* Position the element with margins */ margin-top: 10px; /* Style margin element like any other */ font-size: 12px; color: #c00; > @page @top-left content: element(marginContent); > >
You made it!
Hopefully these features go a long way to giving you full control in creating PDFs that look exactly as you want.
There are even more advanced features in there that we’ll be talking about in coming months. If you experiment and are struggling to accomplish what you’d like with the HTML to PDF API, don’t hesitate to contact us:
support@useanvil.com
HTML2PDF.js Add Page Numbers at Top in PDF Using JavaScript
In this tutorial, you will learn how to add page numbers in PDF documents using JavaScript and HTML2PDF.js library. The full source code of HTML2PDF.js Page Numbering is given below.
HTML2PDF.js Page Numbering Source Code
index.html
div < outline: red solid 1px; page-break-inside: avoid; >#nextpage1