Skip to content

Commit d6a4e89

Browse files
committed
[RELEASE] iText pdfHtml 5.0.4
2 parents 2d2556e + 2050ed3 commit d6a4e89

File tree

176 files changed

+3852
-82
lines changed

Some content is hidden

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

176 files changed

+3852
-82
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<parent>
66
<groupId>com.itextpdf</groupId>
77
<artifactId>root</artifactId>
8-
<version>8.0.3</version>
8+
<version>8.0.4</version>
99
<relativePath />
1010
</parent>
1111

1212
<artifactId>html2pdf</artifactId>
13-
<version>5.0.3</version>
13+
<version>5.0.4</version>
1414

1515
<name>pdfHTML</name>
1616
<description>pdfHTML is an iText add-on that lets you to parse (X)HTML snippets and the associated CSS and converts

src/main/java/com/itextpdf/html2pdf/actions/data/PdfHtmlProductData.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ This file is part of the iText (R) project.
3030
*/
3131
public final class PdfHtmlProductData {
3232
private static final String PDF_HTML_PUBLIC_PRODUCT_NAME = "pdfHTML";
33-
private static final String PDF_HTML_VERSION = "5.0.3";
33+
private static final String PDF_HTML_VERSION = "5.0.4";
3434
private static final int PDF_HTML_COPYRIGHT_SINCE = 2000;
3535
private static final int PDF_HTML_COPYRIGHT_TO = 2024;
3636

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2024 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.html2pdf.attach;
24+
25+
import com.itextpdf.styledxmlparser.node.IElementNode;
26+
27+
/**
28+
* {@link IOutlineMarkExtractor} interface is used to control what part of element will be a mark
29+
* witch will be used to create outline in {@link com.itextpdf.html2pdf.attach.impl.OutlineHandler}
30+
*/
31+
public interface IOutlineMarkExtractor {
32+
/**
33+
* Get element mark.
34+
*
35+
* @param element the element
36+
* @return returns string mark of the element
37+
*/
38+
String getMark(IElementNode element);
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2024 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.html2pdf.attach.impl;
24+
25+
import com.itextpdf.html2pdf.attach.IOutlineMarkExtractor;
26+
import com.itextpdf.styledxmlparser.node.IElementNode;
27+
28+
/**
29+
* {@link ClassOutlineMarkExtractor} class is used to get class of element as a mark for {@link OutlineHandler}
30+
*/
31+
public class ClassOutlineMarkExtractor implements IOutlineMarkExtractor {
32+
@Override
33+
public String getMark(IElementNode element) {
34+
return element.getAttribute("class");
35+
}
36+
}

src/main/java/com/itextpdf/html2pdf/attach/impl/OutlineHandler.java

+130-31
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ This file is part of the iText (R) project.
2323
package com.itextpdf.html2pdf.attach.impl;
2424

2525
import com.itextpdf.commons.datastructures.Tuple2;
26+
import com.itextpdf.html2pdf.attach.IOutlineMarkExtractor;
2627
import com.itextpdf.html2pdf.html.TagConstants;
2728
import com.itextpdf.html2pdf.logs.Html2PdfLogMessageConstant;
2829
import com.itextpdf.html2pdf.attach.ITagWorker;
@@ -45,7 +46,8 @@ This file is part of the iText (R) project.
4546
import java.util.Map;
4647

4748
/**
48-
* A {@link OutlineHandler} handles creating outlines for tags.
49+
* A {@link OutlineHandler} handles creating outlines for marks.
50+
* Marks are extracted via interface {@link IOutlineMarkExtractor}.
4951
* <p>
5052
* This class is not reusable and a new instance shall be created for every new conversion process.
5153
*/
@@ -68,86 +70,182 @@ public class OutlineHandler {
6870
/**
6971
* The current outline.
7072
*/
71-
private PdfOutline currentOutline;
73+
protected PdfOutline currentOutline;
7274

7375
/**
7476
* The destinations in process.
7577
*/
76-
private Deque<Tuple2<String, PdfDictionary>> destinationsInProcess = new LinkedList<Tuple2<String, PdfDictionary>>();
78+
protected Deque<Tuple2<String, PdfDictionary>> destinationsInProcess = new LinkedList<Tuple2<String, PdfDictionary>>();
7779

7880
/**
7981
* The levels in process.
8082
*/
81-
private Deque<Integer> levelsInProcess = new LinkedList<Integer>();
83+
protected Deque<Integer> levelsInProcess = new LinkedList<Integer>();
8284

8385
/**
84-
* The tag priorities mapping.
86+
* The mark priorities mapping.
8587
*/
86-
private Map<String, Integer> tagPrioritiesMapping = new HashMap<String, Integer>();
88+
private Map<String, Integer> markPrioritiesMapping = new HashMap<String, Integer>();
8789

8890
/**
8991
* The destination prefix.
9092
*/
9193
private String destinationNamePrefix = DEFAULT_DESTINATION_NAME_PREFIX;
9294

95+
/**
96+
*The mark extractor defines what part of element will be used to create outline
97+
*/
98+
protected IOutlineMarkExtractor markExtractor;
9399

94100
/**
95-
* Creates an OutlineHandler with standard predefined mappings.
101+
* Creates an OutlineHandler with standard {@link TagOutlineMarkExtractor}.
102+
*/
103+
public OutlineHandler(){
104+
markExtractor = new TagOutlineMarkExtractor();
105+
}
106+
/**
107+
* Creates an OutlineHandler with standard {@link TagOutlineMarkExtractor} and predefined mappings.
96108
*
97109
* @return the outline handler
98110
*/
99111
public static OutlineHandler createStandardHandler() {
100112
OutlineHandler handler = new OutlineHandler();
101-
handler.putTagPriorityMapping(TagConstants.H1, 1);
102-
handler.putTagPriorityMapping(TagConstants.H2, 2);
103-
handler.putTagPriorityMapping(TagConstants.H3, 3);
104-
handler.putTagPriorityMapping(TagConstants.H4, 4);
105-
handler.putTagPriorityMapping(TagConstants.H5, 5);
106-
handler.putTagPriorityMapping(TagConstants.H6, 6);
113+
handler.putMarkPriorityMapping(TagConstants.H1, 1);
114+
handler.putMarkPriorityMapping(TagConstants.H2, 2);
115+
handler.putMarkPriorityMapping(TagConstants.H3, 3);
116+
handler.putMarkPriorityMapping(TagConstants.H4, 4);
117+
handler.putMarkPriorityMapping(TagConstants.H5, 5);
118+
handler.putMarkPriorityMapping(TagConstants.H6, 6);
119+
return handler;
120+
}
121+
122+
/**
123+
* Creates an OutlineHandler with custom {@link IOutlineMarkExtractor}
124+
*
125+
* @param extractor the mark extractor
126+
* @return the outline handler
127+
*/
128+
public static OutlineHandler createHandler(IOutlineMarkExtractor extractor) {
129+
OutlineHandler handler = new OutlineHandler();
130+
handler.markExtractor = extractor;
107131
return handler;
108132
}
109133

110134
/**
111-
* Put tag priority mapping.
135+
* Get mark extractor.
136+
*
137+
* @return the mark extractor
138+
*/
139+
public IOutlineMarkExtractor getMarkExtractor(){
140+
return markExtractor;
141+
}
142+
143+
/**
144+
* Set mark extractor.
145+
*
146+
* @param extractor the mark extractor
147+
* @return the outline handler
148+
*/
149+
public OutlineHandler setMarkExtractor(IOutlineMarkExtractor extractor){
150+
markExtractor = extractor;
151+
return this;
152+
}
153+
/**
154+
* Put tag into priority mapping.
112155
*
113156
* @param tagName the tag name
114157
* @param priority the priority
115158
* @return the outline handler
159+
* @deprecated use {@link #putMarkPriorityMapping(String, Integer)} instead
116160
*/
161+
@Deprecated
117162
public OutlineHandler putTagPriorityMapping(String tagName, Integer priority) {
118-
tagPrioritiesMapping.put(tagName, priority);
163+
putMarkPriorityMapping(tagName, priority);
119164
return this;
120165
}
121166

122167
/**
123-
* Put all tag priority mappings.
168+
* Put mark into priority mapping.
169+
*
170+
* @param markName the mark name
171+
* @param priority the priority
172+
* @return the outline handler
173+
*/
174+
public OutlineHandler putMarkPriorityMapping(String markName, Integer priority) {
175+
markPrioritiesMapping.put(markName, priority);
176+
return this;
177+
}
178+
179+
/**
180+
* Put all tags into priority mappings.
124181
*
125182
* @param mappings the mappings
126183
* @return the outline handler
184+
* @deprecated ue {@link #putAllMarksPriorityMappings(Map)} instead
127185
*/
186+
@Deprecated
128187
public OutlineHandler putAllTagPriorityMappings(Map<String, Integer> mappings) {
129-
tagPrioritiesMapping.putAll(mappings);
188+
putAllMarksPriorityMappings(mappings);
189+
return this;
190+
}
191+
192+
/**
193+
* Put all marks into priority mappings.
194+
*
195+
* @param mappings the mappings
196+
* @return the outline handler
197+
*/
198+
public OutlineHandler putAllMarksPriorityMappings(Map<String, Integer> mappings) {
199+
markPrioritiesMapping.putAll(mappings);
130200
return this;
131201
}
132202

133203
/**
134-
* Gets the tag priority mapping.
204+
* Gets the marks from priority mapping.
135205
*
136206
* @param tagName the tag name
137207
* @return the tag priority mapping
208+
* @deprecated use {@link #getMarkPriorityMapping(String)} instead
138209
*/
210+
@Deprecated
139211
public Integer getTagPriorityMapping(String tagName) {
140-
return tagPrioritiesMapping.get(tagName);
212+
return getMarkPriorityMapping(tagName);
141213
}
142214

143215
/**
144-
* Checks for tag priority mapping.
216+
* Gets the mark from priority mapping.
217+
*
218+
* @param markName the mark name
219+
* @return the tag priority mapping
220+
*/
221+
public Integer getMarkPriorityMapping(String markName) {
222+
return markPrioritiesMapping.get(markName);
223+
}
224+
225+
/**
226+
* Checks for tag in priority mapping.
145227
*
146228
* @param tagName the tag name
147229
* @return true, if the tag name is listed in the tag priorities mapping
230+
* @deprecated use {@link #hasMarkPriorityMapping(String)} instead
148231
*/
232+
@Deprecated
149233
public boolean hasTagPriorityMapping(String tagName) {
150-
return tagPrioritiesMapping.containsKey(tagName);
234+
return hasMarkPriorityMapping(tagName);
235+
}
236+
237+
/**
238+
* Checks for tag in priority mapping.
239+
*
240+
* @param markName the mark name
241+
* @return true, if the tag name is listed in the tag priorities mapping
242+
*/
243+
public boolean hasMarkPriorityMapping(String markName) {
244+
if (markName == null){
245+
return false;
246+
} else {
247+
return markPrioritiesMapping.containsKey(markName);
248+
}
151249
}
152250

153251
/**
@@ -209,10 +307,10 @@ protected String generateUniqueDestinationName(IElementNode element) {
209307
* @return the unique destination name
210308
*/
211309
protected String generateOutlineName(IElementNode element) {
212-
String tagName = element.name();
310+
String markName = markExtractor.getMark(element);
213311
String content = ((JsoupElementNode) element).text();
214312
if (content.isEmpty()) {
215-
content = getUniqueID(tagName);
313+
content = getUniqueID(markName);
216314
}
217315
return content;
218316
}
@@ -228,10 +326,11 @@ protected String generateOutlineName(IElementNode element) {
228326
* @param context the processor context
229327
* @return the outline handler
230328
*/
231-
OutlineHandler addOutlineAndDestToDocument(ITagWorker tagWorker, IElementNode element, ProcessorContext context) {
232-
String tagName = element.name();
233-
if (null != tagWorker && hasTagPriorityMapping(tagName) && context.getPdfDocument() != null) {
234-
int level = (int) getTagPriorityMapping(tagName);
329+
protected OutlineHandler addOutlineAndDestToDocument(ITagWorker tagWorker, IElementNode element,
330+
ProcessorContext context) {
331+
String markName = markExtractor.getMark(element);
332+
if (null != tagWorker && hasMarkPriorityMapping(markName) && context.getPdfDocument() != null) {
333+
int level = (int) getMarkPriorityMapping(markName);
235334
if (null == currentOutline) {
236335
currentOutline = context.getPdfDocument().getOutlines(false);
237336
}
@@ -262,16 +361,16 @@ OutlineHandler addOutlineAndDestToDocument(ITagWorker tagWorker, IElementNode el
262361
* @param element the element
263362
* @return the outline handler
264363
*/
265-
OutlineHandler setDestinationToElement(ITagWorker tagWorker, IElementNode element) {
266-
String tagName = element.name();
267-
if (null != tagWorker && hasTagPriorityMapping(tagName) && destinationsInProcess.size() > 0) {
364+
protected OutlineHandler setDestinationToElement(ITagWorker tagWorker, IElementNode element) {
365+
String markName = markExtractor.getMark(element);
366+
if (null != tagWorker && hasMarkPriorityMapping(markName) && destinationsInProcess.size() > 0) {
268367
Tuple2<String, PdfDictionary> content = destinationsInProcess.pop();
269368
if (tagWorker.getElementResult() instanceof IElement) {
270369
tagWorker.getElementResult().setProperty(Property.DESTINATION, content);
271370
} else {
272371
Logger logger = LoggerFactory.getLogger(OutlineHandler.class);
273372
logger.warn(MessageFormatUtil.format(
274-
Html2PdfLogMessageConstant.NO_IPROPERTYCONTAINER_RESULT_FOR_THE_TAG, tagName));
373+
Html2PdfLogMessageConstant.NO_IPROPERTYCONTAINER_RESULT_FOR_THE_TAG, markName));
275374
}
276375
}
277376
return this;

0 commit comments

Comments
 (0)