If you want to generate a PDF from your application developped with the API, you need to setup a proxy in your environment. This proxy will redirect the requests to the print server provided by api.geo.admin.ch
Hide code<script type="text/javascript">
function init() {
api22 = new GeoAdmin.API();
api22.createMapPanel({
height: 350,
renderTo: "mymap22",
bbar: new Ext.Toolbar()
});
api22.mapPanel.getBottomToolbar().add([
api22.createPrint({
text: OpenLayers.i18n('print map (popup)'),
printBaseUrl: '/apiprintproxy?path=',
printPanelOptions: {
mapPanel: api22.mapPanel
},
windowOptions: {
title: OpenLayers.i18n('print')
}
})
]);
}
</script>
<body onload="init();">
<div id="mymap22" style="width:500px;height:340px;border:1px solid grey;padding: 0 0 0 0;margin:10px !important;"></div>
<script type="text/javascript" src="https://api.geo.admin.ch/loader.js"></script>
</body>
As example, we provide here the code of a MapFish controller that can be used as proxy. The important thing is to redirect the cookies, since the print is not stateless.
import logging
import httplib2
import Cookie
from pylons import request, response, session
from pylons.controllers.util import abort
from chsdi.lib.base import BaseController
log = logging.getLogger(__name__)
class ApiprintproxyController(BaseController):
def index(self):
url_scheme = request.environ["wsgi.url_scheme"]
if url_scheme not in ("http", "https"):
abort(403) # Forbidden
if "url" in request.params:
url = request.params["url"]
if "path" not in request.params:
abort(400, "Missing an 'url' parameter") # Bad Request
printpath = request.params["path"]
# get method
method = request.method
# get body
body = None
if method in ("POST", "PUT"):
body = request.body
# forward request to target (without Host Header)
http = httplib2.Http()
h = dict(request.headers)
h.pop("Host", h)
try:
if "url" in request.params:
resp, content = http.request(url_scheme + "://api.geo.admin.ch/main/wsgi/print/" + str(printpath) + "?url=" + url, method=method, body=body, headers=h)
else:
resp, content = http.request(url_scheme + "://api.geo.admin.ch/main/wsgi/print/" + str(printpath), method=method, body=body, headers=h)
except:
abort(502) # Bad Gateway
if resp.has_key("content-type"):
response.headers["Content-Type"] = resp["content-type"]
if resp.has_key("set-cookie"):
c = Cookie.SimpleCookie()
c.load(resp["set-cookie"])
morsel = c.get('SRV')
if morsel is not None:
morsel['path'] = request.path
response.headers["Set-Cookie"] = "SRV=%s; path=%s" % ( morsel.value, morsel['path'])
if resp.has_key("Content-Disposition"):
response.headers["Content-Disposition"] = resp["Content-Disposition"]
if resp.has_key("content-disposition"):
response.headers["Content-Disposition"] = resp["content-disposition"]
response.status = resp.status
return content