Somewhere in that mess, the non-breaking spaces from the HTML template (the s) are encoding as ISO-8859-1 so that they show up incorrectly as an “Â” character
That’d be encoding to UTF-8 then, not ISO-8859-1. The non-breaking space character is byte 0xA0 in ISO-8859-1; when encoded to UTF-8 it’d be 0xC2,0xA0, which, if you (incorrectly) view it as ISO-8859-1 comes out as
"Â ". That includes a trailing nbsp which you might not be noticing; if that byte isn’t there, then something else has mauled your document and we need to see further up to find out what.
What’s the regexp, how does the templating work? There would seem to be a proper HTML parser involved somewhere if your
strings are (correctly) being turned into U+00A0 NON-BREAKING SPACE characters. If so, you could just process your template natively in the DOM, and ask it to serialise using the ASCII encoding to keep non-ASCII characters as character references. That would also stop you having to do regex post-processing on the HTML itself, which is always a highly dodgy business.
Well anyway, for now you can add one of the following to your document’s
<head> and see if that makes it look right in the browser:
- for HTML4:
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
- for HTML5:
If you’ve done that, then any remaining problem is ActivePDF’s fault.