Mercurial > hg > digilib-old
annotate servlet/src/digilib/pdf/PDFStreamWorker.java @ 817:0b4345866797 stream
fix to log message. fixed jar creation.
author | robcast |
---|---|
date | Mon, 21 Feb 2011 10:24:48 +0100 |
parents | 72662bb585ba |
children |
rev | line source |
---|---|
557 | 1 package digilib.pdf; |
546 | 2 |
3 import java.io.IOException; | |
4 import java.io.OutputStream; | |
5 import java.util.concurrent.Callable; | |
6 import java.util.concurrent.ExecutionException; | |
7 import java.util.concurrent.Future; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 | |
11 import com.itextpdf.text.Document; | |
12 import com.itextpdf.text.DocumentException; | |
13 import com.itextpdf.text.Image; | |
14 import com.itextpdf.text.PageSize; | |
15 import com.itextpdf.text.pdf.PdfWriter; | |
16 | |
17 import digilib.image.DocuImage; | |
557 | 18 import digilib.image.ImageJobDescription; |
19 import digilib.image.ImageWorker; | |
20 import digilib.servlet.DigilibConfiguration; | |
21 import digilib.servlet.PDFRequest; | |
22 import digilib.util.DigilibJobCenter; | |
23 import digilib.util.NumRange; | |
546 | 24 |
25 public class PDFStreamWorker implements Callable<OutputStream> { | |
26 | |
548 | 27 protected static Logger logger = Logger.getLogger(PDFStreamWorker.class); |
546 | 28 |
548 | 29 protected DigilibConfiguration dlConfig = null; |
546 | 30 |
548 | 31 protected Document doc = null; |
546 | 32 |
548 | 33 protected OutputStream outstream = null; |
546 | 34 |
557 | 35 protected PDFRequest job_info = null; |
546 | 36 |
548 | 37 protected DigilibJobCenter<DocuImage> imageJobCenter = null; |
546 | 38 |
39 /** | |
40 * @param dlConfig | |
41 * @param outputfile | |
42 * @param job_info | |
43 */ | |
44 public PDFStreamWorker(DigilibConfiguration dlConfig, OutputStream outputfile, | |
557 | 45 PDFRequest job_info, |
546 | 46 DigilibJobCenter<DocuImage> imageJobCenter) { |
47 super(); | |
48 this.dlConfig = dlConfig; | |
49 this.outstream = outputfile; | |
50 this.job_info = job_info; | |
51 this.imageJobCenter = imageJobCenter; | |
52 } | |
53 | |
54 public OutputStream call() throws Exception { | |
55 outstream = renderPDF(); | |
56 return outstream; | |
57 } | |
58 | |
59 /** | |
60 * @throws DocumentException | |
61 * @throws InterruptedException | |
62 * @throws ExecutionException | |
63 * @throws IOException | |
64 */ | |
65 protected OutputStream renderPDF() throws DocumentException, InterruptedException, | |
66 ExecutionException, IOException { | |
67 // create document object | |
68 doc = new Document(PageSize.A4, 0, 0, 0, 0); | |
69 PdfWriter docwriter = null; | |
70 | |
71 long start_time = System.currentTimeMillis(); | |
72 | |
73 docwriter = PdfWriter.getInstance(doc, outstream); | |
74 | |
75 setPDFProperties(doc); | |
76 | |
77 doc.open(); | |
78 | |
79 addTitlePage(doc); | |
80 | |
594 | 81 logger.debug("PDF: " + outstream + " doc.open()ed (" |
546 | 82 + (System.currentTimeMillis() - start_time) + "ms)"); |
83 | |
84 NumRange pgs = job_info.getPages(); | |
85 | |
86 for (int p : pgs) { | |
594 | 87 logger.debug("PDF: adding Image " + p + " to " + outstream); |
546 | 88 // create ImageJobInformation |
557 | 89 ImageJobDescription iji = ImageJobDescription.getInstance(job_info, job_info.getDlConfig()); |
546 | 90 iji.setValue("pn", p); |
91 addImage(doc, iji); | |
594 | 92 logger.debug("PDF: done adding Image " + p + " to " + outstream); |
546 | 93 } |
94 | |
594 | 95 logger.debug("PDF: done adding all Images to " + outstream); |
546 | 96 |
97 doc.close(); | |
594 | 98 logger.debug("PDF: " + outstream + " doc.close() (" |
546 | 99 + (System.currentTimeMillis() - start_time) + "ms)"); |
801
72662bb585ba
remove all ServletOutputStream.flush(). (stupid me ;-)
robcast
parents:
594
diff
changeset
|
100 docwriter.flush(); |
546 | 101 docwriter.close(); |
102 return outstream; | |
103 } | |
104 | |
105 /** | |
106 * Set PDF-Meta-Attributes. | |
107 */ | |
108 public Document setPDFProperties(Document doc) { | |
109 // TODO get proper Information from dlConfig | |
110 doc.addAuthor(this.getClass().getName()); | |
111 doc.addCreationDate(); | |
112 doc.addKeywords("digilib"); | |
113 doc.addTitle("digilib PDF"); | |
114 doc.addCreator(this.getClass().getName()); | |
115 return doc; | |
116 } | |
117 | |
118 /** | |
119 * Create a title page and append it to the document (should, of course, be | |
120 * called first) | |
121 * | |
122 * @throws DocumentException | |
123 */ | |
124 public Document addTitlePage(Document doc) throws DocumentException { | |
125 PDFTitlePage titlepage = new PDFTitlePage(job_info); | |
126 doc.add(titlepage.getPageContents()); | |
127 doc.newPage(); | |
128 return doc; | |
129 } | |
130 | |
131 /** | |
132 * adds an image to the document. | |
133 * | |
134 * @param doc | |
135 * @param iji | |
136 * @return | |
137 * @throws InterruptedException | |
138 * @throws ExecutionException | |
139 * @throws IOException | |
140 * @throws DocumentException | |
141 */ | |
547 | 142 public Document addImage(Document doc, ImageJobDescription iji) |
546 | 143 throws InterruptedException, ExecutionException, IOException, |
144 DocumentException { | |
145 // create image worker | |
548 | 146 ImageWorker job = new ImageWorker(dlConfig, iji); |
546 | 147 // submit |
548 | 148 Future<DocuImage> jobTicket = imageJobCenter.submit(job); |
546 | 149 // wait for result |
150 DocuImage img = jobTicket.get(); | |
151 // scale the image | |
152 Image pdfimg = Image.getInstance(img.getAwtImage(), null); | |
153 float docW = PageSize.A4.getWidth() - 2 * PageSize.A4.getBorder(); | |
154 float docH = PageSize.A4.getHeight() - 2 * PageSize.A4.getBorder(); | |
548 | 155 // TODO: do we really scale this again? |
546 | 156 pdfimg.scaleToFit(docW, docH); |
157 // add to PDF | |
158 doc.add(pdfimg); | |
159 return doc; | |
160 } | |
161 | |
162 } |