Zend certified PHP/Magento developer

OrderAfterSave send additonal emails

I’m trying to sent out addtional emails after a customer has placed an order. These additional emails will be sent from configurable email adressess and templates. It is part of a custom payment module. So far I have created my custom module and all logic is working as expected, but the email attachment is something I’m struggeling with.

The situation is that in the OrderAfterSave.php I’m trying to send the additional email what works, but I also would like to include the PDF invoice as an attachement. This is possible by using

$pdf = $this->pdfInvoice->getPdf($collection);

This is using: use MagentoSalesModelOrderPdfInvoice as PdfInvoice;

The issue I’m facing is that when I try to get the invoice PDF magento complains about an emulation nesting. After investigating I see that the Invoice.php from Magento gets the storeId and starts an emulation while we are in the checkout.

My questions is how do I get around this and get the PDF to be included in my email? Is there a different option to get the invoice PDF while in the checkout or is it simply I need to copy and rewrite a piece of the Magento 2 core and included it in my module?

Ps in my bundle I do first set the order to paid and create the invoice, so the invoice is present at the moment I try to get it.

Looking forward to input and insights.