]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.export.core/src/org/simantics/export/core/pdf/ExportPdfWriter.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / pdf / ExportPdfWriter.java
1 package org.simantics.export.core.pdf;
2
3 import java.awt.Graphics2D;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.security.KeyStore;
9 import java.security.KeyStoreException;
10 import java.security.NoSuchAlgorithmException;
11 import java.security.PrivateKey;
12 import java.security.UnrecoverableKeyException;
13 import java.security.cert.Certificate;
14 import java.security.cert.CertificateException;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.simantics.Simantics;
22 import org.simantics.databoard.binding.mutable.Variant;
23 import org.simantics.export.core.ExportContext;
24 import org.simantics.export.core.error.ExportException;
25 import org.simantics.export.core.intf.Format;
26 import org.simantics.export.core.manager.Content;
27 import org.simantics.utils.page.MarginUtils.Margins;
28 import org.simantics.utils.page.PageDesc;
29 import org.simantics.utils.page.PageOrientation;
30
31 import com.lowagie.text.Document;
32 import com.lowagie.text.DocumentException;
33 import com.lowagie.text.Element;
34 import com.lowagie.text.ExceptionConverter;
35 import com.lowagie.text.Font;
36 import com.lowagie.text.PageSize;
37 import com.lowagie.text.Phrase;
38 import com.lowagie.text.Rectangle;
39 import com.lowagie.text.pdf.AcroFields;
40 import com.lowagie.text.pdf.BadPdfFormatException;
41 import com.lowagie.text.pdf.ColumnText;
42 import com.lowagie.text.pdf.FontMapper;
43 import com.lowagie.text.pdf.PdfContentByte;
44 import com.lowagie.text.pdf.PdfCopy;
45 import com.lowagie.text.pdf.PdfDictionary;
46 import com.lowagie.text.pdf.PdfFileSpecification;
47 import com.lowagie.text.pdf.PdfImportedPage;
48 import com.lowagie.text.pdf.PdfReader;
49 import com.lowagie.text.pdf.PdfSignatureAppearance;
50 import com.lowagie.text.pdf.PdfStamper;
51 import com.lowagie.text.pdf.PdfTemplate;
52 import com.lowagie.text.pdf.PdfWriter;
53
54 /**
55  * A PDF writer object.  
56  *
57  * @author toni.kalajainen@semantum.fi
58  */
59 public class ExportPdfWriter {
60
61         /** PDF Document */
62         public Document document;
63
64         /** PDF Output stream */
65         public PdfCopy pdfCopy; 
66         
67         /** Open output stream */
68         public FileOutputStream fos;
69
70         /** The direct content byte of the document */
71         public PdfContentByte cb;
72         
73         /** Contains Pdf Templates, e.g. symbols. Resource Uri -> Template mapping */
74         public Map<String, PdfTemplate> templates = new HashMap<String, PdfTemplate>();
75         
76         /** Pages */
77         public List<Page> pages = new ArrayList<Page>(); 
78         
79         /** Suggested Page Desc */
80         public PageDesc defaultPageDesc;
81         
82         /** PageDesc as PDF rectangle */
83         public Rectangle defaultRectangle;
84         
85         /** Initialized FontMapper */
86         public FontMapper fontMapper;
87         
88         /** The output file */
89         public File outputFile;
90         
91         /** All options */
92         public Variant options;
93         
94         /** Export Context */
95         public ExportContext ctx;
96         
97         /** The margins the user selected. */
98         public Margins margins;
99         
100         /** Compression Level */
101         public int compressionLevel;
102
103         /**
104          * Create new page.
105          * 
106          * @param (Optional) page description. If null is used, the default size is used.
107          * @return Page for writing
108          * @throws ExportException
109          */
110         public Page createPage( PageDesc pd ) throws ExportException {
111                                 
112                 Rectangle rect; 
113                 if ( pd == null || pd.isInfinite() ) {
114                         pd = defaultPageDesc;
115                         rect = defaultRectangle;
116                 } else {
117                         rect = toRectangle( pd );
118                 }
119                 
120                 Page page = new Page( pd, rect, pages.size() );                         
121                 pages.add(page);                
122                 return page;
123         }
124         
125         /**
126          * Create a new template.
127          * 
128          * Note, template is not visible on the document until you add it with 
129          *   cb.addTemplate( template.tp, 0, 0);
130          *  
131          * @param name (Optional) Template identifier 
132          * @param pd (Optional) template size description. If null is used, the default size is used.
133          * @return Template handle
134          * @throws ExportException
135          */
136         public Template createTemplate( String name, PageDesc pd ) throws ExportException {
137                 Rectangle rect; 
138                 if ( pd == null || pd.isInfinite() ) {
139                         pd = defaultPageDesc;
140                         rect = defaultRectangle;
141                 } else {
142                         rect = toRectangle( pd );
143                 }
144                 
145         int w = (int) pd.getWidth();
146         int h = (int) pd.getHeight();
147         PdfTemplate tp = cb.createTemplate(w, h);        
148                 Template canvas = new Template( name, pd, rect, tp );
149                 canvas.name = name;
150         if ( name!=null ) templates.put(name, tp);              
151                 return canvas;
152         }       
153         
154         /**
155          * Sign the file with a private+public key pair (PPK).
156          * The file must be closed already. 
157          * 
158          * @param keystoreFile the keystore file
159          * @param keystorePassword (optional) password 
160          * @param privateKeyPassword (optional) password
161          * @param signLocation (optional) sign locaiton, e.g. "Helsinki"
162          * @param signReason (optional) e.g. "approved"
163          * @throws ExportException 
164          */
165         public void sign( File keystoreFile, String keystorePassword, String privateKeyPassword, String signLocation, String signReason) throws ExportException {
166                 // Add Bouncycastle, if found. If not, try anyway.
167                 /*
168         if (providerAdded.compareAndSet(false, true)) {
169                 try {
170                         String className = "org.bouncycastle.jce.provider.BouncyCastleProvider";
171                         Class<?> clazz = Class.forName(className);
172                         Provider provide = (Provider) clazz.newInstance();
173                         Security.addProvider( provide );
174                 } catch (SecurityException se) {
175                         se.printStackTrace();
176                 } catch (NullPointerException npe) {
177                         npe.printStackTrace();
178                 } catch (ClassNotFoundException e) {
179                         e.printStackTrace();
180                         } catch (InstantiationException e) {
181                                 e.printStackTrace();
182                         } catch (IllegalAccessException e) {
183                                 e.printStackTrace();
184                         }
185         }*/
186
187         // Sign
188                 FileInputStream  ksfis = null;
189                 FileInputStream  fis = null;
190                 FileOutputStream fos = null;
191                 File signedFile = null;
192                 try {
193                         KeyStore ks = KeyStore.getInstance("pkcs12");
194                         signedFile = new File( outputFile.getCanonicalPath()+".signed" );
195                         if (signedFile.exists()) signedFile.delete();
196                         ksfis = new FileInputStream(keystoreFile);
197                         fis = new FileInputStream(outputFile);
198                         fos = new FileOutputStream( signedFile );
199                         ks.load(ksfis, keystorePassword != null ? keystorePassword.toCharArray() : null);
200                                         
201                         List<String> aliases = Collections.list(ks.aliases());
202                         String alias = aliases.get(0);
203                         PrivateKey key = (PrivateKey)ks.getKey(alias, privateKeyPassword != null ? privateKeyPassword.toCharArray() : null);
204                         Certificate[] chain = ks.getCertificateChain(alias);
205
206                         PdfReader reader = new PdfReader( fis );
207                         PdfStamper stp = PdfStamper.createSignature(reader, fos, '\0');
208                         PdfSignatureAppearance sap = stp.getSignatureAppearance();
209
210                         /// Signature
211                         String fieldName = "sign"; //signReason==null?"sign":URIUtil.encodeFilename( signReason );
212                 AcroFields af = stp.getAcroFields();
213                 AcroFields.Item item = af.getFieldItem(fieldName);
214                         if (signReason!=null) sap.setReason( signReason );
215                         if (signLocation!=null) sap.setLocation( signLocation );
216                         sap.setCrypto(key, chain, null, PdfSignatureAppearance.SELF_SIGNED);
217                         sap.setCertificationLevel(PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED);
218                         sap.setRender(PdfSignatureAppearance.SignatureRenderNameAndDescription);
219                         
220                         // Make field the signature
221                         //sap.setVisibleSignature(fieldName);
222                         
223                         // Visible signature
224                 //AcroFields af = stp.getAcroFields();
225                 //AcroFields.Item item = af.getFieldItem(fieldName);
226                         //sap.setVisibleSignature(new Rectangle(0, 0, 100, 10), 0, fieldName);
227                                         
228                         // comment next line to have an invisible signature
229                         //sap.setVisibleSignature(new Rectangle(682, 130, 822, 145), 1, "approved_by");
230                                         
231                         //stp.getAcroFields().setField(fieldName, "someValue");
232                                         
233                         stp.close();    
234                         reader.close();
235                         fis.close();
236                 } catch (DocumentException e) {
237                         throw new ExportException( e.getClass().getName()+": "+e.getMessage(), e );
238                 } catch (UnrecoverableKeyException e) {
239                         throw new ExportException( e.getClass().getName()+": "+e.getMessage(), e );
240                 } catch (NoSuchAlgorithmException e) {
241                         throw new ExportException( e.getClass().getName()+": "+e.getMessage(), e );
242                 } catch (CertificateException e) {
243                         throw new ExportException( e.getClass().getName()+": "+e.getMessage(),e  );
244                 } catch (IOException e) {
245                         throw new ExportException( e.getClass().getName()+": "+e.getMessage(), e );
246                 } catch (KeyStoreException e) {
247                         throw new ExportException( e.getClass().getName()+": "+e.getMessage(), e );
248                 } finally {
249                         if ( ksfis != null ) try { ksfis.close(); } catch (IOException e) {}
250                         if ( fis != null ) try { fis.close(); } catch (IOException e) {}
251                         if ( fos != null ) try { fos.close(); } catch (IOException e) {}
252                                         
253                         if ( signedFile!=null && signedFile.exists() && outputFile.exists() ) {
254                                 outputFile.delete();
255                                 signedFile.renameTo( outputFile );
256                         }
257                         
258                 }
259         }
260         
261         public void addAttachment(Content content) throws ExportException {
262                 try {
263                         if ( content.tmpFile == null ) throw new ExportException("Could not export "+content.filename+", null file.");
264                         if ( !content.tmpFile.exists() ) throw new ExportException("Could not export "+content.filename+", file not found.");
265         
266                         Format format = ctx.eep.getFormat( content.formatId );
267                         //byte[] data = StreamUtil.readFully( content.tmpFile );
268                     PdfDictionary fileParameter = new PdfDictionary();      
269             PdfFileSpecification spec = PdfFileSpecification.fileEmbedded(
270                         pdfCopy,
271                         content.tmpFile.getAbsolutePath(),
272                         content.filename, 
273                         null, 
274                         true, 
275                         "application/simantics/"+format.id(), 
276                         fileParameter);
277             
278             pdfCopy.addFileAttachment( content.filename, spec );            
279             
280                         
281                 } catch (IOException e) {
282                         throw new ExportException( e.getClass().getName()+": "+e.getMessage() );
283                 }
284         }
285         
286         public void close() throws ExportException {
287                 // Flush & close
288                 try {
289                         if ( pages.isEmpty() ) {
290                                 Page page = createPage(null);                           
291                                 Graphics2D g2d = page.createGraphics(true);
292                                 try {
293                                         g2d.drawString("This page is intentionally left blank.", 100, 100);
294                                 } finally {
295                                         g2d.dispose();
296                                 }
297                         }
298                         
299                         for ( Page page : pages ) page.close();
300                         
301                         Font f = new Font(Font.HELVETICA, 8);
302
303                         int totalPages = 0;
304                         int currentPage = 1;
305                         
306                         for ( Page page : pages ) {
307                                 PdfReader reader = new PdfReader( page.tmpFile.getAbsolutePath() );
308                                 try {
309                                 totalPages += reader.getNumberOfPages();
310                                 } finally {
311                                         reader.close();
312                                 }
313                         }
314                         
315                         for ( Page page : pages ) {
316                                 PdfReader reader = new PdfReader( page.tmpFile.getAbsolutePath() );
317                                 try {
318                                 int n = reader.getNumberOfPages();
319
320                                 for (int i = 1; i <= n; ++i) {
321                                     Rectangle pageSize = reader.getPageSizeWithRotation(i);
322
323                                     PdfImportedPage imp = pdfCopy.getImportedPage(reader, i);
324                                     PdfCopy.PageStamp ps = pdfCopy.createPageStamp(imp);
325
326                                     PdfContentByte over = ps.getOverContent();
327
328                                     ColumnText.showTextAligned(over, Element.ALIGN_RIGHT,
329                                                 new Phrase(
330                                                                 String.format("%d / %d", currentPage++, totalPages), f),
331                                                                 pageSize.getWidth()-12, 12, 0);
332                                     ps.alterContents();
333                                     pdfCopy.addPage(imp);
334                                     
335                                 }
336                                 } finally {
337                                         reader.close();
338                                 }
339                 }                                               
340                 } catch (IOException e) {
341                         throw new ExportException( e );
342                 } catch (ExceptionConverter e) {
343                         throw new ExportException( e );
344                 } catch (BadPdfFormatException e) {
345                         throw new ExportException( e );
346                 } catch (Exception e) {
347                         throw new ExportException( e );
348                 } finally {
349                         for ( Page page : pages ) {
350                                 if ( page.tmpFile != null ) { page.tmpFile.delete(); page.tmpFile = null; }                             
351                         }
352                         pages.clear();
353                         
354                         if ( document != null ) { document.close(); document = null; }
355                         if ( pdfCopy != null ) { pdfCopy.close(); pdfCopy = null; }
356                         if ( fos != null ) { try {
357                                 fos.close();
358                         } catch (IOException e) {
359                                 throw new ExportException(e);
360                         } fos = null; }
361                 }
362         }
363         
364         public class Page {
365
366                 /** PDF Output stream */
367                 public PdfWriter pdfWriter;
368                 
369                 /** PDF Document */
370                 public Document document;
371                 
372                 /** Open output stream */
373                 public FileOutputStream fos;
374
375                 /** The direct content byte of the document */
376                 public PdfContentByte cb;
377                 
378                 /** Tmp-file where the page is written to */
379                 public File tmpFile;
380                 
381                 /** Suggested Page Desc */
382                 public PageDesc pageDesc;
383                 
384                 /** PageDesc as PDF rectangle */
385                 public Rectangle rectangle;
386                 
387                 /** Page number */
388                 public int pageNumber;
389                 
390                 Page(PageDesc pageDesc, Rectangle rect, int pageNumber) throws ExportException {
391                         try {
392                                 this.pageDesc = pageDesc;
393                                 this.rectangle = rect;
394                                 this.pageNumber = pageNumber;
395                                 this.tmpFile = Simantics.getTempfile("export.core", "pdf");
396                                 this.fos = new FileOutputStream( tmpFile, false );                      
397                                 this.document = new Document(rectangle);                        
398                                 this.document.setPageSize( rect ); // redundant?
399                                 this.pdfWriter = PdfWriter.getInstance(document, fos);
400                                 this.pdfWriter.setPdfVersion(PdfWriter.PDF_VERSION_1_7);
401                                 this.pdfWriter.setCompressionLevel( compressionLevel );
402                                 this.pdfWriter.setPageEvent(new ServiceBasedPdfExportPageEvent());
403                                 this.document.open();
404                                 this.cb = this.pdfWriter.getDirectContent();
405                                 if (!this.document.newPage()) throw new ExportException("Failed to create new page.");
406                         } catch (IOException e) {
407                                 throw new ExportException( e );
408                         } catch (DocumentException e) {
409                                 throw new ExportException( e );
410                         }
411                 }
412                 
413                 /**
414                  * Create a graphics 2d Context that uses millimeters.
415                  * 
416                  * @param applyMargins top left position of margins is applied
417                  * @return graphics 2d context
418                  */
419                 public Graphics2D createGraphics(boolean applyMargins) {
420             float w = rectangle.getWidth();
421             float h = rectangle.getHeight();
422                         double pw = pageDesc.getOrientedWidth();
423                         double ph = pageDesc.getOrientedHeight();
424                         Graphics2D g2d = cb.createGraphics(w, h, fontMapper);
425                         
426                         if ( applyMargins ) {
427                                 Margins m = pageDesc.getMargins();
428
429                     double mw = pw - m.left.diagramAbsolute - m.right.diagramAbsolute;
430                     double mh = ph - m.top.diagramAbsolute - m.bottom.diagramAbsolute;
431                     double sx = m.left.diagramAbsolute;
432                     double sy = m.top.diagramAbsolute;
433                     
434                     // Convert to points
435                     mw = PageDesc.toPoints( mw );
436                     mh = PageDesc.toPoints( mh );
437                     sx = PageDesc.toPoints( sx );
438                     sy = PageDesc.toPoints( sy );
439                     
440                                 g2d.translate(sx, sy);
441                         }
442                         
443                         g2d.scale(w/pw, h/ph);          
444                         return g2d;
445                 }
446                 
447                 /**
448                  * Of area inside the margins, return the width of the page in millimeters. 
449                  * 
450                  * @return width (mm)
451                  */
452                 public double getWidth() {
453                         Margins m = pageDesc.getMargins();
454                         return pageDesc.getOrientedWidth() - m.left.diagramAbsolute - m.right.diagramAbsolute; 
455                 }
456                 
457                 public double getHeight() {
458                         Margins m = pageDesc.getMargins();
459                         return pageDesc.getOrientedHeight() - m.top.diagramAbsolute - m.bottom.diagramAbsolute; 
460                 }
461                 
462                 /**
463                  * Add attachment to this page
464                  * @param content
465                  * @throws ExportException
466                  */
467                 public void addAttachment(Content content) throws ExportException {
468                         /*
469                         try {
470                                 if ( content.tmpFile == null ) throw new ExportException("Could not export "+content.filename+", null file.");
471                                 if ( !content.tmpFile.exists() ) throw new ExportException("Could not export "+content.filename+", file not found.");
472                 
473                                 Format format = ctx.eep.getFormat( content.formatId );
474                                 //byte[] data = StreamUtil.readFully( content.tmpFile );
475                             PdfDictionary fileParameter = new PdfDictionary();      
476                     PdfFileSpecification spec = PdfFileSpecification.fileEmbedded(
477                                 pdfWriter,
478                                 content.tmpFile.getAbsolutePath(),
479                                 content.filename, 
480                                 null, 
481                                 true, 
482                                 "application/simantics/"+format.id(), 
483                                 fileParameter);
484                     
485                     pdfWriter.addFileAttachment( content.filename, spec );                                              
486                         } catch (IOException e) {
487                                 throw new ExportException( e.getClass().getName()+": "+e.getMessage() );
488                         }*/
489                         ExportPdfWriter.this.addAttachment(content);
490                 }
491                 
492                 public void close() throws ExportException {
493                         try {
494                                 if ( document != null ) { document.close(); document = null; }
495                                 if ( pdfWriter != null ) { pdfWriter.close(); pdfWriter = null; }
496                                 if ( fos != null ) { fos.close(); fos = null; }
497                                 if ( cb != null ) { cb = null; }
498                         } catch (IOException e) {
499                                 throw new ExportException(e);
500                         }
501                 }
502                 
503         }
504         
505         public class Template {
506
507                 /** Suggested Page Desc */
508                 public PageDesc pageDesc;
509                 
510                 /** PageDesc as PDF rectangle */
511                 public Rectangle rectangle;
512                 
513                 /** Template name */
514                 public String name;
515                 
516                 /** PdfTemplate */
517                 public PdfTemplate tp;
518
519                 Template(String name, PageDesc pd, Rectangle rect, PdfTemplate tp) {
520                         this.pageDesc = pd;
521                         this.rectangle = rect;
522                         this.name = name;
523                         this.tp = tp;
524                 }
525                 
526                 public Graphics2D createGraphics() {                    
527             double w = pageDesc.getWidth();
528             double h = pageDesc.getHeight();
529                         return tp.createGraphics((float) w, (float) h, fontMapper);
530                 }
531                 
532         }
533         
534     public static Rectangle toRectangle(PageDesc pageDesc) {
535         String arg = PageDesc.toPoints(pageDesc.getWidth()) + " " + PageDesc.toPoints(pageDesc.getHeight());
536         Rectangle r = PageSize.getRectangle(arg);
537
538         if (PageOrientation.Landscape == pageDesc.getOrientation())
539             r = r.rotate();
540
541         // Disable inherent borders from the PDF writer.
542         r.setBorder(0);
543
544         return r;
545     }
546         
547 }