Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

counter-reset: page #93

Open
hejsan opened this Issue · 7 comments

3 participants

@hejsan

Is there a plan to support counter-reset?

I have to generate a pdf combined of a lot of reports for different students. Each report needs to have individual page numbers, i.e. not 1/117 thru 117/117 but instead each one would have numbers relative to just that one report, more like 1/5 thru 5/5 and so on.

So optimally counter-reset: page; should reset the counter to 1 and also make the counter(pages) evaluate correctly the number of pages since the last reset.

Is that even doable?

As a backup plan, do you know of any commandline tool to merge multiple pdf's into one that installs on centos?

Thanks for a superb tool btw.

@SimonSapin
Owner

Hi. In this case I’d recommend using the low-level API to render multiple document separately, then write them to a single PDF file. Something like this:

documents = [HTML(string=render_template(student)).render() for student in students]
documents[0].copy([page for doc in documents for page in doc.pages]).write_pdf(target)
@SimonSapin
Owner

Note: PDFtk can also concatenate PDF files, but that’s probably less efficient than the above as it’s just doing more work.

@hejsan
@SimonSapin
Owner

Even with named page, you’d still need more CSS features (like a new page selector) to only reset the page counter on the first page for a given student.

The low-level API (separating the layout into pages form the painting and PDF writing) was designed for this kind thing, and is already supported. What feature do you mean exactly?

@hejsan

Hi, sorry to be unclear.

I may have misunderstood the css page standard (http://dev.w3.org/csswg/css-page/#page-based-counters)

I thought I could simply wrap each "subreport" like this:

<div class="student-report">
 ... report content ... 
</div>
<div class="student-report">
 ... report content ... 
</div>
...

and in the css I'd write:

@page {
    bottom-center {
        content: counter(page)/counter(pages);
    }
}
.student-report {
    counter-reset: page;
    page-break-after: always;
}

And this would have the effect that each student-report would have it's own page numbers and page count.

@SimonSapin
Owner

I find the spec a bit unclear on this point, but I’m told that this wouldn’t work because of the scoping rules. Your explicit counter-reset would create one scope, and the implicit increment in @page would be a new scope, so that you actually have two page counters.

Maybe we should fix the spec to allow this use case.

@ktross

I would love to see this supported in WeasyPrint. The only html to pdf renderer I have found that supports this is PrinceXML. Below is an example document that should have 5 total pages with 2 separate counters each having 3 and 2 total pages. Here is a pdf that I generated using the html below.

<!doctype html>
<html>
<head>
    <style>
    div.section {
        counter-reset:page 1 pages 1;
    }
    div.page {
        page-break-after:always;
    }
    @page {
        @top-center {
            content: "Page " counter(page) " of " counter(pages);
        }
    }
    </style>
</head>
<body>
    <div class="section">
        <div class="page">
            this should be page 1/3
        </div>
        <div class="page">
            this should be page 2/3
        </div>
        <div class="page">
            this should be page 3/3
        </div>
    </div>
    <div class="section">
        <div class="page">
            this should be page 1/2
        </div>
        <div class="page">
            this should be page 2/2
        </div>
    </div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.