How to Create Invoice programmatically in magento 2?

Create and Capture Invoice Programmatically in Magento 2 required an order id.

Given code, a snippet is used to generating an Invoice in Magento, After generating an invoice automatically send the invoice mail to a customer who has placed the order.

I have just given a demo to create an invoice with the specified order id,

We just need to pass the order id in the generateInvoice($orderId) function in the given code snippet.

Code is checking the Order by order id, validate it, check for the invoice applicable for the order, prepare the Invoice, set qty for the invoice record, set offline capture mode, and finally create a transaction object for the Invoice.

Call function from the specific class,

This is the correct way to check all the possible validation before generating an invoice.