Skip to content

Commit 194c365

Browse files
author
Alexandr Pliushchou
committed
Add alternative description conversion for input tags
DEVSIX-8678
1 parent f871ecf commit 194c365

File tree

8 files changed

+112
-14
lines changed

8 files changed

+112
-14
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ public InputTagWorker(IElementNode element, ProcessorContext context) {
163163
formElement.setProperty(FormProperty.FORM_FIELD_FLATTEN, !context.isCreateAcroForm());
164164
((IAccessibleElement)formElement).getAccessibilityProperties().setLanguage(lang);
165165
formElement.setProperty(FormProperty.FORM_CONFORMANCE_LEVEL, context.getConformance());
166+
167+
String altText = element.getAttribute(AttributeConstants.TITLE);
168+
if (altText != null) {
169+
((IAccessibleElement) formElement).getAccessibilityProperties().setAlternateDescription(altText);
170+
}
166171
}
167172

168173
display = element.getStyles() != null ? element.getStyles().get(CssConstants.DISPLAY) : null;
@@ -215,5 +220,4 @@ static String preprocessInputValue(String value, String inputType) {
215220
}
216221
return value;
217222
}
218-
219223
}

src/test/java/com/itextpdf/html2pdf/HtmlConverterPdfUA1UA2Test.java

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void simpleLinkTest() throws IOException, InterruptedException, XMPExcept
8383
PdfUAExceptionMessageConstants.ANNOTATION_OF_TYPE_0_SHOULD_HAVE_CONTENTS_OR_ALT_KEY,
8484
PdfName.Link.getValue());
8585

86-
convertToUa1AndCheckCompliance(sourceHtml,destinationPdfUa1, cmpPdfUa1, false, expectedUa1Message);
86+
convertToUa1AndCheckCompliance(sourceHtml, destinationPdfUa1, cmpPdfUa1, false, expectedUa1Message);
8787
// Expected valid UA-2 document because PDF/UA-2 does not require Contents in Link annotations
8888
convertToUa2AndCheckCompliance(sourceHtml, destinationPdfUa2, cmpPdfUa2, true);
8989
}
@@ -254,6 +254,23 @@ public void emptyHtmlTest() throws IOException, InterruptedException, XMPExcepti
254254
convertToUa2AndCheckCompliance(sourceHtml, destinationPdfUa2, cmpPdfUa2, false);
255255
}
256256

257+
@Test
258+
public void inputWithTitleTagTest() throws IOException, InterruptedException, XMPException {
259+
String sourceHtml = SOURCE_FOLDER + "inputWithTitleTag.html";
260+
String cmpPdfUa1 = SOURCE_FOLDER + "cmp_inputWithTitleTagUa1.pdf";
261+
String cmpPdfUa2 = SOURCE_FOLDER + "cmp_inputWithTitleTagUa2.pdf";
262+
String destinationPdfUa1 = DESTINATION_FOLDER + "inputWithTitleTagUa1.pdf";
263+
String destinationPdfUa2 = DESTINATION_FOLDER + "inputWithTitleTagUa2.pdf";
264+
265+
ConverterProperties converterProperties = new ConverterProperties();
266+
converterProperties.setCreateAcroForm(true);
267+
268+
convertToUa1AndCheckCompliance(sourceHtml,destinationPdfUa1, cmpPdfUa1, converterProperties, true,
269+
null);
270+
// TODO DEVSIX-8868 Change this test when fixed
271+
convertToUa2AndCheckCompliance(sourceHtml, destinationPdfUa2, cmpPdfUa2, converterProperties, false);
272+
}
273+
257274
private void createSimplePdfUA2Document(PdfDocument pdfDocument) throws IOException, XMPException {
258275
byte[] bytes = Files.readAllBytes(Paths.get(SOURCE_FOLDER + "simplePdfUA2.xmp"));
259276
XMPMeta xmpMeta = XMPMetaFactory.parse(new ByteArrayInputStream(bytes));
@@ -278,38 +295,63 @@ private static void compareAndCheckCompliance(String destinationPdf, String cmpP
278295

279296
private void convertToUa1AndCheckCompliance(String sourceHtml, String destinationPdf, String cmpPdf,
280297
boolean isExpectedOk, String expectedErrorMessage) throws IOException, InterruptedException {
298+
convertToUa1AndCheckCompliance(sourceHtml, destinationPdf, cmpPdf, new ConverterProperties(), isExpectedOk,
299+
expectedErrorMessage);
300+
}
301+
302+
private void convertToUa2AndCheckCompliance(String sourceHtml, String destinationPdf, String cmpPdf,
303+
boolean isExpectedOk) throws IOException, XMPException, InterruptedException {
304+
convertToUa2AndCheckCompliance(sourceHtml, destinationPdf, cmpPdf, new ConverterProperties(), isExpectedOk);
305+
}
306+
307+
private void convertToUa1AndCheckCompliance(String sourceHtml, String destinationPdf, String cmpPdf,
308+
ConverterProperties converterProperties, boolean isExpectedOk,
309+
String expectedErrorMessage) throws IOException, InterruptedException {
281310
PdfDocument pdfDocument = new PdfUADocument(new PdfWriter(destinationPdf),
282311
new PdfUAConfig(PdfUAConformance.PDF_UA_1, "simple doc", "eng"));
283312

284-
ConverterProperties converterProperties = new ConverterProperties();
313+
ConverterProperties converterPropertiesCopy;
314+
if (converterProperties == null) {
315+
converterPropertiesCopy = new ConverterProperties();
316+
} else {
317+
converterPropertiesCopy = new ConverterProperties(converterProperties);
318+
}
319+
285320
FontProvider fontProvider = new BasicFontProvider(false, true, false);
286-
converterProperties.setFontProvider(fontProvider);
287-
converterProperties.setBaseUri(SOURCE_FOLDER);
288-
converterProperties.setOutlineHandler(OutlineHandler.createStandardHandler());
321+
converterPropertiesCopy.setFontProvider(fontProvider);
322+
converterPropertiesCopy.setBaseUri(SOURCE_FOLDER);
323+
converterPropertiesCopy.setOutlineHandler(OutlineHandler.createStandardHandler());
289324

290325
if (expectedErrorMessage != null) {
291326
Exception e = Assertions.assertThrows(PdfUAConformanceException.class,
292327
() -> HtmlConverter.convertToPdf(new FileInputStream(sourceHtml), pdfDocument,
293-
converterProperties));
328+
converterPropertiesCopy));
294329
Assertions.assertEquals(expectedErrorMessage, e.getMessage());
295330
} else {
296-
HtmlConverter.convertToPdf(new FileInputStream(sourceHtml), pdfDocument, converterProperties);
331+
HtmlConverter.convertToPdf(new FileInputStream(sourceHtml), pdfDocument, converterPropertiesCopy);
297332
compareAndCheckCompliance(destinationPdf, cmpPdf, isExpectedOk);
298333
}
299334
}
300335

301336
private void convertToUa2AndCheckCompliance(String sourceHtml, String destinationPdf, String cmpPdf,
302-
boolean isExpectedOk) throws IOException, XMPException, InterruptedException {
337+
ConverterProperties converterProperties, boolean isExpectedOk)
338+
throws IOException, XMPException, InterruptedException {
303339
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationPdf, new WriterProperties().setPdfVersion(
304340
PdfVersion.PDF_2_0)));
305341
createSimplePdfUA2Document(pdfDocument);
306342

307-
ConverterProperties converterProperties = new ConverterProperties();
343+
ConverterProperties converterPropertiesCopy;
344+
if (converterProperties == null) {
345+
converterPropertiesCopy = new ConverterProperties();
346+
} else {
347+
converterPropertiesCopy = new ConverterProperties(converterProperties);
348+
}
349+
308350
FontProvider fontProvider = new BasicFontProvider(false, true, false);
309-
converterProperties.setFontProvider(fontProvider);
310-
converterProperties.setBaseUri(SOURCE_FOLDER);
311-
converterProperties.setOutlineHandler(OutlineHandler.createStandardHandler());
312-
HtmlConverter.convertToPdf(new FileInputStream(sourceHtml), pdfDocument, converterProperties);
351+
converterPropertiesCopy.setFontProvider(fontProvider);
352+
converterPropertiesCopy.setBaseUri(SOURCE_FOLDER);
353+
converterPropertiesCopy.setOutlineHandler(OutlineHandler.createStandardHandler());
354+
HtmlConverter.convertToPdf(new FileInputStream(sourceHtml), pdfDocument, converterPropertiesCopy);
313355

314356
compareAndCheckCompliance(destinationPdf, cmpPdf, isExpectedOk);
315357
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ This file is part of the iText (R) project.
3636
import com.itextpdf.kernel.colors.ColorConstants;
3737
import com.itextpdf.kernel.geom.PageSize;
3838
import com.itextpdf.kernel.pdf.PdfAConformance;
39+
import com.itextpdf.kernel.pdf.PdfUAConformance;
3940
import com.itextpdf.kernel.pdf.PdfDocument;
4041
import com.itextpdf.kernel.pdf.PdfOutputIntent;
4142
import com.itextpdf.kernel.pdf.PdfWriter;
@@ -301,6 +302,14 @@ public void checkboxDiffWidthDisplayBlockTest() throws IOException, InterruptedE
301302
runTest("checkboxDiffWidthDisplayBlock");
302303
}
303304

305+
@Test
306+
@LogMessages(ignore = true, messages = {
307+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.INPUT_TYPE_IS_INVALID)
308+
})
309+
public void inputImageTest() throws IOException, InterruptedException {
310+
runTest("inputImage");
311+
}
312+
304313
@Test
305314
@LogMessages(ignore = true, messages = {
306315
@LogMessage(messageTemplate = LayoutLogMessageConstant.ELEMENT_DOES_NOT_FIT_AREA)
Binary file not shown.
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="de">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Document title</title>
6+
</head>
7+
<body>
8+
<main style="display:block; float:left;">
9+
10+
<form>
11+
<div class="someDivCLass">
12+
<div>
13+
<input type="text" name="name" title="this should be used as TU entry">
14+
</div>
15+
</div>
16+
<div class="someDivCLass2">
17+
<div>
18+
<input type="button" value="little button" name="name" title="this should be used as TU entry too">
19+
</div>
20+
</div>
21+
</form>
22+
</main>
23+
</body>
24+
</html>
Binary file not shown.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="de">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Document title</title>
6+
</head>
7+
<body>
8+
<main style="display:block; float:left;">
9+
10+
<form>
11+
<div class="someDivCLass">
12+
<div>
13+
<input type="img" value="some value" name="name">
14+
</div>
15+
</div>
16+
</form>
17+
</main>
18+
</body>
19+
</html>

0 commit comments

Comments
 (0)