Coverage Report - net.sourceforge.pebble.web.listener.PebblePDFCreationListener
 
Classes in this File Line Coverage Branch Coverage Complexity
PebblePDFCreationListener
0%
0/53
0%
0/24
5
 
 1  
 /*
 2  
  * Copyright (c) 2003-2011, 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  
 
 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  0
         private static Log log = LogFactory.getLog(PebblePDFCreationListener.class);
 65  
         
 66  0
         Properties headerTags = null;
 67  
 
 68  0
         public PebblePDFCreationListener()  {
 69  0
                 headerTags = new Properties();
 70  0
         }
 71  
 
 72  
         public void parseMetaTags(Document sourceXHTML) {
 73  
         
 74  
                 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  
                                         //<meta name="XXXXX" content="XXXXX" />
 87  0
                                         if (name.length() != 0 && content.length() != 0) {
 88  0
                                                 headerTags.setProperty(name,content);
 89  
                                         }
 90  
                                 }
 91  
                 }
 92  
     
 93  0
                 catch (Exception e)  {
 94  0
                                 log.warn("Could not parse header meta tags: " + e);
 95  0
                 }
 96  0
      }
 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 {
 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  
                                         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  0
                         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  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  0
                                 }
 161  
                         }
 162  0
                         catch (Exception e)  {
 163  0
                                 log.warn("Could not set header meta tags to PDF document: " + e);
 164  0
                         }
 165  0
         }
 166  
 
 167  
     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  
                 //Display doc title in the title bar instead of filename,
 177  0
                 writer.setViewerPreferences(PdfWriter.DisplayDocTitle);
 178  
 
 179  0
         }
 180  
 }