How to Convert Order to Invoice Programmatically in Magento 2?

We can convert the order to invoice using programmatically from the Order object in Magento.

You must have a valid Order Object. To Retrieve Order Object check the link, Get order data by order id programmatically in Magento

Class Magento\Sales\Model\Convert\Order have toInvoice() function that will be useful to convert the order to invoice in Magento.

By following the above approach, You can convert the order object to the invoice object.

You can use the given code snippet while you have situations to convert programmatically Order to Invoice Object.

A real example from the Core Magento Sales module, Magento\Sales\Model\Service\InvoiceService