As already quoted, html2canvas
is a great library for use, before we start 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 (although in most tests it did very well)
- To access images from different domains you will need to use proxy (done by languages like php, python, java, etc).
- I believe the use of
<iframe>
not yet supported, so the best thing 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>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="map_canvas"></div>
To add more options to the map read to documentation
Using the html2canvas
I recommend downloading the version 0.5.0-alpha in https://github.com/niklasvh/html2canvas/releases
and include on 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, to access images from different domains, in case your domain accesses the google images, you will need to 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 rather a script that runs on the server and displays the image of the external domain as if I’ve been in your domain, or even if it’s three domains seu-site.com
, maps.google.com
and proxy.seu-site.com
he makes use of CORS
or of data URI scheme
.
Proxy for html2canvas
I developed four proxys in different languages:
The use 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(...)
Option |
Type |
pattern |
Description |
allowTaint |
boolean |
false |
Allows to cause Taint when there are cross-origin images |
background |
string |
#fff |
Change the background color of the canvas, if not specific in the gift use undefined for transparent |
height |
number |
null |
Limits the height of the in pixels. If null will render with the full height of the window |
letterRendering |
boolean |
false |
Used to render each letter separately. Required if using letter-spacing |
logging |
boolean |
false |
When true shows log in browser console |
proxy |
string |
undefined |
The proxy url is used to load cross-origin images. If not set or empty images will not be loaded |
taintTest |
boolean |
true |
Test each image for before drawing, to see if it will stain the <canvas> |
timeout |
number |
0 |
Waiting time for loading images in mileseconds. If 0 there will be no waiting |
width |
number |
null |
Limits the width of the in pixels. If null will render with the full width of the window |
useCORS |
boolean |
false |
If true tries to load cross-source images if you do not try to use the proxy |
Reported: How to use jsPDF addHTML?
I believe there is nothing for PHP to support this. Wkhtmltopdf I believe is the only option. The good thing is that it is extremely fast, although huge (+40MB), and uses a real engine to render the page. It is not 100% stable in what it comes to
iframe
, however.– Vinícius Gobbo A. de Oliveira