I am getting the following error when sending a PDF from my vue.js frontend to my Flask backend:
ValueError: embedded null byte
I am adding the file with b-form-file. When I print the file in the console I can see that it's there. I'm sending it with axios:
extractPayslipData () {
console.log('Extracting payslip data...')
const path = `http://localhost:5000/api/extractPayslipData`
let formData = new FormData()
formData.append('payslip_file', this.payslip_file)
formData.append('payslip_type', this.payslip_type)
var self = this
Axios.post(path, formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
}).then(function (response) {
var extracted_form = response.data['extracted_form']
}).catch(function (error) {
console.log(error)
})
return
}
I can also see it in my python code that receives the request (pdfObject is a FileStorage Object:
<FileStorage: 'Payslip_to_Print_03_20_2020.pdf' ('application/pdf')>
The error hits when I try to open the file that has passed through. The frontend calls extract_payslip_data() where I get the PDF file (I think it's in bytes). I think call payslip_decoder() which is where the error happens:
def extract_payslip_data():
pdfObject = dict(request.files)
payslip_file = pdfObject['payslip_file'].read()
extracted_form = payslip_decoder(payslip_file)
response = {
'extracted_form': extracted_form
}
return jsonify(response)
def payslip_decoder(payslip_file):
# creating a pdf file object
pdfFileObj = open(payslip_file, 'rb')
Here is an example of what shows when I print the file (random excerpt of a long output):
\xb4|\xca\x04\xa2Ty\xbbE<\x81D\x90%b\x08D\xff\xa9\x89\xff0\xecO\x9a\x99k\xb9\xa8\x8d\x1f\x01-\xd1\x06\xa8\\\xa6\x01\xe4\xe7~\x80\xa2\x12\x01\x92\xb0[\xbe\x02\xfd\xde\xb7`|4P\xdc\xbc\x18\xfd\xd1\x99\xb9\xff,\xe8\xdfw\x85\xcb\x14\x8f\\A\xea\xe78vD$\x83+\x15\xe7\xcd\xac)\xae%@\x03\x02P\x064\xa0\t\xf4\x80\x110\x07L\xe0\x00\x9c\x81\x1b\xf0\x04\xbe`\x1e\x08\x05\x91 \x0e,\x06\\\x90\x06\x84@\x0c\xf2\xc12\xb0\x1a\x14\x83R\xb0\x05\xec\x00\xd5\xa0\x0e4\x82f\xd0\n\x0e\x83Np\x0c\x9c\x06\xe7\xc0%p\x05\xdc\x00\xf7\x80\x0c\x8c\x80\xa7`\x12\xbc\x02\xd3\x10\x04a!2D\x854!}\xc8\x04\xb2\x82\x1c \x164\x17\xf2\x85\x82\xa1\x08(\x0eJ\x82R!\x11$\x85\x96Ak\xa1R\xa8\x1c\xaa\x86\xea\xa1f\xe8[\xe8(t\x1a\xba\x00\rBw\xa0!h\x1c\xfa\x15z\x07#0\t\xa6\xc1\xba\xb0)l\x0b\xb3`/8\x08\x8e\x84\x17\xc1\xa9p\x0e\xbc\x04.\x827\xc3\x95p\x03|\x10\xee\x80O\xc3\x97\xe0\x1b\xb0\x0c~\nO!\x00!"t\xc4\x00a",\x84\x8d\x84"\xf1H\n"FV %H\x05\xd2\x80\xb4"\xddH\x1fr\r\x91!\x13\xc8[\x14\x06EE1PL\x94\x1b*\x00\x15\x85\xe2\xa2rP+P\x9bP\xd5\xa8\xfd\xa8\x0eT/\xea\x1aj\x085\x89\xfa\x88&\xa3u\xd0VhWt :\x16\x9d\x8a\xceG\x17\xa3+\xd0M\xe8v\xf4Y\xf4\r\xf4\x08\xfa\x15\x06\x83\xa1c\xcc0\xce\x98\x00L\x1c&\x1d\xb3\x14\xb3\t\xb3\x0b\xd3\x869\x85\x19\xc4\x0cc\xa6\xb0X\xac&\xd6\n\xeb\x8e\r\xc5r\xb0\x12l1\xb6\n{\x10{\x12{\x15;\x82}\x83#\xe2\xf4q\x0e8?\\<N\x84[\x83\xab\xc0\x1d\xc0\x9d\xc0]\xc5\x8d\xe2\xa6\xf1*x\x13\xbc+>\x14\xcf\xc3\x17\xe2\xcb\xf0\x8d\xf8n\xfce\xfc\x08~\x9a\xa0J0#\xb8\x13"\t\xe9\x84\xd5\x84JB+\xe1,\xe1>\xe1\x05\x91H4$\xba\x10\xc3\x89\x02\xe2*b%\xf1\x10\xf1<q\x88\xf8\x96D!Y\x92\xd8\xa4\x04\x92\x94\xb4\x99\xb4\x8ft\x8at\x87\xf4\x82L&\x9b\x92=\xc9\xf1d\ty3\xb9\x99|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