Mercurial > hg > digilib-old
comparison servlet/src/digilib/pdf/PDFTitlePage.java @ 594:85e465e6a642
fixes for PDF generation
author | robcast |
---|---|
date | Fri, 07 Jan 2011 17:47:24 +0100 |
parents | 0885f5ca5b24 |
children | e8668edcb880 |
comparison
equal
deleted
inserted
replaced
591:a667d2952aab | 594:85e465e6a642 |
---|---|
1 package digilib.pdf; | 1 package digilib.pdf; |
2 | 2 |
3 import java.io.File; | |
3 import java.io.IOException; | 4 import java.io.IOException; |
4 import java.net.MalformedURLException; | 5 import java.net.MalformedURLException; |
5 import java.net.URL; | 6 import java.net.URL; |
6 | 7 |
7 import org.apache.log4j.Logger; | 8 import org.apache.log4j.Logger; |
12 import com.itextpdf.text.Element; | 13 import com.itextpdf.text.Element; |
13 import com.itextpdf.text.FontFactory; | 14 import com.itextpdf.text.FontFactory; |
14 import com.itextpdf.text.Image; | 15 import com.itextpdf.text.Image; |
15 import com.itextpdf.text.Paragraph; | 16 import com.itextpdf.text.Paragraph; |
16 | 17 |
17 | |
18 import digilib.io.DigilibInfoReader; | 18 import digilib.io.DigilibInfoReader; |
19 import digilib.io.DocuDirCache; | 19 import digilib.io.FileOpException; |
20 import digilib.servlet.PDFCache; | 20 import digilib.servlet.PDFCache; |
21 import digilib.servlet.PDFRequest; | 21 import digilib.servlet.PDFRequest; |
22 | 22 |
23 /** A class for the generation of title pages for the generated pdf documents. | 23 /** A class for the generation of title pages for the generated pdf documents. |
24 * | 24 * |
26 */ | 26 */ |
27 public class PDFTitlePage { | 27 public class PDFTitlePage { |
28 | 28 |
29 private PDFRequest job_info = null; | 29 private PDFRequest job_info = null; |
30 private DigilibInfoReader info_reader= null; | 30 private DigilibInfoReader info_reader= null; |
31 private DocuDirCache dirCache = null; | |
32 protected static Logger logger = Logger.getLogger("digilib.servlet"); | 31 protected static Logger logger = Logger.getLogger("digilib.servlet"); |
33 | 32 |
34 | 33 |
35 /** | 34 /** |
36 * Initialize a TitlePage | 35 * Initialize a TitlePage |
37 * @param pdfji | 36 * @param pdfji |
38 */ | 37 */ |
39 public PDFTitlePage(PDFRequest pdfji){ | 38 public PDFTitlePage(PDFRequest pdfji){ |
40 job_info = pdfji; | 39 job_info = pdfji; |
41 dirCache = (DocuDirCache) job_info.getDlConfig().getValue("servlet.dir.cache"); | |
42 | 40 |
43 String fn = getBase(dirCache.getDirectory(pdfji.getImageJobInformation().getAsString("fn")).getDir().getPath()) + "presentation/info.xml"; | 41 // use MPIWG-style info.xml |
44 | 42 info_reader = getInfoXmlReader(pdfji); |
45 info_reader = new DigilibInfoReader(fn); | |
46 } | 43 } |
44 | |
45 /** | |
46 * @param pdfji | |
47 * @return | |
48 */ | |
49 protected DigilibInfoReader getInfoXmlReader(PDFRequest pdfji) { | |
50 try { | |
51 // try to load ../presentation/info.xml | |
52 File imgDir = pdfji.getImageJobInformation().getFileDirectory().getDir(); | |
53 File docDir = imgDir.getParentFile(); | |
54 File infoFn = new File(new File(docDir, "presentation"), "info.xml"); | |
55 return new DigilibInfoReader(infoFn.getAbsolutePath()); | |
56 } catch (FileOpException e) { | |
57 logger.warn("info.xml not found"); | |
58 } | |
59 return null; | |
60 } | |
47 | 61 |
48 /** | 62 /** |
49 * generate iText-PDF-Contents for the title page | 63 * generate iText-PDF-Contents for the title page |
50 * | 64 * |
51 * @return | 65 * @return |
56 | 70 |
57 // add vertical whitespace | 71 // add vertical whitespace |
58 for(int i=0; i<8; i++){ | 72 for(int i=0; i<8; i++){ |
59 content.add(Chunk.NEWLINE); | 73 content.add(Chunk.NEWLINE); |
60 } | 74 } |
61 | |
62 | 75 |
63 // add logo | 76 // add logo |
64 content.add(getLogo()); | 77 content.add(getLogo()); |
65 content.add(Chunk.NEWLINE); | 78 content.add(Chunk.NEWLINE); |
66 content.add(Chunk.NEWLINE); | 79 content.add(Chunk.NEWLINE); |
87 | 100 |
88 content.add(Chunk.NEWLINE); | 101 content.add(Chunk.NEWLINE); |
89 content.add(Chunk.NEWLINE); | 102 content.add(Chunk.NEWLINE); |
90 content.add(Chunk.NEWLINE); | 103 content.add(Chunk.NEWLINE); |
91 | 104 |
92 // add credits | |
93 content.add(new Paragraph("MPIWG Berlin 2009", FontFactory.getFont(FontFactory.HELVETICA,10))); | |
94 | |
95 // add digilib version | 105 // add digilib version |
96 content.add(new Paragraph(getDigilibVersion(),FontFactory.getFont(FontFactory.HELVETICA,10))); | 106 content.add(new Paragraph(getDigilibVersion(),FontFactory.getFont(FontFactory.HELVETICA,10))); |
97 | 107 |
98 for(int i=0; i<8; i++){ | 108 for(int i=0; i<8; i++){ |
99 content.add(Chunk.NEWLINE); | 109 content.add(Chunk.NEWLINE); |
107 | 117 |
108 | 118 |
109 return content; | 119 return content; |
110 } | 120 } |
111 | 121 |
112 /** | 122 /* |
113 * return base directory of an image directory | |
114 * | |
115 * @param path | |
116 * @return | |
117 */ | |
118 private String getBase(String path){ | |
119 if(path.contains("/")){ | |
120 String[] x = path.split("/"); | |
121 String newpath = ""; | |
122 for(int i=0; i<x.length-1; i++){ | |
123 newpath += x[i]+"/"; | |
124 } | |
125 return newpath; | |
126 } | |
127 else | |
128 return ""; | |
129 } | |
130 | |
131 | |
132 /** | |
133 * Methods for the different attributes. | 123 * Methods for the different attributes. |
134 * | 124 * |
135 */ | 125 */ |
136 | |
137 | 126 |
138 private Image getLogo(){ | 127 private Image getLogo(){ |
139 try { | 128 try { |
140 URL url = new URL(job_info.getDlConfig().getAsString("pdf-logo")); | 129 URL url = new URL(job_info.getDlConfig().getAsString("pdf-logo")); |
141 if(url!=null && !url.equals("")){ | 130 if(url!=null && !url.equals("")){ |
143 logo.setAlignment(Element.ALIGN_CENTER); | 132 logo.setAlignment(Element.ALIGN_CENTER); |
144 return logo; | 133 return logo; |
145 } | 134 } |
146 } catch (BadElementException e) { | 135 } catch (BadElementException e) { |
147 logger.error(e.getMessage()); | 136 logger.error(e.getMessage()); |
148 e.printStackTrace(); | |
149 } catch (MalformedURLException e) { | 137 } catch (MalformedURLException e) { |
150 logger.error(e.getMessage()); | 138 logger.error(e.getMessage()); |
151 e.printStackTrace(); | |
152 } catch (IOException e) { | 139 } catch (IOException e) { |
153 logger.error(e.getMessage()); | 140 logger.error(e.getMessage()); |
154 e.printStackTrace(); | |
155 } | 141 } |
156 return null; | 142 return null; |
157 } | 143 } |
144 | |
158 private String getTitle(){ | 145 private String getTitle(){ |
159 if(info_reader.hasInfo()) | 146 if(info_reader.hasInfo()) |
160 return info_reader.getAsString("title"); | 147 return info_reader.getAsString("title"); |
161 else | 148 else |
162 return job_info.getImageJobInformation().getAsString("fn"); | 149 return job_info.getImageJobInformation().getAsString("fn"); |
163 } | 150 } |
151 | |
164 private String getAuthor(){ | 152 private String getAuthor(){ |
165 if(info_reader.hasInfo()) | 153 if(info_reader.hasInfo()) |
166 return info_reader.getAsString("author"); | 154 return info_reader.getAsString("author"); |
167 else | 155 else |
168 return " "; | 156 return " "; |
169 } | 157 } |
158 | |
170 private String getDate(){ | 159 private String getDate(){ |
171 if(info_reader.hasInfo()) | 160 if(info_reader.hasInfo()) |
172 return info_reader.getAsString("date"); | 161 return info_reader.getAsString("date"); |
173 else | 162 else |
174 return " "; | 163 return " "; |
175 } | 164 } |
165 | |
176 private String getPages(){ | 166 private String getPages(){ |
177 return "Pages "+job_info.getAsString("pgs") + " (scan page numbers)"; | 167 return "Pages "+job_info.getAsString("pgs") + " (scan page numbers)"; |
178 } | 168 } |
179 | 169 |
180 private String getDigilibVersion(){ | 170 private String getDigilibVersion(){ |