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)");
|
|
100 docwriter.close();
|
|
101 return outstream;
|
|
102 }
|
|
103
|
|
104 /**
|
|
105 * Set PDF-Meta-Attributes.
|
|
106 */
|
|
107 public Document setPDFProperties(Document doc) {
|
|
108 // TODO get proper Information from dlConfig
|
|
109 doc.addAuthor(this.getClass().getName());
|
|
110 doc.addCreationDate();
|
|
111 doc.addKeywords("digilib");
|
|
112 doc.addTitle("digilib PDF");
|
|
113 doc.addCreator(this.getClass().getName());
|
|
114 return doc;
|
|
115 }
|
|
116
|
|
117 /**
|
|
118 * Create a title page and append it to the document (should, of course, be
|
|
119 * called first)
|
|
120 *
|
|
121 * @throws DocumentException
|
|
122 */
|
|
123 public Document addTitlePage(Document doc) throws DocumentException {
|
|
124 PDFTitlePage titlepage = new PDFTitlePage(job_info);
|
|
125 doc.add(titlepage.getPageContents());
|
|
126 doc.newPage();
|
|
127 return doc;
|
|
128 }
|
|
129
|
|
130 /**
|
|
131 * adds an image to the document.
|
|
132 *
|
|
133 * @param doc
|
|
134 * @param iji
|
|
135 * @return
|
|
136 * @throws InterruptedException
|
|
137 * @throws ExecutionException
|
|
138 * @throws IOException
|
|
139 * @throws DocumentException
|
|
140 */
|
547
|
141 public Document addImage(Document doc, ImageJobDescription iji)
|
546
|
142 throws InterruptedException, ExecutionException, IOException,
|
|
143 DocumentException {
|
|
144 // create image worker
|
548
|
145 ImageWorker job = new ImageWorker(dlConfig, iji);
|
546
|
146 // submit
|
548
|
147 Future<DocuImage> jobTicket = imageJobCenter.submit(job);
|
546
|
148 // wait for result
|
|
149 DocuImage img = jobTicket.get();
|
|
150 // scale the image
|
|
151 Image pdfimg = Image.getInstance(img.getAwtImage(), null);
|
|
152 float docW = PageSize.A4.getWidth() - 2 * PageSize.A4.getBorder();
|
|
153 float docH = PageSize.A4.getHeight() - 2 * PageSize.A4.getBorder();
|
548
|
154 // TODO: do we really scale this again?
|
546
|
155 pdfimg.scaleToFit(docW, docH);
|
|
156 // add to PDF
|
|
157 doc.add(pdfimg);
|
|
158 return doc;
|
|
159 }
|
|
160
|
|
161 }
|