How to create invoice programmatically in magento 2?

We just need to pass order id in generateInvoice($orderId) function from template. Below code snippet is used for generating Invoice in Magento 2, After generating an invoice automatically send the invoice mail to a customer who has placed the order.

Call function from a template,