Apps Script

See if this can help you:

Or mine tutorial to create a PDF document using external services: