| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
package net.sourceforge.pebble.web.listener; |
| 33 |
|
|
| 34 |
|
import org.xhtmlrenderer.pdf.ITextRenderer; |
| 35 |
|
import org.xhtmlrenderer.pdf.PDFCreationListener; |
| 36 |
|
|
| 37 |
|
import java.util.Properties; |
| 38 |
|
import java.util.Enumeration; |
| 39 |
|
|
| 40 |
|
import org.w3c.dom.Document; |
| 41 |
|
import org.w3c.dom.Element; |
| 42 |
|
import org.w3c.dom.Node; |
| 43 |
|
import org.w3c.dom.NodeList; |
| 44 |
|
|
| 45 |
|
import com.lowagie.text.pdf.PdfName; |
| 46 |
|
import com.lowagie.text.pdf.PdfString; |
| 47 |
|
import com.lowagie.text.pdf.PdfObject; |
| 48 |
|
import com.lowagie.text.pdf.PdfWriter; |
| 49 |
|
import com.lowagie.text.pdf.PdfDictionary; |
| 50 |
|
|
| 51 |
|
import org.apache.commons.logging.Log; |
| 52 |
|
import org.apache.commons.logging.LogFactory; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
@author |
| 60 |
|
|
|
|
|
| 0% |
Uncovered Elements: 69 (69) |
Complexity: 18 |
Complexity Density: 0,42 |
|
| 61 |
|
public class PebblePDFCreationListener implements PDFCreationListener { |
| 62 |
|
|
| 63 |
|
|
| 64 |
|
private static Log log = LogFactory.getLog(PebblePDFCreationListener.class); |
| 65 |
|
|
| 66 |
|
Properties headerTags = null; |
| 67 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 68 |
0
|
public PebblePDFCreationListener() {... |
| 69 |
0
|
headerTags = new Properties(); |
| 70 |
|
} |
| 71 |
|
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 5 |
Complexity Density: 0,45 |
|
| 72 |
0
|
public void parseMetaTags(Document sourceXHTML) {... |
| 73 |
|
|
| 74 |
0
|
try { |
| 75 |
|
|
| 76 |
0
|
NodeList e = sourceXHTML.getDocumentElement().getElementsByTagName("head"); |
| 77 |
0
|
Element e1 = (Element) e.item(0); |
| 78 |
0
|
NodeList retVal = e1.getElementsByTagName("meta"); |
| 79 |
|
|
| 80 |
0
|
for (int i = 0; i < retVal.getLength(); i++) { |
| 81 |
|
|
| 82 |
0
|
Element thisNode = (Element) retVal.item(i); |
| 83 |
0
|
String name = thisNode.getAttribute("name"); |
| 84 |
0
|
String content = thisNode.getAttribute("content"); |
| 85 |
|
|
| 86 |
|
|
| 87 |
0
|
if (name.length() != 0 && content.length() != 0) { |
| 88 |
0
|
headerTags.setProperty(name,content); |
| 89 |
|
} |
| 90 |
|
} |
| 91 |
|
} |
| 92 |
|
|
| 93 |
|
catch (Exception e) { |
| 94 |
0
|
log.warn("Could not parse header meta tags: " + e); |
| 95 |
|
} |
| 96 |
|
} |
| 97 |
|
|
| 98 |
|
|
| 99 |
|
|
| 100 |
|
|
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
|
|
|
| 0% |
Uncovered Elements: 42 (42) |
Complexity: 10 |
Complexity Density: 0,38 |
|
| 104 |
0
|
public void preOpen(ITextRenderer renderer) { ... |
| 105 |
|
|
| 106 |
0
|
try { |
| 107 |
0
|
Enumeration e = headerTags.propertyNames(); |
| 108 |
|
|
| 109 |
0
|
PdfWriter writer = renderer.getWriter(); |
| 110 |
|
|
| 111 |
0
|
if (writer == null) { |
| 112 |
0
|
log.warn("PdfWriter is null, not able to set header meta tags to PDF document"); |
| 113 |
0
|
return; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
| 124 |
|
|
| 125 |
0
|
writer.setPdfVersion(PdfWriter.VERSION_1_5); |
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
|
|
| 130 |
0
|
writer.setFullCompression(); |
| 131 |
|
|
| 132 |
0
|
while (e.hasMoreElements()) { |
| 133 |
|
|
| 134 |
0
|
String key = (String) e.nextElement() ; |
| 135 |
0
|
PdfString val = new PdfString(headerTags.getProperty(key), PdfObject.TEXT_UNICODE); |
| 136 |
|
|
| 137 |
0
|
PdfDictionary dictionary = writer.getInfo(); |
| 138 |
|
|
| 139 |
0
|
if (key.equals("title" )) { |
| 140 |
0
|
dictionary.put(PdfName.TITLE, val); |
| 141 |
|
} |
| 142 |
0
|
else if (key.equals("author")) { |
| 143 |
0
|
dictionary.put(PdfName.AUTHOR, val); |
| 144 |
|
} |
| 145 |
0
|
else if (key.equals("subject")) { |
| 146 |
0
|
dictionary.put(PdfName.SUBJECT, val); |
| 147 |
|
} |
| 148 |
0
|
else if (key.equals("keywords")) { |
| 149 |
0
|
dictionary.put(PdfName.KEYWORDS, val); |
| 150 |
|
} |
| 151 |
0
|
else if (key.equals("creator")) { |
| 152 |
0
|
dictionary.put(PdfName.CREATOR, val); |
| 153 |
|
} |
| 154 |
0
|
else if (key.equals("producer")) { |
| 155 |
0
|
dictionary.put(PdfName.PRODUCER, val); |
| 156 |
|
} |
| 157 |
|
else { |
| 158 |
0
|
log.warn("Unexpected header meta tag: " + key + ", value: " + val); |
| 159 |
|
} |
| 160 |
|
} |
| 161 |
|
} |
| 162 |
|
catch (Exception e) { |
| 163 |
0
|
log.warn("Could not set header meta tags to PDF document: " + e); |
| 164 |
|
} |
| 165 |
|
} |
| 166 |
|
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0,4 |
|
| 167 |
0
|
public void onClose(ITextRenderer renderer) { ... |
| 168 |
|
|
| 169 |
0
|
PdfWriter writer = renderer.getWriter(); |
| 170 |
|
|
| 171 |
0
|
if (writer == null) { |
| 172 |
0
|
log.warn("PdfWriter is null, not able to set header meta tags to PDF document"); |
| 173 |
0
|
return; |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
|
| 177 |
0
|
writer.setViewerPreferences(PdfWriter.DisplayDocTitle); |
| 178 |
|
|
| 179 |
|
} |
| 180 |
|
} |