Skip to content

Commit

Permalink
Fix XML parsing error
Browse files Browse the repository at this point in the history
  • Loading branch information
JanisSaldabols committed Jan 5, 2024
1 parent ee3d5dc commit c0f7516
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
21 changes: 19 additions & 2 deletions src/main/java/org/folio/print/server/service/PdfService.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public static byte[] createPdfFile(String htmlContent) {
if (htmlContent != null && !htmlContent.isBlank()) {
try (PDDocument document = new PDDocument();
ByteArrayOutputStream os = new ByteArrayOutputStream()) {
htmlContent = "<div>" + htmlContent + "</div>";
htmlContent = htmlContent.replace("<br>", "<br/>");
htmlContent = cleanHtmlData(htmlContent);
PDPage page = new PDPage(PDRectangle.A4);
document.addPage(page);
ITextRenderer renderer = new ITextRenderer();
Expand All @@ -46,6 +45,24 @@ public static byte[] createPdfFile(String htmlContent) {
return new byte[0];
}

private static String cleanHtmlData(String htmlContent) {
return "<div>" + htmlContent
.replace("<br>", "<br/>")
.replace("&nbsp;", "&#160;")
.replace("&lt;", "&#60;")
.replace("&gt;", "&#62;")
.replace("&amp;", "&#38;")
.replace("&quot;", "&#34;")
.replace("&apos;", "&#39;")
.replace("&ndash;", "&#8211;")
.replace("&mdash;", "&#8212;")
.replace("&copy;", "&#169;")
.replace("&reg;", "&#174;")
.replace("&nbsp;", "&#160;")
.replace("&trade;", "&#8482;")
+ "</div>";
}

/**
* Combine single print entries in batch print file.
* @param entries Entries to combine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class PdfServiceTest {

@Test
public void createPdfFile(){
byte[] result = PdfService.createPdfFile("<div><p>PDF file</p></div><br><p>Content</p>");
byte[] result = PdfService.createPdfFile("<div><p>PDF file</p></div><br><p>Content &nbsp; &lt; &gt; "
+ "&amp; &quot; &apos; &ndash; &mdash; &copy; &reg; &nbsp; &trade;</p>");
assertTrue(result.length > 0);
}

Expand Down

0 comments on commit c0f7516

Please sign in to comment.