简体   繁体   中英

How to convert Google Map to PDF using Javascript?

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.

如果您对多个库没问题,可以尝试使用phantom.js对html页面进行快照,然后使用node.js 转换为pdf

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM