Skip to content

Commit

Permalink
Map Image PDF Export
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsCharlier committed Mar 6, 2017
1 parent 58a6748 commit cfa257b
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import com.google.common.collect.Lists;
import com.google.common.io.Closer;
import com.google.common.io.Files;
import com.lowagie.text.BadElementException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;
import com.vividsolutions.jts.awt.ShapeWriter;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Polygon;
Expand Down Expand Up @@ -188,19 +192,46 @@ private URI createMergedGraphic(final File printDirectory,
final List<URI> graphics,
final MapfishMapContext mapContext,
final String outputFormat) throws IOException, JRException {

final File mergedGraphic = File.createTempFile("map-", "." + outputFormat, printDirectory);
int width = (int) Math.round(mapContext.getMapSize().width);
int height = (int) Math.round(mapContext.getMapSize().height);
boolean isJpeg = RenderType.fromFileExtension(outputFormat) == RenderType.JPEG;
final BufferedImage bufferedImage = new BufferedImage(width, height,
(isJpeg ? this.jpegImageType.value : this.imageType.value)
);
Graphics g = bufferedImage.getGraphics();

if (isJpeg) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);

if ("pdf".equalsIgnoreCase(outputFormat)) {
com.lowagie.text.Document document = new com.lowagie.text.Document(
new com.lowagie.text.Rectangle(width, height));
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(mergedGraphic));
document.open();
PdfContentByte pdfCB = writer.getDirectContent();
Graphics g = pdfCB.createGraphics(width, height);
drawGraphics(width, height, graphics, g);
g.dispose();
document.close();
} catch (BadElementException e) {
throw new IOException(e);
} catch (DocumentException e) {
throw new IOException(e);
}
} else {
boolean isJpeg = RenderType.fromFileExtension(outputFormat) == RenderType.JPEG;
final BufferedImage bufferedImage = new BufferedImage(width, height,
(isJpeg ? this.jpegImageType.value : this.imageType.value));
Graphics g = bufferedImage.getGraphics();
if (isJpeg) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
}
drawGraphics(width, height, graphics, g);
g.dispose();
ImageIO.write(bufferedImage, outputFormat, mergedGraphic);
}


return mergedGraphic.toURI();
}

private void drawGraphics(final int width, final int height,
final List<URI> graphics, final Graphics g) throws IOException, JRException {
for (URI graphic : graphics) {
final File graphicFile = new File(graphic);
if (Files.getFileExtension(graphicFile.getName()).equals("svg")) {
Expand All @@ -214,11 +245,6 @@ private URI createMergedGraphic(final File printDirectory,
g.drawImage(image, 0, 0, width, height, null);
}
}

final File mergedGraphic = File.createTempFile("map-", "." + outputFormat, printDirectory);
ImageIO.write(bufferedImage, outputFormat, mergedGraphic);

return mergedGraphic.toURI();
}

private URI createMapSubReport(final File printDirectory,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@
p:fileSuffix="tiff" p:contentType="image/tiff"/>
<bean id="tifMapOutputFormat" class="org.mapfish.print.output.MapExportOutputFormat" scope="prototype"
p:fileSuffix="tif" p:contentType="image/tiff" />
<bean id="pdfMapOutputFormat" class="org.mapfish.print.output.MapExportOutputFormat" scope="prototype"
p:fileSuffix="pdf" p:contentType="application/pdf" />
</beans>
Binary file not shown.
100 changes: 100 additions & 0 deletions examples/src/test/resources/examples/mapexport/requestData_pdf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
{
"outputFormat": "pdf",
"layout": "plain",
"attributes": {
"mapDef": {
"width": 780,
"height": 660,
"projection": "EPSG:900913",
"dpi": 72,
"rotation": 0,
"center": [-8233518.5005945, 4980320.4059228],
"scale": 25000,
"layers": [
{
"type": "grid",
"gridType": "points",
"numberOfLines": [5,5],
"renderAsSvg": true,
"haloColor" : "#CCFFCC",
"labelColor" : "black",
"labelFormat": "%1.0f %s",
"indent": 10,
"haloRadius" : 4,
"font" : {
"name" : ["Liberation Sans", "Helvetica", "Nimbus Sans L", "Liberation Sans", "FreeSans", "Sans-serif"],
"size" : 8,
"style" : "BOLD"
}
},
{
"baseURL": "http://localhost:8080/gs-web-app/wms",
"opacity": 1,
"type": "WMS",
"layers": ["tiger:tiger_roads"],
"imageFormat": "image/png",
"styles": ["line"],
"customParams": {
"TRANSPARENT": "true"
}
},
{
"type": "WMTS",
"baseURL": "http://localhost:8080/gs-web-app/gwc/service/wmts",
"opacity": 1.0,
"layer": "tiger-ny",
"version": "1.0.0",
"requestEncoding": "KVP",
"dimensions": null,
"dimensionParams": {},
"matrixSet": "EPSG:900913",
"matrices": [
{
"identifier": "EPSG:900913:12",
"matrixSize": [4096, 4096],
"scaleDenominator": 136494.69334738597,
"tileSize": [256, 256],
"topLeftCorner": [-2.003750834E7, 2.0037508E7]
},
{
"identifier": "EPSG:900913:13",
"matrixSize": [8192, 8192],
"scaleDenominator": 68247.34667369298,
"tileSize": [256, 256],
"topLeftCorner": [-2.003750834E7, 2.0037508E7]
},
{
"identifier": "EPSG:900913:14",
"matrixSize": [16384, 16384],
"scaleDenominator": 34123.67333684649,
"tileSize": [256, 256],
"topLeftCorner": [-2.003750834E7, 2.0037508E7]
},
{
"identifier": "EPSG:900913:15",
"matrixSize": [32768, 32768],
"scaleDenominator": 17061.836668423246,
"tileSize": [256, 256],
"topLeftCorner": [-2.003750834E7, 2.0037508E7]
},
{
"identifier": "EPSG:900913:16",
"matrixSize": [65536, 65536],
"scaleDenominator": 8530.9183342116231,
"tileSize": [256, 256],
"topLeftCorner": [-2.003750834E7, 2.0037508E7]
},
{
"identifier": "EPSG:900913:17",
"matrixSize": [131072, 131072],
"scaleDenominator": 4265.4591671058115,
"tileSize": [256, 256],
"topLeftCorner": [-2.003750834E7, 2.0037508E7]
}
],
"imageFormat": "image/png"
}
]
}
}
}

0 comments on commit cfa257b

Please sign in to comment.