Skip to content

Commit 381be88

Browse files
committed
Enable interactivity for form fields with identical names across multiple pages
DEVSIX-7738
1 parent e4d6d57 commit 381be88

File tree

42 files changed

+8082
-14
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+8082
-14
lines changed

src/main/java/com/itextpdf/html2pdf/ConverterProperties.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ This file is part of the iText (R) project.
2222
*/
2323
package com.itextpdf.html2pdf;
2424

25+
import com.itextpdf.commons.actions.contexts.IMetaInfo;
2526
import com.itextpdf.html2pdf.attach.ITagWorkerFactory;
2627
import com.itextpdf.html2pdf.attach.impl.OutlineHandler;
2728
import com.itextpdf.html2pdf.css.apply.ICssApplierFactory;
28-
import com.itextpdf.commons.actions.contexts.IMetaInfo;
2929
import com.itextpdf.layout.font.FontProvider;
3030
import com.itextpdf.styledxmlparser.css.media.MediaDeviceDescription;
3131
import com.itextpdf.styledxmlparser.resolver.resource.IResourceRetriever;
@@ -326,8 +326,11 @@ public boolean isCreateAcroForm() {
326326
* If createAcroForm is set, then when the form is encountered in HTML, AcroForm will be created, otherwise
327327
* a visually identical, but not functional element will be created. Please bare in mind that the created
328328
* Acroform may visually differ a bit from the HTML one.
329+
* <p>
330+
* When enabling this acroform creation, it will disable the immediateFlushing property.
329331
*
330332
* @param createAcroForm true if an AcroForm needs to be created
333+
*
331334
* @return the {@link ConverterProperties} instance
332335
*/
333336
public ConverterProperties setCreateAcroForm(boolean createAcroForm) {

src/main/java/com/itextpdf/html2pdf/attach/impl/tags/HtmlTagWorker.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ This file is part of the iText (R) project.
3131
import com.itextpdf.html2pdf.attach.util.WaitingInlineElementsHelper;
3232
import com.itextpdf.html2pdf.css.CssConstants;
3333
import com.itextpdf.html2pdf.html.AttributeConstants;
34+
import com.itextpdf.html2pdf.logs.Html2PdfLogMessageConstant;
3435
import com.itextpdf.io.font.PdfEncodings;
3536
import com.itextpdf.kernel.pdf.PdfDocument;
3637
import com.itextpdf.kernel.pdf.PdfString;
@@ -45,15 +46,24 @@ This file is part of the iText (R) project.
4546
import com.itextpdf.styledxmlparser.node.IElementNode;
4647
import com.itextpdf.styledxmlparser.node.INode;
4748

49+
import org.slf4j.Logger;
50+
import org.slf4j.LoggerFactory;
51+
4852
/**
4953
* TagWorker class for the {@code html} element.
5054
*/
5155
public class HtmlTagWorker implements ITagWorker {
5256

53-
/** The iText document instance. */
57+
private static final Logger LOGGER = LoggerFactory.getLogger(HtmlTagWorker.class);
58+
59+
/**
60+
* The iText document instance.
61+
*/
5462
private Document document;
5563

56-
/** Helper class for waiting inline elements. */
64+
/**
65+
* Helper class for waiting inline elements.
66+
*/
5767
private WaitingInlineElementsHelper inlineHelper;
5868

5969
/**
@@ -65,14 +75,19 @@ public class HtmlTagWorker implements ITagWorker {
6575
public HtmlTagWorker(IElementNode element, ProcessorContext context) {
6676
// TODO DEVSIX-4261 more precise check if a counter was actually added to the document
6777
boolean immediateFlush =
68-
context.isImmediateFlush() && !context.getCssContext().isPagesCounterPresent();
78+
context.isImmediateFlush() && !context.getCssContext().isPagesCounterPresent()
79+
&& !context.isCreateAcroForm();
80+
if (context.isImmediateFlush() && context.isCreateAcroForm()) {
81+
LOGGER.info(Html2PdfLogMessageConstant.IMMEDIATE_FLUSH_DISABLED);
82+
}
6983
PdfDocument pdfDocument = context.getPdfDocument();
7084
document = new HtmlDocument(pdfDocument, pdfDocument.getDefaultPageSize(), immediateFlush);
7185
document.setRenderer(new HtmlDocumentRenderer(document, immediateFlush));
7286

7387
DefaultHtmlProcessor.setConvertedRootElementProperties(element.getStyles(), context, document);
7488

75-
inlineHelper = new WaitingInlineElementsHelper(element.getStyles().get(CssConstants.WHITE_SPACE), element.getStyles().get(CssConstants.TEXT_TRANSFORM));
89+
inlineHelper = new WaitingInlineElementsHelper(element.getStyles().get(CssConstants.WHITE_SPACE),
90+
element.getStyles().get(CssConstants.TEXT_TRANSFORM));
7691

7792
String lang = element.getAttribute(AttributeConstants.LANG);
7893
if (lang != null) {

src/main/java/com/itextpdf/html2pdf/logs/Html2PdfLogMessageConstant.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public final class Html2PdfLogMessageConstant {
172172
public static final String ELEMENT_DOES_NOT_FIT_CURRENT_AREA = "Element does not fit current area";
173173
public static final String OPTGROUP_NOT_SUPPORTED_IN_INTERACTIVE_SELECT = "Option groups are not supported in "
174174
+ "interactive mode";
175+
public static final String IMMEDIATE_FLUSH_DISABLED = "Setting createAcroForm disables immediateFlush property";
175176

176177
private Html2PdfLogMessageConstant() {
177178
//Private constructor will prevent the instantiation of this class directly

src/test/java/com/itextpdf/html2pdf/element/FormTest.java

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,23 @@ This file is part of the iText (R) project.
2727
import com.itextpdf.forms.logs.FormsLogMessageConstants;
2828
import com.itextpdf.html2pdf.ConverterProperties;
2929
import com.itextpdf.html2pdf.HtmlConverter;
30+
import com.itextpdf.html2pdf.logs.Html2PdfLogMessageConstant;
3031
import com.itextpdf.io.logs.IoLogMessageConstant;
32+
import com.itextpdf.io.source.ByteArrayOutputStream;
3133
import com.itextpdf.io.util.UrlUtil;
3234
import com.itextpdf.kernel.pdf.PdfDocument;
3335
import com.itextpdf.kernel.pdf.PdfReader;
3436
import com.itextpdf.kernel.pdf.PdfWriter;
3537
import com.itextpdf.kernel.utils.CompareTool;
3638
import com.itextpdf.layout.logs.LayoutLogMessageConstant;
3739
import com.itextpdf.test.ExtendedITextTest;
40+
import com.itextpdf.test.LogLevelConstants;
3841
import com.itextpdf.test.annotations.LogMessage;
3942
import com.itextpdf.test.annotations.LogMessages;
4043
import com.itextpdf.test.annotations.type.IntegrationTest;
4144

4245
import java.io.File;
46+
import java.io.FileInputStream;
4347
import java.io.IOException;
4448
import org.junit.Assert;
4549
import org.junit.BeforeClass;
@@ -67,6 +71,41 @@ public void textFieldWithPlaceholderTest() throws IOException, InterruptedExcept
6771
runTest("textFieldWithPlaceholder");
6872
}
6973

74+
@Test
75+
public void textFieldHeadersFootersTest() throws IOException, InterruptedException {
76+
runTest("textFieldHeadersFooters");
77+
}
78+
79+
@Test
80+
public void textFieldHeadersFootersWithValueTest() throws IOException, InterruptedException {
81+
runTest("textFieldHeadersFootersWithValue");
82+
}
83+
84+
@Test
85+
public void textAreaHeadersFootersWithValueTest() throws IOException, InterruptedException {
86+
runTest("textAreaHeadersFooters");
87+
}
88+
89+
90+
@Test
91+
public void checkBoxHeadersFootersWithValueTest() throws IOException, InterruptedException {
92+
//TODO DEVSIX-7760 Adding formfield elements to header generates strange results
93+
runTest("checkBoxHeadersFooters");
94+
}
95+
96+
@Test
97+
public void radioHeadersFootersWithValueTest() throws IOException, InterruptedException {
98+
//TODO DEVSIX-7760 Adding formfield elements to header generates strange results
99+
runTest("radioHeadersFooters");
100+
}
101+
102+
103+
@Test
104+
public void dropDownHeadersFootersWithValueTest() throws IOException, InterruptedException {
105+
runTest("dropDownHeadersFooters");
106+
}
107+
108+
70109
@Test
71110
public void splitTextFieldTest() throws IOException, InterruptedException {
72111
runTest("splitTextField");
@@ -185,6 +224,7 @@ public void radiobox1Test() throws IOException, InterruptedException {
185224
public void radiobox2Test() throws IOException, InterruptedException {
186225
runTest("radiobox2");
187226
}
227+
188228
@Test
189229
@LogMessages(messages = {
190230
@LogMessage(messageTemplate = IoLogMessageConstant.MULTIPLE_VALUES_ON_A_NON_MULTISELECT_FIELD)})
@@ -238,6 +278,57 @@ public void radioButtonNoPageCounterTest() throws IOException, InterruptedExcept
238278
pdf, sourceFolder + "cmp_radioButtonNoPageCounter.pdf", destinationFolder));
239279
}
240280

281+
@Test
282+
@LogMessages(messages = {
283+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.IMMEDIATE_FLUSH_DISABLED, logLevel =
284+
LogLevelConstants.INFO)})
285+
public void checkLogInfo() throws IOException {
286+
String html = sourceFolder + "radiobox1.html";
287+
try (FileInputStream fileInputStream = new FileInputStream(html);
288+
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
289+
HtmlConverter.convertToPdf(fileInputStream, baos,
290+
new ConverterProperties().setCreateAcroForm(true));
291+
}
292+
}
293+
294+
@Test
295+
@LogMessages(messages = {
296+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.IMMEDIATE_FLUSH_DISABLED, logLevel =
297+
LogLevelConstants.INFO, count = 0)})
298+
public void checkLogInfoNoAcroForm() throws IOException {
299+
String html = sourceFolder + "radiobox1.html";
300+
try (FileInputStream fileInputStream = new FileInputStream(html);
301+
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
302+
HtmlConverter.convertToPdf(fileInputStream, baos,
303+
new ConverterProperties().setCreateAcroForm(false));
304+
}
305+
}
306+
307+
@Test
308+
@LogMessages(messages = {
309+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.IMMEDIATE_FLUSH_DISABLED, logLevel =
310+
LogLevelConstants.INFO, count = 0)})
311+
public void checkLogInfoAcroFormFlushDisabled() throws IOException {
312+
String html = sourceFolder + "radiobox1.html";
313+
try (FileInputStream fileInputStream = new FileInputStream(html);
314+
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
315+
HtmlConverter.convertToPdf(fileInputStream, baos,
316+
new ConverterProperties().setCreateAcroForm(true).setImmediateFlush(false));
317+
}
318+
}
319+
320+
@Test
321+
@LogMessages(messages = {
322+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.IMMEDIATE_FLUSH_DISABLED, logLevel =
323+
LogLevelConstants.INFO, count = 0)})
324+
public void checkLogInfoDefault() throws IOException {
325+
String html = sourceFolder + "radiobox1.html";
326+
try (FileInputStream fileInputStream = new FileInputStream(html);
327+
ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
328+
HtmlConverter.convertToPdf(fileInputStream, baos);
329+
}
330+
}
331+
241332
private void runTest(String name) throws IOException, InterruptedException {
242333
runTest(name, true);
243334
}
@@ -253,7 +344,8 @@ private void runTest(String name, boolean flattenPdfAcroFormFields) throws IOExc
253344
String diff = "diff_" + name + "_";
254345

255346
HtmlConverter.convertToPdf(new File(htmlPath), new File(outPdfPath));
256-
HtmlConverter.convertToPdf(new File(htmlPath), new File(outAcroPdfPath), new ConverterProperties().setCreateAcroForm(true));
347+
HtmlConverter.convertToPdf(new File(htmlPath), new File(outAcroPdfPath),
348+
new ConverterProperties().setCreateAcroForm(true));
257349
if (flattenPdfAcroFormFields) {
258350
PdfDocument document = new PdfDocument(new PdfReader(outAcroPdfPath), new PdfWriter(outAcroFlattenPdfPath));
259351
PdfAcroForm acroForm = PdfFormCreator.getAcroForm(document, false);
@@ -264,7 +356,9 @@ private void runTest(String name, boolean flattenPdfAcroFormFields) throws IOExc
264356
Assert.assertNull(new CompareTool().compareByContent(outPdfPath, cmpPdfPath, destinationFolder, diff));
265357
Assert.assertNull(new CompareTool().compareByContent(outAcroPdfPath, cmpAcroPdfPath, destinationFolder, diff));
266358
if (flattenPdfAcroFormFields) {
267-
Assert.assertNull(new CompareTool().compareByContent(outAcroFlattenPdfPath, cmpAcroFlattenPdfPath, destinationFolder, diff));
359+
Assert.assertNull(
360+
new CompareTool().compareByContent(outAcroFlattenPdfPath, cmpAcroFlattenPdfPath, destinationFolder,
361+
diff));
268362
}
269363
}
270364
}

src/test/java/com/itextpdf/html2pdf/element/TaggedPdfFormTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,7 @@ public void fieldSetFormTagged()
126126
}
127127

128128
@Test
129-
// TODO DEVSIX-4601
130-
// exception is thrown on "convert tagged PDF with acroform" stage
129+
@Ignore("DEVSIX-4601 exception is thrown on \"convert tagged PDF with acroform\" stage")
131130
public void inputFormPrematureFlush()
132131
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
133132
junitExpectedException.expect(PdfException.class);

src/test/java/com/itextpdf/html2pdf/element/TagsInsideButtonTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@ This file is part of the iText (R) project.
2323
package com.itextpdf.html2pdf.element;
2424

2525
import com.itextpdf.html2pdf.ExtendedHtmlConversionITextTest;
26-
import com.itextpdf.kernel.exceptions.PdfException;
2726
import com.itextpdf.test.annotations.type.IntegrationTest;
27+
28+
import java.io.IOException;
29+
import javax.xml.parsers.ParserConfigurationException;
2830
import org.junit.BeforeClass;
2931
import org.junit.Rule;
3032
import org.junit.Test;
3133
import org.junit.experimental.categories.Category;
3234
import org.junit.rules.ExpectedException;
3335
import org.xml.sax.SAXException;
3436

35-
import javax.xml.parsers.ParserConfigurationException;
36-
import java.io.IOException;
37-
3837
@Category(IntegrationTest.class)
3938
public class TagsInsideButtonTest extends ExtendedHtmlConversionITextTest {
4039

@@ -87,7 +86,6 @@ public void buttonWithPInsideTagged()
8786
@Test
8887
public void buttonInsideMoreThanTwoAreas()
8988
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
90-
junitExpectedException.expect(PdfException.class);
9189
convertToPdfAcroformFlattenAndCompare("buttonInsideMoreThanTwoAreas", sourceFolder,
9290
destinationFolder, true);
9391
}

0 commit comments

Comments
 (0)