I’ve written a Laravel application that generates content for documents and inserts it into existing Word templates using PHPWord. My desired approach is pretty simple:
- I generate the content that I need with Markdown formatting
- I write that content to named slots in Word (E.g. ${slot}) using PHPWord’s Template Processing
The issue that I’m experiencing is finding reliable ways to write formatted content such as bulleted lists or applying styles that are pre-existing in the Word document. For example, a markdown section of content might be formatted like this:
# Some heading
A paragraph of text
- Bullet 1
- Bullet 2
Some more text
## A sub-heading
- Bullet 1
- Bullet 2
Some text
I don’t have an easy way of knowing what the format will be before the text is generated, but I need to now insert this into Word. I’ve seen people doing this by adding HTML to sections like here, but I don’t know how to do it using named slots, as my content needs to fit into specific places in the document.
I’ve tried using the HTML route, but to no avail (formatting not applied, issues with special characters and the Word documents not opening). I’ve tried plain text, but that obviously leaves out all my required formatting. My latest approach has been to write the actual HTML tags and then use Copilot in Word to format them, but that is tedious and not automated.
Can someone help me with either:
- A better way of telling PHPWord to apply the in-document styles (e.g Heading 1, Heading 2 etc.) to markdown text generated and inserted into an existing template.
- Or, a way of programmatically telling Copilot in Word to auto-rewrite all of the content inserted with HTML tags.