简体   繁体   中英

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文件

When I tried to save the file it brings A string literal cannot contain NUL (0x00) characters. so I followed the solution from here which replaces the null with a character

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

The problem is that I can't convert it back to PDF now. I tried encode() and other methods

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

but it returns blank pdf

空白 PDF 文件

Is there any way to replaces the character with a null or any other way to convert it back to normal 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文件

When I tried to save the file it brings A string literal cannot contain NUL (0x00) characters. so I followed the solution from here which replaces the null with a character

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

The problem is that I can't convert it back to PDF now. I tried encode() and other methods

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

but it returns blank pdf

空白 PDF 文件

Is there any way to replaces the character with a null or any other way to convert it back to normal pdf? Maybe the first solution with replacing also wasn't right and there is another way?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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