Is there a way to create an OSM map by a java bean, without displaying it in a web page, but instead stamping it inside a PDF which is generated with iText 5?

In my case, I want to generate kind of a serial letter as PDF, each copy containing a map, that displays a personal routing for the recipient.

So there is no need to show a map in a jsp or JSF page, only to create it and add it to the PDF.


You could achieve this using the GeoTools library, the tile client module can render an OSM map. Then instead of calling the render to draw on a MapFrame's graphic you could use a PdfGraphics2D as the output instead. The tricky bit is setting the size you want.

DefaultFontMapper mapper = new DefaultFontMapper();
PdfContentByte cb = writer.getDirectContent();

Graphics2D g2d = new PdfGraphics2D(cb, pageSize.getWidth(),
               pageSize.getHeight(), mapper ,rescale,rescale,0);
pageSize.setBorder(com.itextpdf.text.Rectangle.BOTTOM | com.itextpdf.text.Rectangle.TOP
      | com.itextpdf.text.Rectangle.LEFT | com.itextpdf.text.Rectangle.RIGHT);
  float dW = (float) (pageSize.getWidth() - canvasSize.getWidth());
  float dw2 = dW / 2.0f;
  float dH = (float) (pageSize.getHeight() - canvasSize.getHeight());
  float dh2 = dH / 2.0f;

  outputArea = new Rectangle((int) dw2, (int) dh2, canvasSize.width, canvasSize.height);
  ReferencedEnvelope dataArea = mapContent.getViewport().getBounds();
  mapPane.getRenderer().paint(g2d, outputArea, dataArea);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.