comparison servlet/src/main/java/digilib/pdf/PDFStreamWorker.java @ 883:7ffb45138f61 mvnify

rearrange sources to maven directory standard
author robcast
date Tue, 19 Apr 2011 18:44:25 +0200
parents servlet/src/digilib/pdf/PDFStreamWorker.java@f1d1f8c9225f
children
comparison
equal deleted inserted replaced
882:2bba166f4608 883:7ffb45138f61
1 package digilib.pdf;
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;
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;
24
25 public class PDFStreamWorker implements Callable<OutputStream> {
26
27 protected static Logger logger = Logger.getLogger(PDFStreamWorker.class);
28
29 protected DigilibConfiguration dlConfig = null;
30
31 protected Document doc = null;
32
33 protected OutputStream outstream = null;
34
35 protected PDFRequest job_info = null;
36
37 protected DigilibJobCenter<DocuImage> imageJobCenter = null;
38
39 /**
40 * @param dlConfig
41 * @param outputfile
42 * @param job_info
43 */
44 public PDFStreamWorker(DigilibConfiguration dlConfig, OutputStream outputfile,
45 PDFRequest job_info,
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
81 logger.debug("PDF: " + outstream + " doc.open()ed ("
82 + (System.currentTimeMillis() - start_time) + "ms)");
83
84 NumRange pgs = job_info.getPages();
85
86 for (int p : pgs) {
87 logger.debug("PDF: adding Image " + p + " to " + outstream);
88 // create ImageJobInformation
89 ImageJobDescription iji = ImageJobDescription.getInstance(job_info, job_info.getDlConfig());
90 iji.setValue("pn", p);
91 addImage(doc, iji);
92 logger.debug("PDF: done adding Image " + p + " to " + outstream);
93 }
94
95 logger.debug("PDF: done adding all Images to " + outstream);
96
97 doc.close();
98 logger.debug("PDF: " + outstream + " doc.close() ("
99 + (System.currentTimeMillis() - start_time) + "ms)");
100 docwriter.flush();
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 */
142 public Document addImage(Document doc, ImageJobDescription iji)
143 throws InterruptedException, ExecutionException, IOException,
144 DocumentException {
145 // create image worker
146 ImageWorker job = new ImageWorker(dlConfig, iji);
147 // submit
148 Future<DocuImage> jobTicket = imageJobCenter.submit(job);
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();
155 // TODO: do we really scale this again?
156 pdfimg.scaleToFit(docW, docH);
157 // add to PDF
158 doc.add(pdfimg);
159 return doc;
160 }
161
162 }