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