5

Does there is any javascript code to convert generated google map into PDF using Javascript or any JS library?

Any help will be appreciated, and it will be more helpful if there is demo of such scenario.

Kara
  • 6,115
  • 16
  • 50
  • 57
Mazzu
  • 2,799
  • 20
  • 30
  • Have you seen this one Mazzu? http://stackoverflow.com/questions/2647833/google-maps-and-pdf This is a nice one as well: http://gis.stackexchange.com/questions/27510/export-map-to-pdf-using-esri-javascript-api – Luis Gouveia Mar 11 '14 at 09:19
  • 1
    @LuisGouveia, yeah I have seen it but actually it doesn't relates – Mazzu Mar 14 '14 at 11:01
  • 1
    @LuisGouveia, can your provide any further details on it? – Mazzu Mar 14 '14 at 11:14

2 Answers2

2

If your okay with multiple libraries, you could try phantom.js to snapshot the html page then use node.js to convert to pdf.

Community
  • 1
  • 1
MilesStanfield
  • 4,571
  • 1
  • 21
  • 32
2

I have a solution using 2 libraries: jspdf.min.js and html2canvas.js. add these to your index.html:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
  function export_to_pdf() {
    html2canvas(document.body, {
        useCORS: true,
        onrendered: function(canvas) {
            var img =canvas.toDataURL("image/jpeg,1.0");
            var pdf = new jsPDF();
            pdf.addImage(img, 'JPEG', 15, 40, 180, 180);
            pdf.save('a4.pdf')
        }
    });
}

explanation:

  1. html2canvas takes body or any other DOM element and creates a canvas object.
  2. canvas.toDataURL creates an image string.
  3. pdf.addImage adds the image to pdf object.
  4. pdf.save(), downloads the image.
Ehud
  • 105
  • 5