File Source: pebblepdfcreationlistener.java
/*
P/P * Method: net.sourceforge.pebble.web.listener.PebblePDFCreationListener__static_init
*
* Postconditions:
* init'ed(log)
*/
1 /*
2 * Copyright (c) 2003-2006, Simon Brown
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * - Neither the name of Pebble nor the names of its contributors may
17 * be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32 package net.sourceforge.pebble.web.listener;
33
/*
P/P * Method: void net.sourceforge.pebble.web.listener.PebblePDFCreationListener()
*
* Postconditions:
* this.headerTags == &new Properties(PebblePDFCreationListener#1)
* new Properties(PebblePDFCreationListener#1) num objects == 1
*/
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 * Add ability to manipulate PDF output document before it's closed and opened.
56 *
57 * Soltuion was adopted from: https://xhtmlrenderer.dev.java.net/servlets/ReadMsg?listName=users&msgNo=1908
58 *
59 * @author Alexander Zagniotov
60 */
61 public class PebblePDFCreationListener implements PDFCreationListener {
62
63 /** the log used by this class */
64 private static Log log = LogFactory.getLog(PebblePDFCreationListener.class);
65
66 Properties headerTags = null;
67
68 public PebblePDFCreationListener() {
69 headerTags = new Properties();
70 }
71
/*
P/P * Method: void parseMetaTags(Document)
*
* Preconditions:
* (soft) log != null
* (soft) sourceXHTML != null
* (soft) this.headerTags != null
*
* Presumptions:
* org.w3c.dom.Document:getDocumentElement(...)@76 != null
* org.w3c.dom.Element:getAttribute(...)@83 != null
* org.w3c.dom.Element:getAttribute(...)@84 != null
* org.w3c.dom.Element:getElementsByTagName(...)@76 != null
* org.w3c.dom.Element:getElementsByTagName(...)@78 != null
* ...
*
* Test Vectors:
* java.lang.String:length(...)@87: {0}, {1..232-1}
*/
72 public void parseMetaTags(Document sourceXHTML) {
73
74 try {
75
76 NodeList e = sourceXHTML.getDocumentElement().getElementsByTagName("head");
77 Element e1 = (Element) e.item(0);
78 NodeList retVal = e1.getElementsByTagName("meta");
79
80 for (int i = 0; i < retVal.getLength(); i++) {
81
82 Element thisNode = (Element) retVal.item(i);
83 String name = thisNode.getAttribute("name");
84 String content = thisNode.getAttribute("content");
85
86 //<meta name="XXXXX" content="XXXXX" />
87 if (name.length() != 0 && content.length() != 0) {
88 headerTags.setProperty(name,content);
89 }
90 }
91 }
92
93 catch (Exception e) {
94 log.warn("Could not parse header meta tags: " + e);
95 }
96 }
97
98
99 /**
100 * Called directly before calling open() on the Document.
101 * That allows you to e.g. modify headers before the document is created.
102 *
103 */
104 public void preOpen(ITextRenderer renderer) {
105
106 try {
/*
P/P * Method: void preOpen(ITextRenderer)
*
* Preconditions:
* (soft) log != null
* (soft) renderer != null
* (soft) this.headerTags != null
*
* Presumptions:
* init'ed(com.lowagie.text.pdf.PdfName.AUTHOR)
* init'ed(com.lowagie.text.pdf.PdfName.CREATOR)
* init'ed(com.lowagie.text.pdf.PdfName.KEYWORDS)
* init'ed(com.lowagie.text.pdf.PdfName.PRODUCER)
* init'ed(com.lowagie.text.pdf.PdfName.SUBJECT)
* ...
*
* Test Vectors:
* java.lang.String:equals(...)@139: {0}, {1}
* java.lang.String:equals(...)@142: {0}, {1}
* java.lang.String:equals(...)@145: {0}, {1}
* java.lang.String:equals(...)@148: {0}, {1}
* java.lang.String:equals(...)@151: {0}, {1}
* java.lang.String:equals(...)@154: {0}, {1}
* org.xhtmlrenderer.pdf.ITextRenderer:getWriter(...)@109: Inverse{null}, Addr_Set{null}
*/
107 Enumeration e = headerTags.propertyNames();
108
109 PdfWriter writer = renderer.getWriter();
110
111 if (writer == null) {
112 log.warn("PdfWriter is null, not able to set header meta tags to PDF document");
113 return;
114 }
115
116 /*
117 Available versions:
118 PdfWriter.VERSION_1_2,
119 PdfWriter.VERSION_1_3,
120 PdfWriter.VERSION_1_4, //default
121 PdfWriter.VERSION_1_5,
122 PdfWriter.VERSION_1_6,
123 PdfWriter.VERSION_1_7
124 */
125 writer.setPdfVersion(PdfWriter.VERSION_1_5);
126
127 //Full compression means that not only page streams are compressed,
128 //but some other objects as well, such as the cross-reference table.
129 //This is only possible since PDF-1.5
130 writer.setFullCompression();
131
132 while (e.hasMoreElements()) {
133
134 String key = (String) e.nextElement() ;
135 PdfString val = new PdfString(headerTags.getProperty(key), PdfObject.TEXT_UNICODE);
136
137 PdfDictionary dictionary = writer.getInfo();
138
139 if (key.equals("title" )) {
140 dictionary.put(PdfName.TITLE, val);
141 }
142 else if (key.equals("author")) {
143 dictionary.put(PdfName.AUTHOR, val);
144 }
145 else if (key.equals("subject")) {
146 dictionary.put(PdfName.SUBJECT, val);
147 }
148 else if (key.equals("keywords")) {
149 dictionary.put(PdfName.KEYWORDS, val);
150 }
151 else if (key.equals("creator")) {
152 dictionary.put(PdfName.CREATOR, val);
153 }
154 else if (key.equals("producer")) {
155 dictionary.put(PdfName.PRODUCER, val);
156 }
157 else {
158 log.warn("Unexpected header meta tag: " + key + ", value: " + val);
159 }
160 }
161 }
162 catch (Exception e) {
163 log.warn("Could not set header meta tags to PDF document: " + e);
164 }
165 }
166
167 public void onClose(ITextRenderer renderer) {
168
/*
P/P * Method: void onClose(ITextRenderer)
*
* Preconditions:
* renderer != null
* (soft) log != null
*
* Test Vectors:
* org.xhtmlrenderer.pdf.ITextRenderer:getWriter(...)@169: Inverse{null}, Addr_Set{null}
*/
169 PdfWriter writer = renderer.getWriter();
170
171 if (writer == null) {
172 log.warn("PdfWriter is null, not able to set header meta tags to PDF document");
173 return;
174 }
175
176 //Display doc title in the title bar instead of filename,
177 writer.setViewerPreferences(PdfWriter.DisplayDocTitle);
178
179 }
180 }
SofCheck Inspector Build Version : 2.22510
| pebblepdfcreationlistener.java |
2010-Jun-25 19:40:32 |
| pebblepdfcreationlistener.class |
2010-Jul-19 20:23:38 |