1

So, I have a page that contains multiple bootstrap-cards - those bootstrap-cards contain an Leaflet - map inside of it, like the following:

enter image description here

In the top right corner you can see a print-button. This button executes my printCard function, that works like the following: I open a new window, append the card-html to it, and execute the window.print() function. This is because the user should be able to choose whether he wants to print the full page (via browser print), or a single card.

function printCard(ele, title){
    
    var mywindow = window.open('', 'new div', 'height=1080,width=1080');
  mywindow.document.write('<html><head><title></title>');
  
  mywindow.document.write('<script type="text/javascript" src="./javascript/jquery.min.js"><\/script>');
  mywindow.document.write('<link rel="stylesheet" href="./stylesheets/bootstrap.min.css" type="text/css" />');
  mywindow.document.write('<link rel="stylesheet" href="./stylesheets/print.css" type="text/css" />');
  mywindow.document.write('<script type="text/javascript" src="./javascript/leaflet.js"><\/script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.4.1/leaflet.markercluster-src.js"><\/script><script type="text/javascript" src="./javascript/leaflet-print.js"><\/script>');
  mywindow.document.write('</head><body >');
  
  mywindow.document.write(ele.prop("outerHTML"));
  mywindow.document.write('</body></html>');
  mywindow.document.close();
  mywindow.focus();
  setTimeout(function(){mywindow.print(); 
        mywindow.close(); 
  },1000);

  return true;
  
}

Everything works perfectly fine, the only thing is, it throws the html content all around the printing-page:

enter image description here

Any idea what the problem could be?

4
  • 1
    It is hard to say with just the code you provided but it seems to me you might be loading the JavaScript code in the "wrong" order; can you try to change the function as you can see here: jsfiddle.net/xuoyts8q/1 ?
    – secan
    Commented Aug 18, 2021 at 13:32
  • @secan Thanks for your suggestion. Sadly, changing the order didn't really help - it still shows the same result as you can see in my last screenshot..
    – Redeye
    Commented Aug 18, 2021 at 13:49
  • Then probably the issue is not with the function you posted but somewhere else. Anyway, it is impossible to tell where without knowing the content of HTML, CSS, and JavaScript code: maybe some positioning in "print.css" depends on an HTML element that you are not including in the pop-up window, maybe there is something wrong in one of your JavaScript files, maybe... who knows?
    – secan
    Commented Aug 18, 2021 at 13:56
  • 1
    @secan and you were right... I was missing a leaflet.css file in my code, importing that css file solved the issue. Thank you so much, you really helped me.
    – Redeye
    Commented Aug 18, 2021 at 14:15

1 Answer 1

0

You can use print plugin of leaflet to print map (it uses Mapfish / GeoServer print module)

Leaflet.print

Not the answer you're looking for? Browse other questions tagged or ask your own question.