简体   繁体   English

Django + Postgres:将带有 NUL (0x00) 字符的 PDF 文件保存到 DB 并将其解码回 PDF

[英]Django + Postgres: saving PDF file with NUL (0x00) characters to DB and decoding it back to PDF

I have a pdf file that I want to save in my Postgres DB我有一个 pdf 文件要保存在我的 Postgres 数据库中

PDF文件

When I tried to save the file it brings A string literal cannot contain NUL (0x00) characters.当我尝试保存文件时,它带来了A string literal cannot contain NUL (0x00) characters. so I followed the solution from here which replaces the null with a character所以我遵循了here的解决方案,用 字符替换了 null

unicode(ppbData[0], errors='ignore').replace("\\x00", "\�")

The problem is that I can't convert it back to PDF now.问题是我现在无法将其转换回 PDF。 I tried encode() and other methods我试过encode()和其他方法

file = open('new.pdf', 'wb')
file.write(text.encode())
file.close()

but it returns blank pdf但它返回空白的pdf

空白 PDF 文件

Is there any way to replaces the character with a null or any other way to convert it back to normal pdf?有什么方法可以用 null 替换 字符或以任何其他方式将其转换回普通 pdf? Maybe the first solution with replacing also wasn't right and there is another way?也许替换的第一个解决方案也不正确,还有另一种方法吗?

I have a pdf file that I want to save in my Postgres DB我有一个 pdf 文件要保存在我的 Postgres 数据库中

PDF文件

When I tried to save the file it brings A string literal cannot contain NUL (0x00) characters.当我尝试保存文件时,它带来了A string literal cannot contain NUL (0x00) characters. so I followed the solution from here which replaces the null with a character所以我遵循了here的解决方案,用 字符替换了 null

unicode(ppbData[0], errors='ignore').replace("\\x00", "\�")

The problem is that I can't convert it back to PDF now.问题是我现在无法将其转换回 PDF。 I tried encode() and other methods我试过encode()和其他方法

file = open('new.pdf', 'wb')
file.write(text.encode())
file.close()

but it returns blank pdf但它返回空白的pdf

空白 PDF 文件

Is there any way to replaces the character with a null or any other way to convert it back to normal pdf?有什么方法可以用 null 替换 字符或以任何其他方式将其转换回普通 pdf? Maybe the first solution with replacing also wasn't right and there is another way?也许替换的第一个解决方案也不正确,还有另一种方法吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM