changeset 320:18302372703f

Implemented pdf export of charts. gnv-artifacts/trunk@381 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 30 Nov 2009 09:51:59 +0000
parents 251f16a083f8
children a4376fd23f99
files gnv-artifacts/ChangeLog gnv-artifacts/pom.xml gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java
diffstat 4 files changed, 165 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/gnv-artifacts/ChangeLog	Fri Nov 27 13:00:00 2009 +0000
+++ b/gnv-artifacts/ChangeLog	Mon Nov 30 09:51:59 2009 +0000
@@ -1,3 +1,17 @@
+2009-11-30  Ingo Weinzierl <ingo.weinzierl@intevation.de>
+
+	* pom.xm: Added iText 2.1.7 dependency (to create pdf's).
+
+	* src/main/java/de/intevation/gnv/exports/ChartExportHelper.java:
+	  Implemented chart export to pdf using iText library. The chart fills the
+	  complete page. A boolean system property "export.pdf.landscape" can be
+	  used to adjust the page orientation (portrait or landscape, default is
+	  landscape).
+
+	* src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java:
+	  Added method for exporting chart to pdf. Calls the new method of
+	  ChartExportHelper.
+
 2009-11-27  Tim Englich  <tim.englich@intevation.de>
 
 	* src/test/java/de/intevation/gnv/artifacts/GNVArtifactsTestCase.java 
--- a/gnv-artifacts/pom.xml	Fri Nov 27 13:00:00 2009 +0000
+++ b/gnv-artifacts/pom.xml	Mon Nov 30 09:51:59 2009 +0000
@@ -79,6 +79,11 @@
       <version>1.0.15</version>
     </dependency>
     <dependency>
+      <groupId>com.lowagie</groupId>
+      <artifactId>itext</artifactId>
+      <version>2.1.7</version>
+    </dependency>
+    <dependency>
       <groupId>net.sf.opencsv</groupId>
       <artifactId>opencsv</artifactId>
       <version>2.0</version>
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java	Fri Nov 27 13:00:00 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/exports/ChartExportHelper.java	Mon Nov 30 09:51:59 2009 +0000
@@ -1,7 +1,17 @@
 package de.intevation.gnv.exports;
 
+import com.lowagie.text.Document;
+import com.lowagie.text.DocumentException;
+import com.lowagie.text.PageSize;
+import com.lowagie.text.Rectangle;
+import com.lowagie.text.pdf.PdfContentByte;
+import com.lowagie.text.pdf.PdfTemplate;
+import com.lowagie.text.pdf.PdfWriter;
+
 import java.awt.Transparency;
-import java.awt.image.BufferedImage;
+import java.awt.Graphics2D;
+import java.awt.geom.Rectangle2D;
+import java.awt.geom.Rectangle2D.Double;
 import java.io.OutputStream;
 import java.io.IOException;
 import javax.imageio.ImageIO;
@@ -10,12 +20,13 @@
 
 import org.jfree.chart.JFreeChart;
 
+
 /**
  * @author Ingo Weinzierl <ingo.weinzierl@intevation.de>
  */
 public class ChartExportHelper {
 
-    private static final String DEFAULT_PAGE_SIZE = "A4";
+    private static final String  DEFAULT_PAGE_SIZE = "A4";
 
     private static Logger log = Logger.getLogger(ChartExportHelper.class);
 
@@ -39,11 +50,73 @@
         );
     }
 
+
     public static void exportSVG() {
         log.info("export chart as svg (not implemented yet)");
     }
 
-    public static void exportPDF() {
-        log.info("export chart as pdf (not implemented yet)");
+
+    public static void exportPDF(
+        OutputStream out,
+        JFreeChart   chart,
+        String       pageFormat,
+        boolean      landscape,
+        float        marginLeft,
+        float        marginRight,
+        float        marginTop,
+        float        marginBottom
+    ) {
+        log.info("export chart as pdf.");
+
+        if (pageFormat == null)
+            pageFormat = DEFAULT_PAGE_SIZE;
+
+        Rectangle page = PageSize.getRectangle(pageFormat);
+        int pageWidth  = (int) (page.getRight(marginRight) - page.getLeft(marginLeft));
+        int pageHeight = (int) (page.getTop(marginTop) - page.getBottom(marginBottom));
+
+        Document document = null;
+        if (landscape) {
+            document = new Document(page.rotate());
+            log.debug("Create landscape pdf.");
+        }
+        else
+            document = new Document(page);
+
+        try {
+            PdfWriter writer = PdfWriter.getInstance(document, out);
+
+            document.addSubject(chart.getTitle().getText());
+            document.addCreationDate();
+            document.open();
+
+            PdfContentByte content  = writer.getDirectContent();
+
+            int width  = 0;
+            int height = 0;
+            if (landscape) {
+                width  = pageHeight;
+                height = pageWidth;
+            }
+            else {
+                width  = pageWidth;
+                height = pageHeight;
+            }
+
+            PdfTemplate template = content.createTemplate(width, height);
+            Graphics2D  graphics = template.createGraphics(width, height);
+            Rectangle2D area = new Rectangle2D.Double(0.0D, 0.0D,width,height);
+
+            chart.draw(graphics, area);
+            graphics.dispose();
+            content.addTemplate(template, marginLeft, marginBottom);
+        }
+        catch (DocumentException de) {
+            log.error("Error while exporting chart to pdf.", de);
+        }
+        finally {
+            document.close();
+        }
     }
 }
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Fri Nov 27 13:00:00 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/timeseries/TimeSeriesOutputTransition.java	Mon Nov 30 09:51:59 2009 +0000
@@ -68,9 +68,12 @@
  */
 public class TimeSeriesOutputTransition extends OutputTransitionBase {
 
-    protected static final boolean CACHE_CHART = 
+    protected static final boolean CACHE_CHART =
         Boolean.parseBoolean(System.getProperty("cache.chart", "false"));
 
+    protected static final boolean PDF_FORMAT_LANDSCAPE =
+        Boolean.parseBoolean(System.getProperty("export.pdf.landscape","true"));
+
     protected static final String[] IMG_EXPORT_FORMAT = {
         "PNG", "JPEG", "GIF"
     };
@@ -241,6 +244,34 @@
                     chartHeight
                 );
             }
+            else if (outputMode.equalsIgnoreCase("pdf")) {
+                log.debug("Output mode == pdf");
+
+                Locale[] serverLocales    =
+                    RessourceFactory.getInstance().getLocales();
+                Locale locale             =
+                    callMeta.getPreferredLocale(serverLocales);
+
+                log.debug(
+                    "Best locale - regarding intersection of server and " +
+                    "browser locales -  is " + locale.toString()
+                );
+
+                createPDF(
+                    outputStream,
+                    getParameters(uuid),
+                    getMeasurements(uuid),
+                    getDates(uuid),
+                    new ChartLabels(
+                        this.getFisName(callMeta.getLanguages()) +
+                        " "+ getSelectedFeatureName(uuid),
+                        this.domainLable),
+                    uuid,
+                    "A4",
+                    true,
+                    locale
+                );
+            }
             else if (outputMode.equalsIgnoreCase("csv")) {
                 log.debug("CSV-File will be generated.");
                 Collection<Result> chartResult = this.getChartResult(uuid);
@@ -449,6 +480,42 @@
     }
 
 
+    protected void createPDF(
+        OutputStream outputStream,
+        Collection   parameters,
+        Collection   measurements,
+        Collection   dates,
+        ChartLabels  chartLables,
+        String       uuid,
+        String       exportFormat,
+        boolean      landscape,
+        Locale       locale
+    ) {
+        Chart chart = getChart(
+            chartLables,
+            parameters,
+            measurements,
+            dates,
+            getChartResult(uuid),
+            locale,
+            uuid
+        );
+
+        if (chart == null) {
+            log.error("Could not initialize chart.");
+            return;
+        }
+
+        ChartExportHelper.exportPDF(
+            outputStream,
+            chart.generateChart(),
+            "A4",
+            PDF_FORMAT_LANDSCAPE,
+            50F, 50F, 50F, 50F
+        );
+    }
+
+
     protected Chart getChart(
         ChartLabels  chartLables,
         Collection   parameters,
@@ -607,4 +674,4 @@
         return null;
     }
 }
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org