Create Sales invoice in background

I want to create sales invoice in background and sync it on when i add item, i’ve a custom page like pos, i want to create sales invoice on some events and sync details from sales invoice,

example if i add item from my page, it should add in sales invoice items and it should return me the rate and discounts applied in sales invoice, so i can use them in my custom page.

just want to know that what is the best way to achieve?