Use a summary script based on the line items table. Use a find or go to related records to pull up a found set of the line items for one invoice. Use a layout similar to that found in your starter solution for printing an invoice but replace the body layout part with a sub summary layout part "when sorted by" your product ID field. Replace quantity fields with a summary field that computes the total of your quantity field and place it in this sub summary part along with the product ID and description fields.
If you include a summary field that computes the cost of each group of line items, you may want to then use this layout for printing your invoices.