.. _mapfish_print:
Developer Guide - Print Server
******************************
As nearly everything in the world a thing is easy to do if you have done it once, but to get to that step it might take some time.
That documentation hopefully will help user to get the print module to run in a short time. Many thanks to Eric Lemoine who helped a lot in the mailing list.
Install MapFish Client
======================
You need to have to run a simple mapfish-client application. For that, please have a look at:
http://trac.mapfish.org/trac/mapfish/wiki/HowToInstall
You should test the examples that you find at
.......mapfish/client/examples/map/....
Install MapFish Client Print
============================
If your mapfish-examples are running let`s take the following code for a basic print-application:
.. code-block:: html
Gastronomap
help
The file proxy2.cgi should be in your cgi-bin-directory (might be /etc/apache2/cgi-bin/), it looks like::
#!/usr/bin/env python
"""This is a blind proxy that we use to get around browser
restrictions that prevent the Javascript from loading pages not on the
same server as the Javascript. This has several problems: it's less
efficient, it might break some sites, and it's a security risk because
people can use this proxy to browse the web and possibly do bad stuff
with it. It only loads pages via http and https, but it can load any
content type. It supports GET and POST requests."""
import urllib2
import cgi
import sys, os
# Designed to prevent Open Proxy type stuff.
allowedHosts = ['www.openlayers.org', 'openlayers.org',
'labs.metacarta.com', 'world.freemap.in',
'www.bsc-eoc.org','www.your_server.de','www.your_server.de:8080',
'sigma.openplans.org','osm.wheregroup.com',
'www.openstreetmap.org','demo.mapfish.org','www.camptocamp.org']
method = os.environ["REQUEST_METHOD"]
if method == "POST":
qs = os.environ["QUERY_STRING"]
d = cgi.parse_qs(qs)
if d.has_key("url"):
url = d["url"][0]
else:
url = "http://www.openlayers.org"
else:
fs = cgi.FieldStorage()
url = fs.getvalue('url', "http://www.openlayers.org")
try:
host = url.split("/")[2]
if allowedHosts and not host in allowedHosts:
print "Status: 502 Bad Gateway!!!"
print "Content-Type: text/plain"
print
print "This proxy does not allow you to access that location (%s)." % (host,)
print
print os.environ
elif url.startswith("http://") or url.startswith("https://"):
if method == "POST":
length = int(os.environ["CONTENT_LENGTH"])
headers = {"Content-Type": os.environ["CONTENT_TYPE"]}
body = sys.stdin.read(length)
r = urllib2.Request(url, body, headers)
y = urllib2.urlopen(r)
else:
y = urllib2.urlopen(url)
# print content type header
i = y.info()
if i.has_key("Content-Type"):
print "Content-Type: %s" % (i["Content-Type"])
else:
print "Content-Type: text/plain"
print
print y.read()
y.close()
else:
print "Content-Type: text/plain"
print
print "Illegal request."
except Exception, E:
print "Status: 500 Unexpected Error"
print "Content-Type: text/plain"
print
print "Some unexpected error occurred. Error text was:", E
In your Apache-configuration-file you will need an entry like::
ScriptAlias /cgi-bin/ /etc/apache2/cgi-bin/
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
You also need tho change the file ....../mapfish/client/examples/examples.js, it should have the following function::
(function() {
// Some examples, like geostat and search, use XMLHttpRequest's. If those
// XMLHttpRequest's target a different domain that the serving the page,
// some proxy is needed. So do not set ProxyHost (and comment this line) if
// the example pages are served by the same domain than that responding to
// the XMLHttpRequest's. Otherwise set up a proxy script (for example using
// the provided proxy.cgi script) and adjust the OpenLayers.ProxyHost
// variable accordingly.
OpenLayers.ProxyHost="/cgi-bin/proxy2.cgi?url=";
mapfish.SERVER_BASE_URL = 'http://www.your-server.com:8080/print-servlet-1.1/pdf/';
})();
If that application is running on your server you will get something like:
http://www.kneipenculture.de/mapfish/print_one.jpg
Install MapFish Server Print
============================
You definetely should install "firebug" in your Fillezilla-browser to do some tests if your application will run well.
By now - of course - it won`t, you will need to install Maven and Tomcat, please have a look at:
https://trac.mapfish.org/trac/mapfish/wiki/PrintModuleInstallation
By installing Tomcat (e.g. on a Debian System) you should BETTER NOT do something like "apt-get install tomcat5.5 tomcat5.5-admin tomcat5.5-webapps"
(well, I did, and it took me a couple of days to find out that this created many errors somehow), you should install Tomcat by doing::
$ wget 'http://apache.crihan.fr/dist/tomcat/tomcat-6/v6.0.20/bin/apache-tomcat-6.0.20.tar.gz'
$ tar xvzf apache-tomcat-6.0.20.tar.gz
If our Tomcat is situated e.g. in::
/apache-tomcat-6.0.20/
you need to copy the file "print-servlet-1.1.war" (probably from .........mapfish/server/java/print/print-servlet/target) into: /apache-tomcat-6.0.20/webapps
and then do::
$ cd apache-tomcat-6.0.20/bin
$ ./startup.sh
Now you should look if the "print-servlet-1.1.war" has been unpacked, means: You need a huge directory-structure in apache-tomcat-6.0.20/webapps/print-servlet-1.1/ !!
Then you should call:
http://www.your-server.com:8080/print-servlet-1.1/
you should get a side like that:
http://www.kneipenculture.de/mapfish/print_two.jpg
If you dont get this, and not even a welcome screen at
http://www.your-server.com:8080
....something does`t work well with your Tomcat.
Please be aware: Some Tomcat-versions are listening at port 8180.
.. note::
You have Apache serving your HTML/JS on one side, and Tomcat executing the print servlet on the other. Since the print JS component cannot send Ajax requests directly to Tomcat
(because of the [http://en.wikipedia.org/wiki/Same_origin_policy same origin policy]) you need to work with a proxy (in that case we work with a proxy-script
(that one, that is eg. in /etc/apache2/cgi-bin/)
Configure MapFish Print Template
================================
As described here:
https://trac.mapfish.org/trac/mapfish/wiki/PrintModuleInstallation
You need to copy the file config.yaml (probably from ......mapfish/server/java/print/print-servlet/target/print-servlet-1.1) to /apache-tomcat-6.0.20/webapps/print-servlet-1.1/
You should change it a bit e.g. like that:
Changed config.yaml
You should uregently have a look at:
https://trac.mapfish.org/trac/mapfish/wiki/PrintModuleServer
NOW it`s time for pressing the print-button in the application.
If everything works well you get a print-out like:
http://www.kneipenculture.de/mapfish/print_three.jpg
Please be aware:
If you use firebug you will see that after pressing the "print-button" the application goes to "files" like "info.json" or "create.json", e.g.
http://www.your_site.de/cgi-bin/proxy2.cgi?url=http%3A%2F%2Fwww.tourismus-maps.de%3A8080%2F
print-servlet-1.1%2Fpdf%2Finfo.json
or
http://www.your_site.de/cgi-bin/proxy2.cgi?url=http%3A%2F%2Fwww.tourismus-maps.de%3A8080%2F
print-servlet-1.1%2Fpdf%2Fcreate.json
This aren`t "files" that exist on your server but urls that reference a web service provided by the print module.
Alternative install via Apache-proxy
------------------------------------
In the four steps above the print module works via a proxy-script. Instead of this you can use Apache as a proxy. To do so please have a look at:
https://trac.mapfish.org/trac/mapfish/wiki/ConfigureProxy
Be attentive that you have enabled the relevant modules in your Apache.
If you have set the following in your Virtualhost-section in the apache-config-file::
RewriteEngine On
RewriteRule /test/(.*) http://www.your_site.com:8080/$1 [P]
Order allow,deny
Allow from all
......a call like http://www.your_site.com/test/print-servlet-1.1/
will lead you to the printing-test-application (step three).
In the file in ....../mapfish/client/examples/examples.js you need to change a bit::
(function() {
// Some examples, like geostat and search, use XMLHttpRequest's. If those
// XMLHttpRequest's target a different domain that the serving the page,
// some proxy is needed. So do not set ProxyHost (and comment this line) if
// the example pages are served by the same domain than that responding to
// the XMLHttpRequest's. Otherwise set up a proxy script (for example using
// the provided proxy.cgi script) and adjust the OpenLayers.ProxyHost
// variable accordingly.
OpenLayers.ProxyHost = null;
mapfish.SERVER_BASE_URL = 'http://www.your_site.de/test/print-servlet-1.1/pdf/';
})();
After that you can do prints via the Apache-proxy (but be aware that Order allow,deny Allow from all .....allows everyone to use your proxy).
Helpful might be also the following discussion on the mailinglist: http://www.mapfish.org/pipermail/users/2009-May/001201.html (with 11 answers)