Skip to content

Commit dde1e84

Browse files
authored
Merge pull request #727 from fanthos/patch-1
Fast renderer should support initialPageNo
2 parents 4ed1880 + 48898c1 commit dde1e84

File tree

6 files changed

+62
-16
lines changed

6 files changed

+62
-16
lines changed

openhtmltopdf-core/src/main/java/com/openhtmltopdf/outputdevice/helper/BaseRendererBuilder.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,19 @@ public TFinalClass useExternalResourceAccessControl(
626626
return (TFinalClass) this;
627627
}
628628

629+
/**
630+
* Allows the setting of the initial page number to use with the
631+
* <code>page</code> and <code>pages</code> CSS counters.
632+
* Useful when appending to an existing document.
633+
* Must be one or greater.
634+
*
635+
* @return this for method chaining.
636+
*/
637+
public TFinalClass useInitialPageNumber(int initialPageNumber) {
638+
state._initialPageNumber = initialPageNumber;
639+
return (TFinalClass) this;
640+
}
641+
629642
public enum TextDirection {
630643
RTL, LTR
631644
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<html>
2+
<head>
3+
<style>
4+
@page {
5+
size: 300px 150px;
6+
margin: 25px;
7+
8+
@top-center {
9+
content: counter(page) " of " counter(pages);
10+
}
11+
@bottom-center {
12+
content: element(ftr);
13+
}
14+
}
15+
.footer::before {
16+
content: "Footer on page " counter(page) " of " counter(pages);
17+
}
18+
body {
19+
margin: 0;
20+
}
21+
</style>
22+
</head>
23+
<body>
24+
<div class="footer" style="position: running(ftr);"></div>
25+
26+
<h2 style="margin-top: 0;">Page five</h2>
27+
<h2 style="margin-top: 0; page-break-before: always;">Page six</h2>
28+
</body>
29+
</html>

openhtmltopdf-examples/src/test/java/com/openhtmltopdf/visualregressiontests/VisualRegressionTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,16 @@ public void testIssue649MultipleBgImagesPageBox() throws IOException {
14131413
assertTrue(vt.runTest("issue-649-multiple-bg-images-page-box"));
14141414
}
14151415

1416+
/**
1417+
* Tests setting an initial page number with CSS page and pages counters.
1418+
*/
1419+
@Test
1420+
public void testPr727InitialPageNumber() throws IOException {
1421+
assertTrue(vt.runTest("pr-727-initial-page-number", builder -> {
1422+
builder.useInitialPageNumber(5);
1423+
}));
1424+
}
1425+
14161426
// TODO:
14171427
// + Elements that appear just on generated overflow pages.
14181428
// + content property (page counters, etc)

openhtmltopdf-java2d/src/main/java/com/openhtmltopdf/java2d/api/Java2DRendererBuilder.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,6 @@ public Java2DRendererBuilder useLayoutGraphics(Graphics2D g2d) {
3737
return this;
3838
}
3939

40-
/**
41-
* Used to set an initial page number for use with page counters, etc.
42-
*
43-
* @param pageNumberInitial
44-
* @return this for method chaining
45-
*/
46-
public Java2DRendererBuilder useInitialPageNumber(int pageNumberInitial) {
47-
state._initialPageNumber = pageNumberInitial;
48-
return this;
49-
}
50-
5140
/**
5241
* Whether to use fonts available in the environment. Enabling environment fonts may mean different text
5342
* rendering behavior across different environments. The default is not to use environment fonts.

openhtmltopdf-pdfbox/src/main/java/com/openhtmltopdf/pdfboxout/PdfBoxRenderer.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ public class PdfBoxRenderer implements Closeable, PageSupplier {
138138
private PageSupplier _pageSupplier;
139139

140140
private final Closeable diagnosticConsumer;
141-
141+
142+
private final int _initialPageNumber;
143+
142144
/**
143145
* This method is constantly changing as options are added to the builder.
144146
*/
@@ -269,6 +271,8 @@ else if (doc.file != null) {
269271
}
270272

271273
this._os = state._os;
274+
275+
this._initialPageNumber = state._initialPageNumber;
272276
}
273277

274278
public Document getDocument() {
@@ -477,7 +481,7 @@ public void createPDF(OutputStream os, boolean finish) throws IOException {
477481
@Deprecated
478482
public void createPDF(OutputStream os, boolean finish, int initialPageNo) throws IOException {
479483
if (_useFastMode) {
480-
createPdfFast(finish);
484+
createPdfFast(finish, initialPageNo);
481485
return;
482486
}
483487

@@ -493,7 +497,7 @@ public void createPDF(OutputStream os, boolean finish, int initialPageNo) throws
493497

494498
RenderingContext c = newRenderingContext();
495499
c.setInitialPageNo(initialPageNo);
496-
500+
497501
PageBox firstPage = pages.get(0);
498502
Rectangle2D firstPageSize = new Rectangle2D.Float(0, 0,
499503
firstPage.getWidth(c) / _dotsPerPoint,
@@ -523,7 +527,7 @@ public void createPDF(OutputStream os, boolean finish, int initialPageNo) throws
523527
/**
524528
* Go fast!
525529
*/
526-
private void createPdfFast(boolean finish) throws IOException {
530+
private void createPdfFast(boolean finish, int initialPageNo) throws IOException {
527531
boolean success = false;
528532

529533
XRLog.log(Level.INFO, LogMessageId.LogMessageId0Param.GENERAL_PDF_USING_FAST_MODE);
@@ -537,7 +541,7 @@ private void createPdfFast(boolean finish) throws IOException {
537541
List<PageBox> pages = _root.getLayer().getPages();
538542

539543
RenderingContext c = newRenderingContext();
540-
c.setInitialPageNo(0);
544+
c.setInitialPageNo(initialPageNo != 0 ? initialPageNo : _initialPageNumber);
541545
c.setFastRenderer(true);
542546

543547
PageBox firstPage = pages.get(0);
@@ -596,6 +600,7 @@ private void writePDFFast(List<PageBox> pages, RenderingContext c, Rectangle2D f
596600

597601
int pageCount = _root.getLayer().getPages().size();
598602
c.setPageCount(pageCount);
603+
599604
firePreWrite(pageCount); // opportunity to adjust meta data
600605
setDidValues(doc); // set PDF header fields from meta data
601606

0 commit comments

Comments
 (0)