Skip to content
New issue

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

Rsdev 240 pdf page numbers #65

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public enum DATE_FOOTER_PREF {
private boolean provenance = true;
private boolean comments = true;
private boolean annotations = true;
private boolean pageName = true;
private boolean restartPageNumberPerDoc = true;
private boolean includeFieldLastModifiedDate = true;

@NotNull private ExportPageSize pageSize = ExportPageSize.A4;
Expand All @@ -67,11 +67,7 @@ public ExportScope getExportScope() {
return exportScope;
}

/**
* A name() String of an {@link ExportFormat} enum
*
* @param exportFormat
*/
/** A name() String of an {@link ExportFormat} enum */
public void setExportFormat(String exportFormat) {
this.exportFormat = ExportFormat.valueOf(exportFormat);
}
Expand Down Expand Up @@ -110,8 +106,8 @@ public String toString() {
+ comments
+ ", annotations="
+ annotations
+ ", pageName="
+ pageName
+ ", restartPageNumberPerDoc="
+ restartPageNumberPerDoc
+ ", pageSize="
+ pageSize
+ ", dateType="
Expand Down
30 changes: 22 additions & 8 deletions src/main/java/com/researchspace/export/pdf/PdfProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,32 @@ public void makeExport(
ExportToFileConfig config)
throws IOException {

int startPage;
// 1 pdf export can contain multiple docs
int pdfPageLength;
try {
startPage = doExportPdf(tempExportFile, documentData, strucDoc, config);
pdfPageLength = doExportPdf(tempExportFile, documentData, strucDoc, config);
} catch (DocumentException e) {
throw new IOException("Could not generate PDFWriter", e);
}
int startPage = calculatePageNumber(config, pdfPageLength);
config.setStartPage(startPage);
}

if (!config.isPageName()) {
config.setStartPage(startPage);
} else {
config.setStartPage(0);
/***
* The pdf writer expects the start page to be either:
* - 0 for a new doc, or when restarting the page numbering for each doc on a
* multi-doc/notebook export
* - the length of the existing pdf on multi-doc/notebook export when the user has selected
* not to restart the page numbering for each individual doc
* */
private int calculatePageNumber(ExportToFileConfig config, int pdfPageLength) {
if (!config.isRestartPageNumberPerDoc()) {
int currentStartPage = config.getStartPage();
return currentStartPage == 0
? currentStartPage + pdfPageLength
: currentStartPage + pdfPageLength - 1;
}
return 0;
}

private int doExportPdf(
Expand All @@ -85,7 +99,7 @@ private int doExportPdf(
ExportToFileConfig config)
throws DocumentException, IOException {

log.info("Before: {}", documentData.getDocumentAsHtml());
log.debug("Before: {}", documentData.getDocumentAsHtml());
documentData = preProcessHTML(documentData);

String html = pdfHtmlGenerator.prepareHtml(documentData, strucDoc, config);
Expand All @@ -103,7 +117,7 @@ private int doExportPdf(
renderer.setDocumentFromString(html);
renderer.layout();
try (FileOutputStream out = new FileOutputStream(tempExportFile)) {
renderer.createPDF(out);
renderer.createPDF(out, true, config.getStartPage());
}
return renderer.getWriter().getPageNumber();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<td><input id="pdf_a1IncludeFieldLastModifiedDate" type="checkbox" name="includeFieldLastModifiedDate" checked></input></td>
</tr>
<tr><td><fmt:message key="dialogs.pdfArchiving.label.numbering"/></td>
<td><input id="pdf_a1pagenbr" type="checkbox" name="pagename" checked></input></td>
<td><input id="pdf_a1pagenbr" type="checkbox" name="restartPageNumberPerDoc" checked></input></td>
</tr>
<tr><td><fmt:message key="dialogs.pdfArchiving.label.pageFormat"/></td>
<td><select id="pdf_a1size" name="pagesize">
Expand Down
4 changes: 2 additions & 2 deletions src/main/webapp/scripts/tags/exportOtherDialogs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/main/webapp/ui/src/Export/ExportDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ let pdfConfig = {
provenance: true,
comments: true,
annotations: true,
pageName: true,
restartPageNumberPerDoc: true,
pageSize: "A4",
defaultPageSize: "A4",
dateType: "EXP",
Expand Down
4 changes: 2 additions & 2 deletions src/main/webapp/ui/src/Export/PdfExport.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type PdfExportDetails = {|
provenance: boolean,
comments: boolean,
annotations: boolean,
pageName: boolean,
restartPageNumberPerDoc: boolean,
pageSize: PageSize,
defaultPageSize: PageSize,
dateType: "EXP" | "NEW" | "UPD",
Expand Down Expand Up @@ -49,7 +49,7 @@ const checkboxes = {
comments: "Include comments",
annotations: "Include image annotations",
includeFieldLastModifiedDate: "Include last modified dates for fields",
pageName: "Restart page numbering for each document",
restartPageNumberPerDoc: "Restart page numbering for each document",
includeFooter: "Insert date footer at file end only",
};

Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/ui/src/Export/__tests__/PdfExport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("PdfExport", () => {
provenance: false,
comments: false,
annotations: false,
pageName: false,
restartPageNumberPerDoc: false,
pageSize: "A4",
defaultPageSize: "A4",
dateType: "EXP",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"provenance" : true,
"comments" : true,
"annotations" : true,
"pageName" : true,
"restartPageNumberPerDoc" : true,
"includeFieldLastModifiedDate" : true,
"pageSize" : "A4",
"dateType" : "EXP",
Expand Down
35 changes: 25 additions & 10 deletions src/test/java/com/researchspace/export/pdf/PdfProcessorTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.researchspace.export.pdf;

import static com.researchspace.testutils.RSpaceTestUtils.loadTextResourceFromPdfDir;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;
Expand All @@ -9,7 +10,6 @@
import com.lowagie.text.pdf.parser.PdfTextExtractor;
import com.researchspace.model.User;
import com.researchspace.model.record.StructuredDocument;
import com.researchspace.service.UserExternalIdResolver;
import com.researchspace.testutils.TestRunnerController;
import java.io.File;
import java.io.IOException;
Expand All @@ -26,12 +26,6 @@

@ExtendWith(MockitoExtension.class)
public class PdfProcessorTest {
@Mock private UserExternalIdResolver externalIdResolver;

@Mock private HTMLUnicodeFontProcesser htmlUnicodeFontProcesser;

@Mock private HtmlImageResolver htmlImageResolver;

@Mock private PdfHtmlGenerator pdfHtmlGenerator;

@InjectMocks private PdfProcessor pdfProcessor;
Expand Down Expand Up @@ -85,6 +79,12 @@ public void outputNonAsciiCharactersTest(String nonAscii) throws Exception {

@Test
public void concatenatesFiles() throws Exception {
String output = concatTwoDocuments();
assertTrue(output.contains("This is document 1."));
assertTrue(output.contains("This is document 2."));
}

private String concatTwoDocuments() throws Exception {
// create 2 pdf docs then concatenate and verify the output doc contains the 2 inputs
String doc1Html = loadTextResourceFromPdfDir("doc1.html");
ExportProcesserInput exportProcesserInput1 =
Expand All @@ -105,10 +105,25 @@ public void concatenatesFiles() throws Exception {

List<File> filesToConcatenate = List.of(pdfDoc1, pdfDoc2);
pdfProcessor.concatenateExportedFilesIntoOne(outputFile, filesToConcatenate, config);
String output = readPdfContent(outputFile);
return readPdfContent(outputFile);
}

assertTrue(output.contains("This is document 1."));
assertTrue(output.contains("This is document 2."));
@Test
public void testPageNumbersRestartedEachDoc() throws Exception {
concatTwoDocuments();

// config has start page reset to 0 i.e. next doc would begin at page 1, which is the default
assertEquals(0, config.getStartPage());
}

@Test
public void testPageNumbersContiguous() throws Exception {
config.setRestartPageNumberPerDoc(false);
concatTwoDocuments();

// config has start page as page 3 i.e. next doc would begin at page 4, since the config
// property to restart the page number on each doc is set to false
assertEquals(3, config.getStartPage());
}

private String readPdfContent(File outFile) throws IOException {
Expand Down
Loading