How to export a map for print
A map in the browser is drawn by MapLibre GL JS on the GPU, and a screenshot of it is only as sharp as the screen. For a poster, a report or a design tool you need a file of its own: an SVG that stays sharp at any size, a PNG at the resolution of your printer, or a PDF.
The VersaTiles renderer draws a MapLibre style to such a file, without a browser and without a GPU. It reads the same tiles and styles as your web map, including all VersaTiles styles. It also works on a server: to draw map images on demand, for example a preview image of a location for social media.
WARNING
The renderer is still experimental. It does not support every feature of MapLibre GL JS, and in some cases its maps differ from what MapLibre GL JS draws. Check the result before you print it. What the renderer does not draw lists the known gaps.
| You want … | Use |
|---|---|
| one map, without writing code | the export button on tiles.versatiles.org |
| SVG from a script, in Node.js or a browser | @versatiles/svg-renderer |
| PNG at a set DPI, in Node.js | @versatiles/png-renderer |
| map images drawn on a server, e.g. for Open Graph | @versatiles/png-renderer |
| an SVG, converted | |
| an export button in your own map | @versatiles/maplibre-svg-export |
| a print layout with a legend, grid and text | QGIS |
One map, without code
- Open tiles.versatiles.org, choose a style and move the map to the area you want.
- Click the export button in the top right corner. A panel opens with a preview.
- Enter the size in pixels: 1123 × 794 for A4 landscape, or see the table of paper sizes.
- Turn on labels and icons, then download the SVG.
Open the SVG in Inkscape, Illustrator, Affinity Designer or Figma to add a title and a legend, or convert it to PDF. The panel lists the attribution of the map data: add it to your print, see Attribution.
Paper size and DPI
The renderer lays out a map in pixels, as a browser does: width and height decide how much of the world fits at a given zoom level, and how large labels and lines are next to it. The number of pixels in the finished image is a separate choice.
Take one layout pixel as 1/96 inch, the size of a CSS pixel. A label of 12 pixels is then 3.2 mm tall on paper, as it is on a screen at 100 %. Vector editors such as Inkscape and converters such as rsvg-convert use the same 96 pixels per inch, so an SVG of 1123 × 794 opens as an A4 page.
layout pixels = millimeters / 25.4 × 96
image pixels = layout pixels × DPI / 96| Paper (landscape) | Millimeters | width × height | PNG at 300 DPI (scale: 3.125) |
|---|---|---|---|
| A5 | 210 × 148 | 794 × 559 | 2481 × 1747 |
| A4 | 297 × 210 | 1123 × 794 | 3509 × 2481 |
| A3 | 420 × 297 | 1587 × 1123 | 4959 × 3509 |
| A2 | 594 × 420 | 2245 × 1587 | 7016 × 4959 |
| US Letter | 279 × 216 | 1056 × 816 | 3300 × 2550 |
For portrait, swap width and height. For other resolutions, scale is the DPI divided by 96: 1.5625 for 150 DPI, 6.25 for 600 DPI.
To show more of the world on the same paper, lower the zoom level. A larger width and height show more as well, but make labels and lines smaller on paper.
Zoom level for a map scale
A map scale such as 1:25,000 means that 1 mm on paper is 25 m on the ground. With 96 layout pixels per inch, the zoom level for a scale is:
function zoomForScale(scale, lat) {
// meters on the ground per millimeter on paper, at zoom level 0
const metersPerMillimeter = (40075016.686 * Math.cos((lat * Math.PI) / 180)) / 512 / (25.4 / 96);
return Math.log2((metersPerMillimeter * 1000) / scale);
}
zoomForScale(25000, 52.52); // 12.81: Berlin at 1:25,000The zoom level does not have to be a whole number. The scale holds at the center of the map; web maps use the Web Mercator projection, so it changes slowly towards north and south, which matters only for maps of large areas.
SVG from code
@versatiles/svg-renderer renders SVG in Node.js (22 or later) and in the browser. It has no native code.
npm install @versatiles/svg-rendererimport { renderToSVG } from '@versatiles/svg-renderer';
import { writeFile } from 'node:fs/promises';
const url = 'https://tiles.versatiles.org/assets/styles/colorful/style.json';
const style = await (await fetch(url)).json();
const svg = await renderToSVG({
style,
lon: 13.4,
lat: 52.52,
zoom: 12.81,
width: 1123, // A4 landscape
height: 794,
labels: 'glyphs',
});
await writeFile('map.svg', svg);Labels are off by default. Choose how they are drawn with labels:
'glyphs'draws the letters as outlines, with the shapes MapLibre GL JS draws. They look the same in every program, so use this for print.'glyphs-text'draws the same outlines and lays the text invisibly over them, so it can be selected and searched.'text'writes real text that you can edit in a vector editor. It names the style's fonts (Noto Sans for the VersaTiles styles), so install them, or the program will use another font.
To render many maps of one style, create an SVGMapRenderer once and call renderSVG for each view: it reads the style once and keeps the tiles it has loaded. The package README has all options, including rotation (bearing) and your own images.
PNG at a set DPI
@versatiles/png-renderer renders PNG in Node.js. It needs no GPU, so it runs on any server and in CI. It also exports renderToSVG, so one package covers both formats.
npm install @versatiles/png-rendererimport { renderToPNG } from '@versatiles/png-renderer';
import { writeFile } from 'node:fs/promises';
const url = 'https://tiles.versatiles.org/assets/styles/colorful/style.json';
const style = await (await fetch(url)).json();
const png = await renderToPNG({
style,
lon: 13.4,
lat: 52.52,
zoom: 12.81,
width: 1123, // A4 landscape
height: 794,
scale: 300 / 96, // 300 DPI: 3509 × 2481 pixels
labels: 'glyphs',
});
await writeFile('map.png', png);The PNG file does not record its DPI. When you place it in a layout program, set its size to the paper size, or its resolution to the DPI you rendered.
Map images on a server
The renderer needs no browser and no GPU, so a server can draw map images on demand. One use is a preview image for a link to a location: social networks and chat apps show the image named in the page's Open Graph tags when someone shares the link.
Create one PNGMapRenderer when the server starts. It reads the style once and keeps the tiles it has loaded, so the next image of the same area is faster:
import { PNGMapRenderer } from '@versatiles/png-renderer';
const url = 'https://tiles.versatiles.org/assets/styles/colorful/style.json';
const style = await (await fetch(url)).json();
const map = new PNGMapRenderer({ style, labels: 'glyphs', strict: true });
// 1200 × 630 is the usual size of an Open Graph image
export function renderPreview(lon, lat) {
return map.renderPNG({ lon, lat, zoom: 14, width: 1200, height: 630 });
}Serve the PNG at a URL of its own and name it in the page of the location:
<meta property="og:image" content="https://example.org/preview/13.4/52.52.png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />A few things matter on a server:
- Cache the images. An image of 1200 × 630 takes about a second to render, or half a second once its tiles are cached, while the image of a location rarely changes. Store each image, or put a CDN in front of your server.
- Don't serve gaps. With
strict: true, a render that could not load all its tiles fails with anIncompleteRenderErrorinstead of returning a map with holes. Answer with an error and try again later. - Mark the location yourself.
map.project(view, [lon, lat])gives the position of a coordinate in the image, andrenderCanvasreturns a canvas to draw a marker on. The package README shows how. - Mind where the tiles come from. Each image loads tiles like a map in the browser does. tiles.versatiles.org limits the requests per IP address, and if a commercial service relies on it with a lot of traffic, its terms ask for a donation or your own tile server.
PDF
Convert the SVG. Both tools keep the page size: an SVG of 1123 × 794 becomes an A4 page.
# librsvg (brew install librsvg, apt install librsvg2-bin)
rsvg-convert -f pdf -o map.pdf map.svg
# Inkscape
inkscape map.svg --export-filename=map.pdfThe map stays vector graphics in the PDF and prints sharp at any resolution.
An export button in your own map
@versatiles/maplibre-svg-export is the button from tiles.versatiles.org, as a control for your own MapLibre GL JS map:
<script src="https://cdn.jsdelivr.net/npm/@versatiles/maplibre-svg-export@2"></script>
<script>
map.addControl(new VersaTilesSVG.SVGExportControl(), 'top-right');
</script>Your users can then download the current view as SVG. See the demo.
Print layouts in QGIS
For a print layout with a legend, a coordinate grid, a scale bar and several maps on one page, add VersaTiles to QGIS and use its print layout. QGIS draws the map with its own renderer, so it does not look exactly like the VersaTiles styles.
What the renderer does not draw
The renderer follows MapLibre GL JS and is compared with it screenshot by screenshot. A few things are not supported yet:
- 3D: tilt (
pitch), 3D terrain, 3D buildings (fill-extrusion) and the sky. Every export is a flat map seen from above. - Relief from
raster-dem:hillshadeandcolor-relieflayers are not drawn. For relief in print, use the vector hillshading tileset, which is drawn as ordinary fill layers. - Heatmaps and line gradients.
Parts of a style that are not drawn are reported as warnings. The coverage table lists every feature.
The VersaTiles styles use the globe projection, so at low zoom levels the export shows the globe. For a flat world map, switch the style to Mercator before rendering:
style.projection = { type: 'mercator' };Attribution
A printed map needs the attribution of its data, as a web map does. For the VersaTiles styles that is © OpenStreetMap contributors, and for the landcover also ESA WorldCover 2021 (CC BY 4.0). The renderer does not draw it on the map: put it in a corner of the map, in a caption or in your list of sources. Each source of a style names its attribution in attribution.