将编码字符串转换为字节,然后编写PDF文件不起作用 - 这是什么样的编码?

发布于 2025-02-12 14:12:39 字数 883 浏览 0 评论 0 原文

编辑:这是解决的。谢谢杰里米·拉克曼(Jeremy Lakeman)的评论,这使我走上了正确的轨道。

我有一个PDF文件内容字符串,我试图用来产生PDF文件。

编辑:编码的字符串看起来像这样: image 或Google doc: https://docs.google.com/document/d/1i4utmaidyrtvcjd7005jnfp0gtoaauif0hrgbb3k6wk6w1s/edit?usp = sharing

问题的一部分当然没有得到任何编码的经验为此原谅我。

我正在尝试处理这样的文件的转换和编写:

var bytes = System.Text.Encoding.Unicode.GetBytes(FileContentString);
File.WriteAllBytes(filePath, bytes);

这确实会产生一个PDF文件,但是当我尝试打开它时,Edge PDF Viewer说:“我们无法打开此文件。”

除Unicode外,我还尝试将字符串作为UTF-8和UTF-32处理,结果多样。所有这些选项都会写入文件,但是该文件不能打开或显示空白页。

您知道这是什么样的编码吗?我肯定不会。或者,我只是在总体上处理这个错误吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2025-02-19 14:12:39

专家提示:切勿将文件的内容读为字符串,然后如果可以避免使用,则尝试将其转换回原始字符。在这种情况下,我的内容被读取了:

HttpResponseMessage response = await _httpClient.SendAsync(request);
var content = response.Content.ReadAsStringAsync();

只需将其切换到:

var content.content.content.contasbytearrayasync();

,然后:

file> file.writeallbytes(filepath,content,content);

做到了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文