updated code for html content for pdf
This commit is contained in:
@@ -14,6 +14,7 @@ import net.gepafin.tendermanagement.entities.*;
|
||||
import net.gepafin.tendermanagement.model.request.FieldLabelValuePairRequest;
|
||||
import net.gepafin.tendermanagement.model.response.*;
|
||||
import net.gepafin.tendermanagement.service.CallService;
|
||||
import net.gepafin.tendermanagement.util.PdfUtils;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.util.Validator;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -207,7 +208,9 @@ public class PdfDao {
|
||||
|
||||
// Loop through the list of strings and create a cell for each string
|
||||
for (String item : values) {
|
||||
PdfPCell valueCell = new PdfPCell(new Phrase(item, valueFont));
|
||||
|
||||
// PdfPCell valueCell = new PdfPCell(new Phrase(item, valueFont));
|
||||
PdfPCell valueCell = PdfUtils.htmlToPdfPCell(item, valueFont);
|
||||
valueCell.setPadding(5f); // Increase padding for better spacing
|
||||
valueCell.setPaddingLeft(leftMargin); // Increase left margin for value
|
||||
valueCell.setBorder(Rectangle.NO_BORDER); // Remove border for value cell
|
||||
@@ -280,7 +283,8 @@ public class PdfDao {
|
||||
if(Boolean.TRUE.equals(Utils.isItalianFormattedAmount(fieldValue)) ){
|
||||
fieldValue= String.valueOf(Utils.convertToItalianFormat(fieldValue));
|
||||
}
|
||||
PdfPCell valueCell = new PdfPCell(new Phrase(fieldValue1, valueFont));
|
||||
// PdfPCell valueCell = new PdfPCell(new Phrase(fieldValue1, valueFont));
|
||||
PdfPCell valueCell = PdfUtils.htmlToPdfPCell(fieldValue1, valueFont);
|
||||
valueCell.setPadding(5f); // Increase padding for better spacing
|
||||
valueCell.setPaddingLeft(leftMargin); // Increase left margin for value
|
||||
valueCell.setBorder(Rectangle.NO_BORDER); // Remove border for value cell
|
||||
@@ -359,7 +363,8 @@ public class PdfDao {
|
||||
if (!headersAdded) {
|
||||
for (String key : orderedKeys) {
|
||||
String headerValue = stateFieldMap.get(key); // Header text
|
||||
PdfPCell headerCell = new PdfPCell(new Phrase(headerValue)); // Create a new PdfPCell for the header
|
||||
// PdfPCell headerCell = new PdfPCell(new Phrase(headerValue)); // Create a new PdfPCell for the header
|
||||
PdfPCell headerCell = PdfUtils.htmlToPdfPCell(headerValue, null);
|
||||
headerCell.setHorizontalAlignment(Element.ALIGN_CENTER); // Center align
|
||||
headerCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
||||
headerCell.setBackgroundColor(new BaseColor(178, 190, 181)); // Light gray background for header
|
||||
@@ -379,7 +384,8 @@ public class PdfDao {
|
||||
if(Boolean.TRUE.equals(Utils.isItalianFormattedAmount(fieldValue)) ){
|
||||
fieldValue= String.valueOf(Utils.convertToItalianFormat(fieldValue));
|
||||
}
|
||||
PdfPCell dataCell = new PdfPCell(new Phrase(fieldValue != null ?fieldValue: "", textFont));
|
||||
// PdfPCell dataCell = new PdfPCell(new Phrase(fieldValue != null ?fieldValue: "", textFont));
|
||||
PdfPCell dataCell = PdfUtils.htmlToPdfPCell(value != null ? value.toString() : "", textFont);
|
||||
dataCell.setBackgroundColor(new BaseColor(239, 243, 248)); // Light blue for the cell
|
||||
dataCell.setMinimumHeight(rowHeight);
|
||||
dataCell.setPadding(7f);
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
package net.gepafin.tendermanagement.util;
|
||||
import com.itextpdf.text.DocumentException;
|
||||
import com.itextpdf.text.Element;
|
||||
import com.itextpdf.text.Font;
|
||||
import com.itextpdf.text.pdf.PdfPCell;
|
||||
import com.itextpdf.text.html.simpleparser.HTMLWorker;
|
||||
import com.itextpdf.text.html.simpleparser.StyleSheet;
|
||||
import com.itextpdf.text.Paragraph;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PdfUtils {
|
||||
|
||||
|
||||
|
||||
public static PdfPCell htmlToPdfPCell(String htmlContent, Font font) {
|
||||
PdfPCell cell = new PdfPCell();
|
||||
|
||||
// Check if the content is not null or empty
|
||||
if (htmlContent != null && !htmlContent.trim().isEmpty()) {
|
||||
// Wrap plain text in a paragraph tag if it doesn't contain any HTML
|
||||
// Check if the string does not contain any '<' or '>' characters
|
||||
if (!htmlContent.contains("<") || !htmlContent.contains(">")) {
|
||||
htmlContent = "<p>" + htmlContent + "</p>"; // Wrap in paragraph tags
|
||||
}
|
||||
|
||||
try {
|
||||
// Create a list to hold the elements parsed from the HTML
|
||||
List<Element> elements = HTMLWorker.parseToList(new StringReader(htmlContent), new StyleSheet());
|
||||
|
||||
// Create a paragraph to hold the formatted text
|
||||
Paragraph paragraph = new Paragraph();
|
||||
|
||||
// Add each element to the paragraph
|
||||
for (Element element : elements) {
|
||||
// If the element is a Paragraph, set the font directly
|
||||
if (element instanceof Paragraph) {
|
||||
((Paragraph) element).setFont(font);
|
||||
}
|
||||
// If the element is a Chunk, set the font directly
|
||||
else if (element instanceof com.itextpdf.text.Chunk) {
|
||||
((com.itextpdf.text.Chunk) element).setFont(font);
|
||||
}
|
||||
// Add the element to the paragraph
|
||||
paragraph.add(element);
|
||||
}
|
||||
|
||||
// Add the paragraph to the cell
|
||||
cell.addElement(paragraph);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(); // Log the exception for debugging
|
||||
}
|
||||
}
|
||||
|
||||
return cell;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user