"Why is that?" Because spaces in text may be replaced with moving the cursor in a PDF. It can be more efficient, and it certainly is a requirement with justified text.
(Your code is not justified – but why create separate output routines for that when you can also use a single routine?)
So the PDF does not contain "one or two or four spaces", it contains text with a smaller or larger move inside. Spaces at the start are simply ignored; because you have to supply a starting point for the text anyway, why not start with the first visible character? It's all about efficiency, and originally the PDF format was designed to correctly represent a design on screen and in print.
That said: there are 2 options that may help you, although InDesign does not allow immediate access to these. A PDF may contain an "ActualText" entry for any text fragment, which should be the preferred contents when copying (with the right software – your intended browser usage is actually a bad omen, and Acrobat Pro may possibly perform better even with your current file).
The other option is kind of a guess. What will InDesign do when the space character is actually visible? It should then be required to output that literal character! Normally I would suggest to just try it with my very own IndyFont 🙂 as all it needs is a font with a small dot or dash in the space character – but, off the top of my head, I think IF ignores a custom space because it ought to be a rare use case, and the chances of messing up space handling would be too large.
Still: it might be worth locating a visible-space font elsewhere and just testing to see what happens.
... View more