Reputation: 11
EDIT: This is solved. Thank you Jeremy Lakeman for your comment, it put me on the right track.
I have a PDF file content string that I am trying to use to produce a PDF file.
EDIT: The encoded string looks like this: image or google doc: https://docs.google.com/document/d/1I4UtmAIDYRTvCjd7005jnfP0GTOAauiF0hRGB3K6w1s/edit?usp=sharing
Part of the problem is certainly that I have not dealt with any encoding so far in my experience, so please forgive me for that.
I'm trying to handle the conversion and writing of the file like this:
var bytes = System.Text.Encoding.Unicode.GetBytes(FileContentString);
File.WriteAllBytes(filePath, bytes);
This does produce a PDF file, but when I try to open it, Edge PDF Viewer says, "We can't open this file."
In addition to Unicode, I've tried handling the string as UTF-8 and UTF-32 with varied results. All of these options do write the file, but the file either does not open or displays a blank page instead.
Do you know what kind of encoding this is? I sure don't. Or, am I just handling this wrong in general?
Upvotes: 0
Views: 957
Reputation: 11
Pro tip: never read a file's contents as a string and then try to convert it back to raw bytes if you can avoid it. In this case, my contents were being read like so:
HttpResponseMessage response = await _httpClient.SendAsync(request);
var content = response.Content.ReadAsStringAsync();
Simply switching it to:
var content = response.Content.ReadAsByteArrayAsync();
And then:
File.WriteAllBytes(filePath, content);
Did the trick.
Upvotes: 1