You’re trying to convert an HTML template to a PDF, but you're seeing an error like:
"templateData is not valid JSON"
This happens when the JSON formatting in your templateData is incorrect—usually due to unescaped double quotes inside strings.
Why This Happens
JSON requires that double quotes inside strings be escaped correctly. If you use double quotes within a string that's already wrapped in double quotes, it breaks the JSON structure unless they’re properly escaped.
How to Fix It
To resolve this, you’ll need to escape the internal double quotes in your templateData field. There are two ways to do it:
Option 1: Use Escape Characters
Use \\\"
to escape internal double quotes within the JSON string.
Incorrect (causes an error):
{
"templateData": "{\"note\": \"Thank you for your \"support\" of advanced robotics.\"}"
}
Correct (escape the inner quotes):
{
"templateData": "{\"note\": \"Thank you for your \\\"support\\\" of advanced robotics.\"}"
}
Option 2: Use Single Quotes and Escape Outer Double Quotes
You can also switch to single quotes for the string content and escape the outer quotes.
Also Correct:
{
"templateData": "{'note': 'Thank you for your \"support\" of advanced robotics.'}"
}
This is often easier to read and avoids needing triple backslashes.
Helpful Tips
Always validate your JSON
Be careful with nested data or special characters like \, ', or "
If your templateData is very complex, build it separately first and then paste it in