| 1 |
|
package org.europa.together.application; |
| 2 |
|
|
| 3 |
|
import java.io.File; |
| 4 |
|
import java.io.FileNotFoundException; |
| 5 |
|
import java.io.FileOutputStream; |
| 6 |
|
import java.io.IOException; |
| 7 |
|
import java.io.OutputStream; |
| 8 |
|
import java.util.ArrayList; |
| 9 |
|
import java.util.List; |
| 10 |
|
import org.europa.together.application.internal.PdfDocument; |
| 11 |
|
import org.europa.together.application.internal.PdfReplacedElementFactory; |
| 12 |
|
import org.europa.together.business.Logger; |
| 13 |
|
import org.europa.together.business.PdfRenderer; |
| 14 |
|
import org.europa.together.domain.LogLevel; |
| 15 |
|
import org.europa.together.utils.StringUtils; |
| 16 |
|
import org.jsoup.Jsoup; |
| 17 |
|
import org.jsoup.nodes.Document; |
| 18 |
|
import org.openpdf.text.pdf.PdfStamper; |
| 19 |
|
import org.springframework.stereotype.Repository; |
| 20 |
|
import org.xhtmlrenderer.layout.SharedContext; |
| 21 |
|
import org.xhtmlrenderer.pdf.ITextRenderer; |
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
@Repository |
| |
|
| 100% |
Uncovered Elements: 0 (63) |
Complexity: 19 |
Complexity Density: 0.43 |
|
| 29 |
|
public class OpenPdfRenderer implements PdfRenderer { |
| 30 |
|
|
| 31 |
|
private static final long serialVersionUID = 11L; |
| 32 |
|
private static final Logger LOGGER = new LogbackLogger(OpenPdfRenderer.class); |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
private String format = "size: 21cm 29.7cm; margin: 20mm 20mm;"; |
| 36 |
|
|
| 37 |
|
private String title = ""; |
| 38 |
|
private String subject = ""; |
| 39 |
|
private String author = ""; |
| 40 |
|
private String keywords = ""; |
| 41 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
| 42 |
7 |
@Override... |
| 43 |
|
public PdfDocument loadDocument(final File pdfDocument) |
| 44 |
|
throws IOException { |
| 45 |
4 |
if (!pdfDocument.exists()) { |
| 46 |
1 |
throw new FileNotFoundException(); |
| 47 |
|
} |
| 48 |
3 |
return new PdfDocument(pdfDocument.getAbsolutePath()); |
| 49 |
|
} |
| 50 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 51 |
2 |
@Override... |
| 52 |
|
public void writeDocument(final PdfDocument pdf, final String destination) |
| 53 |
|
throws IOException, FileNotFoundException { |
| 54 |
2 |
PdfStamper pdfStamper = new PdfStamper(pdf, new FileOutputStream(destination)); |
| 55 |
2 |
pdfStamper.close(); |
| 56 |
|
} |
| 57 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
| 58 |
1 |
@Override... |
| 59 |
|
public PdfDocument removePage(final PdfDocument pdf, final int... pages) |
| 60 |
|
throws IOException { |
| 61 |
1 |
PdfDocument newPDF = new PdfDocument(pdf); |
| 62 |
1 |
int pagesTotal = newPDF.getNumberOfPages(); |
| 63 |
1 |
List<Integer> allPages = new ArrayList<>(pagesTotal); |
| 64 |
6 |
for (int i = 1; i <= pagesTotal; i++) { |
| 65 |
5 |
allPages.add(i); |
| 66 |
|
} |
| 67 |
1 |
for (Integer page : pages) { |
| 68 |
3 |
allPages.remove(page); |
| 69 |
|
} |
| 70 |
1 |
newPDF.selectPages(allPages); |
| 71 |
1 |
LOGGER.log("Document contains " + newPDF.getNumberOfPages() + " Pages", |
| 72 |
|
LogLevel.DEBUG); |
| 73 |
1 |
return newPDF; |
| 74 |
|
} |
| 75 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (16) |
Complexity: 3 |
Complexity Density: 0.19 |
|
| 76 |
4 |
@Override... |
| 77 |
|
public void renderDocumentFromHtml(final String file, final String template) |
| 78 |
|
throws FileNotFoundException, IOException { |
| 79 |
4 |
StringBuilder html = new StringBuilder(); |
| 80 |
4 |
html.append("<html>") |
| 81 |
|
.append("<head>") |
| 82 |
|
.append("<meta charset=\"UTF-8\" />") |
| 83 |
|
.append("<title>").append( |
| 84 |
|
StringUtils.escapeXmlCharacters(getTitle())).append("</title>") |
| 85 |
|
.append("<meta name=\"author\" content=\"").append( |
| 86 |
|
StringUtils.escapeXmlCharacters(getAuthor())).append("\" />") |
| 87 |
|
.append("<meta name=\"subject\" content=\"").append( |
| 88 |
|
StringUtils.escapeXmlCharacters(getSubject())).append("\" />") |
| 89 |
|
.append("<meta name=\"keywords\" content=\"").append( |
| 90 |
|
StringUtils.escapeXmlCharacters(getKeywords())).append("\" />") |
| 91 |
|
.append("<style>") |
| 92 |
|
.append("@page {").append(format).append("}") |
| 93 |
|
.append("</style>") |
| 94 |
|
.append("</head>") |
| 95 |
|
.append("<body>") |
| 96 |
|
.append(template) |
| 97 |
|
.append("</body></html>"); |
| 98 |
|
|
| 99 |
4 |
Document document = Jsoup.parse(html.toString(), "UTF-8"); |
| 100 |
4 |
document.outputSettings().syntax(Document.OutputSettings.Syntax.xml); |
| 101 |
4 |
LOGGER.log("test", LogLevel.DEBUG); |
| 102 |
4 |
try (OutputStream os = new FileOutputStream(file)) { |
| 103 |
3 |
ITextRenderer renderer = new ITextRenderer(); |
| 104 |
3 |
SharedContext sharedContext = renderer.getSharedContext(); |
| 105 |
3 |
sharedContext.setPrint(true); |
| 106 |
3 |
sharedContext.setInteractive(false); |
| 107 |
|
|
| 108 |
3 |
sharedContext.setReplacedElementFactory(new PdfReplacedElementFactory()); |
| 109 |
3 |
sharedContext.getTextRenderer().setSmoothingThreshold(0); |
| 110 |
|
|
| 111 |
3 |
renderer.setDocumentFromString(createWellFormedHtml(html.toString())); |
| 112 |
3 |
renderer.layout(); |
| 113 |
3 |
renderer.createPDF(os); |
| 114 |
|
} |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 118 |
4 |
@Override... |
| 119 |
|
public void setAuthor(final String author) { |
| 120 |
4 |
this.author = author; |
| 121 |
|
} |
| 122 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 123 |
4 |
@Override... |
| 124 |
|
public void setKeywords(final String keywords) { |
| 125 |
4 |
this.keywords = keywords; |
| 126 |
|
} |
| 127 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 128 |
4 |
@Override... |
| 129 |
|
public void setSubject(final String subject) { |
| 130 |
4 |
this.subject = subject; |
| 131 |
|
} |
| 132 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 133 |
4 |
@Override... |
| 134 |
|
public void setTitle(final String title) { |
| 135 |
4 |
this.title = title; |
| 136 |
|
} |
| 137 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 138 |
1 |
@Override... |
| 139 |
|
public void setFormat(final String format) { |
| 140 |
1 |
this.format = format; |
| 141 |
|
} |
| 142 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 143 |
5 |
@Override... |
| 144 |
|
public String getAuthor() { |
| 145 |
5 |
return author; |
| 146 |
|
} |
| 147 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 148 |
5 |
@Override... |
| 149 |
|
public String getKeywords() { |
| 150 |
5 |
return keywords; |
| 151 |
|
} |
| 152 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 153 |
5 |
@Override... |
| 154 |
|
public String getSubject() { |
| 155 |
5 |
return subject; |
| 156 |
|
} |
| 157 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 158 |
5 |
@Override... |
| 159 |
|
public String getTitle() { |
| 160 |
5 |
return title; |
| 161 |
|
} |
| 162 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 163 |
1 |
@Override... |
| 164 |
|
public String getFormat() { |
| 165 |
1 |
return format; |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 169 |
3 |
private String createWellFormedHtml(final String html) {... |
| 170 |
3 |
Document content = Jsoup.parse(html, "UTF-8"); |
| 171 |
3 |
content.outputSettings().syntax( |
| 172 |
|
Document.OutputSettings.Syntax.xml); |
| 173 |
3 |
return content.html(); |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
} |