Add header to Html for printing to PDF

For quite a while I have been using the MBS plugin to convert my emails in html format to PDF. Then I use OpenAsCGPDFDocumentMBS to add a header and page numbers to the PDF.

There is a super weird bug that OpenAsCGPDFDocumentMBS is sometimes nil even if the PDF can be opened by double clicking it. I’ve done extensive testing this week. Sometimes I can create 10k PDFs and don’t see the error. Sometimes I see the problem after 1k emails.

I had the idea to add the idea to add the header directly to the CSS. Unfortunately, Safari/Webkit doesn’t support @page.

Does anyone have an idea what else I might try?

Xojo 2021r2.1. Different versions of macOS.