Question

I am new in LeafletJS. I am trying to print map in LeafletJS. My code for print is like this:

     printProvider = L.print.provider({
      capabilities: printConfig,
      method: 'GET',
      dpi: 254,
      autoLoad: true,
      // outputFormat: 'pdf',
      customParams: {
          mapTitle: 'Print Test',
          comment: 'Testing Leaflet printing'
      }
  });
  // Create a print control with the configured provider and add to the map
  printControl = L.control.print({
      provider: printProvider
  });
  map.addControl(printControl);

But when I click the print button error comes like this

Proxy Error.
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /mapfish-print/pdf/print.pdf.

Reason: Error reading from remote server

Can anyone help me?

Was it helpful?

Solution

You can use jQuery Plugin to print a map.

The code is as simple as

$('#map').print();

Here is the codepen.

OTHER TIPS

Have you added this line to your html ?

<script src="http://apps2.geosmart.co.nz/mapfish-print/pdf/info.json?var=printConfig"></script>

Plus I think you have to enable CORS.

I think you've missed to add some host to the list of allowed hosts in config.yaml in your mapfish print server.

If you want to allow all hosts you can add:

- !ipMatch
  host:0.0.0.0
  mask:0.0.0.0
//Printing
function printMyMap(){
   let myMapHTML= document.getElementById("myMap");
   let mywindow = window.open("", "PrintTheMap","width=600,height=800");

 let header = '<html><head><link rel="stylesheet"  href="/your path/Site.css" media="print" /> <link rel="stylesheet"  href="/your path/leaflet.css" /> </head>
 //Adding the header to the window
 mywindow.document.write(header);
 //Adding the map into the body
 mywindow.document.write("<body>"+myMapHTML+"<body>");
 mywindow.document.close(); // necessary for IE >= 10
 mywindow.focus(); // necessary for IE >= 10
 mywindow.print();
 mywindow.close();

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top