annotate servlet/src/digilib/pdf/PDFStreamWorker.java @ 593:7768ea8f59cf stream

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