Skip to content

Commit

Permalink
[RELEASE] iText pdfHtml 5.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
iText-CI committed Apr 18, 2024
2 parents 2d2556e + 2050ed3 commit d6a4e89
Show file tree
Hide file tree
Showing 176 changed files with 3,852 additions and 82 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>com.itextpdf</groupId>
<artifactId>root</artifactId>
<version>8.0.3</version>
<version>8.0.4</version>
<relativePath />
</parent>

<artifactId>html2pdf</artifactId>
<version>5.0.3</version>
<version>5.0.4</version>

<name>pdfHTML</name>
<description>pdfHTML is an iText add-on that lets you to parse (X)HTML snippets and the associated CSS and converts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This file is part of the iText (R) project.
*/
public final class PdfHtmlProductData {
private static final String PDF_HTML_PUBLIC_PRODUCT_NAME = "pdfHTML";
private static final String PDF_HTML_VERSION = "5.0.3";
private static final String PDF_HTML_VERSION = "5.0.4";
private static final int PDF_HTML_COPYRIGHT_SINCE = 2000;
private static final int PDF_HTML_COPYRIGHT_TO = 2024;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2024 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itextpdf.html2pdf.attach;

import com.itextpdf.styledxmlparser.node.IElementNode;

/**
* {@link IOutlineMarkExtractor} interface is used to control what part of element will be a mark
* witch will be used to create outline in {@link com.itextpdf.html2pdf.attach.impl.OutlineHandler}
*/
public interface IOutlineMarkExtractor {
/**
* Get element mark.
*
* @param element the element
* @return returns string mark of the element
*/
String getMark(IElementNode element);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2024 Apryse Group NV
Authors: Apryse Software.
This program is offered under a commercial and under the AGPL license.
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
AGPL licensing:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itextpdf.html2pdf.attach.impl;

import com.itextpdf.html2pdf.attach.IOutlineMarkExtractor;
import com.itextpdf.styledxmlparser.node.IElementNode;

/**
* {@link ClassOutlineMarkExtractor} class is used to get class of element as a mark for {@link OutlineHandler}
*/
public class ClassOutlineMarkExtractor implements IOutlineMarkExtractor {
@Override
public String getMark(IElementNode element) {
return element.getAttribute("class");
}
}
161 changes: 130 additions & 31 deletions src/main/java/com/itextpdf/html2pdf/attach/impl/OutlineHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This file is part of the iText (R) project.
package com.itextpdf.html2pdf.attach.impl;

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

/**
* A {@link OutlineHandler} handles creating outlines for tags.
* A {@link OutlineHandler} handles creating outlines for marks.
* Marks are extracted via interface {@link IOutlineMarkExtractor}.
* <p>
* This class is not reusable and a new instance shall be created for every new conversion process.
*/
Expand All @@ -68,86 +70,182 @@ public class OutlineHandler {
/**
* The current outline.
*/
private PdfOutline currentOutline;
protected PdfOutline currentOutline;

/**
* The destinations in process.
*/
private Deque<Tuple2<String, PdfDictionary>> destinationsInProcess = new LinkedList<Tuple2<String, PdfDictionary>>();
protected Deque<Tuple2<String, PdfDictionary>> destinationsInProcess = new LinkedList<Tuple2<String, PdfDictionary>>();

/**
* The levels in process.
*/
private Deque<Integer> levelsInProcess = new LinkedList<Integer>();
protected Deque<Integer> levelsInProcess = new LinkedList<Integer>();

/**
* The tag priorities mapping.
* The mark priorities mapping.
*/
private Map<String, Integer> tagPrioritiesMapping = new HashMap<String, Integer>();
private Map<String, Integer> markPrioritiesMapping = new HashMap<String, Integer>();

/**
* The destination prefix.
*/
private String destinationNamePrefix = DEFAULT_DESTINATION_NAME_PREFIX;

/**
*The mark extractor defines what part of element will be used to create outline
*/
protected IOutlineMarkExtractor markExtractor;

/**
* Creates an OutlineHandler with standard predefined mappings.
* Creates an OutlineHandler with standard {@link TagOutlineMarkExtractor}.
*/
public OutlineHandler(){
markExtractor = new TagOutlineMarkExtractor();
}
/**
* Creates an OutlineHandler with standard {@link TagOutlineMarkExtractor} and predefined mappings.
*
* @return the outline handler
*/
public static OutlineHandler createStandardHandler() {
OutlineHandler handler = new OutlineHandler();
handler.putTagPriorityMapping(TagConstants.H1, 1);
handler.putTagPriorityMapping(TagConstants.H2, 2);
handler.putTagPriorityMapping(TagConstants.H3, 3);
handler.putTagPriorityMapping(TagConstants.H4, 4);
handler.putTagPriorityMapping(TagConstants.H5, 5);
handler.putTagPriorityMapping(TagConstants.H6, 6);
handler.putMarkPriorityMapping(TagConstants.H1, 1);
handler.putMarkPriorityMapping(TagConstants.H2, 2);
handler.putMarkPriorityMapping(TagConstants.H3, 3);
handler.putMarkPriorityMapping(TagConstants.H4, 4);
handler.putMarkPriorityMapping(TagConstants.H5, 5);
handler.putMarkPriorityMapping(TagConstants.H6, 6);
return handler;
}

/**
* Creates an OutlineHandler with custom {@link IOutlineMarkExtractor}
*
* @param extractor the mark extractor
* @return the outline handler
*/
public static OutlineHandler createHandler(IOutlineMarkExtractor extractor) {
OutlineHandler handler = new OutlineHandler();
handler.markExtractor = extractor;
return handler;
}

/**
* Put tag priority mapping.
* Get mark extractor.
*
* @return the mark extractor
*/
public IOutlineMarkExtractor getMarkExtractor(){
return markExtractor;
}

/**
* Set mark extractor.
*
* @param extractor the mark extractor
* @return the outline handler
*/
public OutlineHandler setMarkExtractor(IOutlineMarkExtractor extractor){
markExtractor = extractor;
return this;
}
/**
* Put tag into priority mapping.
*
* @param tagName the tag name
* @param priority the priority
* @return the outline handler
* @deprecated use {@link #putMarkPriorityMapping(String, Integer)} instead
*/
@Deprecated
public OutlineHandler putTagPriorityMapping(String tagName, Integer priority) {
tagPrioritiesMapping.put(tagName, priority);
putMarkPriorityMapping(tagName, priority);
return this;
}

/**
* Put all tag priority mappings.
* Put mark into priority mapping.
*
* @param markName the mark name
* @param priority the priority
* @return the outline handler
*/
public OutlineHandler putMarkPriorityMapping(String markName, Integer priority) {
markPrioritiesMapping.put(markName, priority);
return this;
}

/**
* Put all tags into priority mappings.
*
* @param mappings the mappings
* @return the outline handler
* @deprecated ue {@link #putAllMarksPriorityMappings(Map)} instead
*/
@Deprecated
public OutlineHandler putAllTagPriorityMappings(Map<String, Integer> mappings) {
tagPrioritiesMapping.putAll(mappings);
putAllMarksPriorityMappings(mappings);
return this;
}

/**
* Put all marks into priority mappings.
*
* @param mappings the mappings
* @return the outline handler
*/
public OutlineHandler putAllMarksPriorityMappings(Map<String, Integer> mappings) {
markPrioritiesMapping.putAll(mappings);
return this;
}

/**
* Gets the tag priority mapping.
* Gets the marks from priority mapping.
*
* @param tagName the tag name
* @return the tag priority mapping
* @deprecated use {@link #getMarkPriorityMapping(String)} instead
*/
@Deprecated
public Integer getTagPriorityMapping(String tagName) {
return tagPrioritiesMapping.get(tagName);
return getMarkPriorityMapping(tagName);
}

/**
* Checks for tag priority mapping.
* Gets the mark from priority mapping.
*
* @param markName the mark name
* @return the tag priority mapping
*/
public Integer getMarkPriorityMapping(String markName) {
return markPrioritiesMapping.get(markName);
}

/**
* Checks for tag in priority mapping.
*
* @param tagName the tag name
* @return true, if the tag name is listed in the tag priorities mapping
* @deprecated use {@link #hasMarkPriorityMapping(String)} instead
*/
@Deprecated
public boolean hasTagPriorityMapping(String tagName) {
return tagPrioritiesMapping.containsKey(tagName);
return hasMarkPriorityMapping(tagName);
}

/**
* Checks for tag in priority mapping.
*
* @param markName the mark name
* @return true, if the tag name is listed in the tag priorities mapping
*/
public boolean hasMarkPriorityMapping(String markName) {
if (markName == null){
return false;
} else {
return markPrioritiesMapping.containsKey(markName);
}
}

/**
Expand Down Expand Up @@ -209,10 +307,10 @@ protected String generateUniqueDestinationName(IElementNode element) {
* @return the unique destination name
*/
protected String generateOutlineName(IElementNode element) {
String tagName = element.name();
String markName = markExtractor.getMark(element);
String content = ((JsoupElementNode) element).text();
if (content.isEmpty()) {
content = getUniqueID(tagName);
content = getUniqueID(markName);
}
return content;
}
Expand All @@ -228,10 +326,11 @@ protected String generateOutlineName(IElementNode element) {
* @param context the processor context
* @return the outline handler
*/
OutlineHandler addOutlineAndDestToDocument(ITagWorker tagWorker, IElementNode element, ProcessorContext context) {
String tagName = element.name();
if (null != tagWorker && hasTagPriorityMapping(tagName) && context.getPdfDocument() != null) {
int level = (int) getTagPriorityMapping(tagName);
protected OutlineHandler addOutlineAndDestToDocument(ITagWorker tagWorker, IElementNode element,
ProcessorContext context) {
String markName = markExtractor.getMark(element);
if (null != tagWorker && hasMarkPriorityMapping(markName) && context.getPdfDocument() != null) {
int level = (int) getMarkPriorityMapping(markName);
if (null == currentOutline) {
currentOutline = context.getPdfDocument().getOutlines(false);
}
Expand Down Expand Up @@ -262,16 +361,16 @@ OutlineHandler addOutlineAndDestToDocument(ITagWorker tagWorker, IElementNode el
* @param element the element
* @return the outline handler
*/
OutlineHandler setDestinationToElement(ITagWorker tagWorker, IElementNode element) {
String tagName = element.name();
if (null != tagWorker && hasTagPriorityMapping(tagName) && destinationsInProcess.size() > 0) {
protected OutlineHandler setDestinationToElement(ITagWorker tagWorker, IElementNode element) {
String markName = markExtractor.getMark(element);
if (null != tagWorker && hasMarkPriorityMapping(markName) && destinationsInProcess.size() > 0) {
Tuple2<String, PdfDictionary> content = destinationsInProcess.pop();
if (tagWorker.getElementResult() instanceof IElement) {
tagWorker.getElementResult().setProperty(Property.DESTINATION, content);
} else {
Logger logger = LoggerFactory.getLogger(OutlineHandler.class);
logger.warn(MessageFormatUtil.format(
Html2PdfLogMessageConstant.NO_IPROPERTYCONTAINER_RESULT_FOR_THE_TAG, tagName));
Html2PdfLogMessageConstant.NO_IPROPERTYCONTAINER_RESULT_FOR_THE_TAG, markName));
}
}
return this;
Expand Down
Loading

0 comments on commit d6a4e89

Please sign in to comment.