If you just want to see a working example, you can find example code on Github. All you need to do is clone the repo, install the dependencies, and run the application using the following commands: # ...
Common problem with printing map with a legend is external CSS plugins that ruins everything, here is an actual good answer why it is like that with Bootstrap plugin. Please read it carefully.