As already quoted, html2canvas
is a great library to use, before we get started keep in mind:
-
html2canvas
does not work real DOM elements it just simulates them with Canvas
- It is considered alpha or experimental, so it still has a lot to evolve (though in most tests it did very well)
- To access images of different domains, you will need to use proxy (made by languages such as php, python, java, etc.).
- I believe the use of
<iframe>
is not yet supported, so the best in this case is to use the google-maps API instead of iframes .
Using the Google Maps API
An example of using Google Maps would be:
var parametreCarteVillage = {
zoom : 9,
center : new google.maps.LatLng(38.959409, -87.289124),
disableDoubleClickZoom : false,
draggable : true,
scrollwheel : true,
panControl : false,
disableDefaultUI : true,
mapTypeControl : true,
keyboardShortcuts : true,
mapTypeId : google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'), parametreCarteVillage);
var marker = new google.maps.Marker({
position:new google.maps.LatLng(38.959409,-87.289124),
map: map,
title: 'Titulo!'
});
#map_canvas{
height: 400px;
width: 600px;
border: 1px #c0c0c0 solid;
}
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script><scripttype="text/javascript" src="https://www.google.com/jsapi"></script><divid="map_canvas"></div>
To add more options to the map, read the documentation
Using the html2canvas
I recommend downloading the 0.5.0-alpha version at link
and include in the page that will use Google Maps, should look something like:
<script src="html2canvas.js"></script>
html2canvas(document.getElementById("map_canvas"), {
"logging": true //Habilita os logs
}).then(function(canvas) {
var img = new Image();
img.onload = function () {
img.onload = null;
document.getElementById("output").appendChild(img);
};
img.src = canvas.toDataURL("image/png");
});
But as I mentioned earlier, in order to access images from different domains, in case your domain accesses google images, you must use CORS
, but the images are in the Google domain and we have no control, so we will have to use proxy.
Proxy in this case is not the technology to use a different ip on your machine, but a script that runs on the server and displays the image of the external domain as if it were in your domain, or even three domains seu-site.com
, maps.google.com
and proxy.seu-site.com
it makes use of CORS
or data URI scheme
.
Proxy for html2canvas
I developed four proxies in different languages:
-
Proxy in PHP : link
-
C # proxy : link
-
Proxy in Python : link (supports any framework)
-
Proxy in VbScript (for classic asp) : link
The usage would be something like (example with aspx
):
html2canvas(document.getElementById("map_canvas"), {
"logging": true, //Habilita os logs
"proxy":"html2canvasproxy.ashx"
}).then(function(canvas) {
var img = new Image();
img.onload = function () {
img.onload = null;
document.getElementById("output").appendChild(img);
};
img.src = canvas.toDataURL("image/png");
});
List of options to use in html2canvas(..., {opções}).then(...)
+-----------------+---------+-----------+----------------------+
| Opção | Tipo | padrão | Descrição |
+-----------------+---------+-----------+----------------------+
| allowTaint | boolean | false | Permite causar o |
| | | | taint quando houver |
| | | | imagens cross-origin |
+-----------------+---------+-----------+----------------------+
| background | string | #fff | Troca a cor de fundo |
| | | | do canvas, se não |
| | | | espeficicado no dom |
| | | | use 'undefined' para |
| | | | transparente |
+-----------------+---------+-----------+----------------------+
| height | number | null | Limita a altura do |
| | | | em pixels. Se 'null' |
| | | | irá renderezar com |
| | | | a altura total da ja-|
| | | | nela |
+-----------------+---------+-----------+----------------------+
| letterRendering | boolean | false | Usado para renderizar|
| | | | cada letra separada- |
| | | | mente. É necessário |
| | | | se estiver usando |
| | | | 'letter-spacing' |
+-----------------+---------+-----------+----------------------+
| logging | boolean | false | Quando 'true' mostra |
| | | | log no console do |
| | | | navegador |
+-----------------+---------+-----------+----------------------+
| proxy | string | undefined | A url do proxy é usa-|
| | | | da para carregar ima-|
| | | | gens cross-origin. |
| | | | Se não definido ou |
| | | | vazio as imagens não |
| | | | serão carregadas |
+-----------------+---------+-----------+----------------------+
| taintTest | boolean | true | Testa cada imagem |
| | | | para antes de dese- |
| | | | nhar, para verificar |
| | | | se irá manchar o can-|
| | | | vas. |
+-----------------+---------+-----------+----------------------+
| timeout | number | 0 |Tempo de espera para |
| | | | o carregamento das |
| | | | imagens em milesegun-|
| | | | dos. Se 0 não haverá |
| | | | esperar |
+-----------------+---------+-----------+----------------------+
| width | number | null | Limita a largura do |
| | | | em pixels. Se 'null' |
| | | | irá renderezar com |
| | | | a largura total da |
| | | | janela |
+-----------------+---------+-----------+----------------------+
| useCORS | boolean | false | Se 'true' tenta car- |
| | | | regar as imagens com |
| | | | cross-origem, se não |
| | | | tenta usar o proxy |
+-----------------+---------+-----------+----------------------+
Reported: How to use jsPDF addHTML?