View Javadoc
1   package fr.ifremer.tutti.service;
2   
3   /*
4    * #%L
5    * Tutti :: Service
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2012 - 2014 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU General Public License as
13   * published by the Free Software Foundation, either version 3 of the
14   * License, or (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public
22   * License along with this program.  If not, see
23   * <http://www.gnu.org/licenses/gpl-3.0.html>.
24   * #L%
25   */
26  
27  import freemarker.cache.ClassTemplateLoader;
28  import freemarker.ext.beans.BeansWrapper;
29  import freemarker.template.Configuration;
30  import freemarker.template.Template;
31  import org.apache.commons.logging.Log;
32  import org.apache.commons.logging.LogFactory;
33  import org.nuiton.jaxx.application.ApplicationTechnicalException;
34  import org.xhtmlrenderer.pdf.ITextRenderer;
35  
36  import java.io.File;
37  import java.io.FileOutputStream;
38  import java.io.OutputStream;
39  import java.io.StringWriter;
40  import java.io.Writer;
41  import java.util.Locale;
42  
43  import static org.nuiton.i18n.I18n.t;
44  
45  /**
46   * Created on 11/22/14.
47   *
48   * @author Tony Chemit - chemit@codelutin.com
49   * @since 3.10
50   */
51  public class PdfGeneratorService extends AbstractTuttiService {
52  
53      /** Logger. */
54      private static final Log log = LogFactory.getLog(PdfGeneratorService.class);
55  
56      protected Configuration freemarkerConfiguration;
57  
58      @Override
59      public void setServiceContext(TuttiServiceContext context) {
60          super.setServiceContext(context);
61  
62          freemarkerConfiguration = new Configuration();
63  
64          // needed to overwrite "Defaults to default system encoding."
65          // fix encoding issue on some systems
66          freemarkerConfiguration.setEncoding(Locale.getDefault(), "UTF-8");
67  
68          // specific template loader to get template from jars (classpath)
69          ClassTemplateLoader templateLoader = new ClassTemplateLoader(getClass(), "/ftl");
70          freemarkerConfiguration.setTemplateLoader(templateLoader);
71  
72          // pour les maps dans les template (entre autre)
73          freemarkerConfiguration.setObjectWrapper(new BeansWrapper());
74  
75      }
76  
77      public void generatePdf(File targetFile, Locale locale, String templateName, Object model) {
78  
79          if (log.isInfoEnabled()) {
80              log.info("Will generate pdf from template " + templateName + " at " + targetFile);
81          }
82          try {
83  
84              // Get freemarker template
85              Template mapTemplate = freemarkerConfiguration.getTemplate(templateName, locale);
86  
87              Writer out = new StringWriter();
88              mapTemplate.process(model, out);
89  
90              out.flush();
91  
92              // render template output as pdf
93              try (OutputStream os = new FileOutputStream(targetFile)) {
94  
95                  ITextRenderer renderer = new ITextRenderer();
96                  renderer.setDocumentFromString(out.toString());
97                  renderer.layout();
98                  renderer.createPDF(os);
99  
100                 os.close();
101             }
102 
103         } catch (Exception ex) {
104             throw new ApplicationTechnicalException(t("tutti.service.exportPdf.error", targetFile), ex);
105         }
106     }
107 }