'Python gets KeyError in put images FPDF
Anyone who can help me about FPDF error because I was trying to regenerate PDF, but got the following FPDF error:
File "/usr/local/lib/python3.5/dist-packages/fpdf/fpdf.py", line 1230, in output
self.close()
File "/usr/local/lib/python3.5/dist-packages/fpdf/fpdf.py", line 322, in close
self._enddoc() # close document
File "/usr/local/lib/python3.5/dist-packages/fpdf/fpdf.py", line 1868, in _enddoc
self._putresources()
File "/usr/local/lib/python3.5/dist-packages/fpdf/fpdf.py", line 1791, in _putresources
self._putimages()
File "/usr/local/lib/python3.5/dist-packages/fpdf/fpdf.py", line 1706, in _putimages
del info['data']
KeyError: 'data'
The template has images but I'm not sure about the issue. Please refer below.
def start(self, data, display=''):
""" Start PDF """
content = data['data']
file_name = data['file_name']
self.front_page(data)
self.render_data(content)
self.output(file_name, "F")
The issue is in this part of the pdf library:
def _putimages(self):
filter=''
if self.compress:
filter='/Filter /FlateDecode '
i = [(x[1]["i"],x[1]) for x in self.images.items()]
i.sort()
for idx,info in i:
self._putimage(info)
del info['data']
if 'smask' in info:
del info['smask']
Thanks in advance! [1]: https://i.stack.imgur.com/Nj8ar.png
Solution 1:[1]
I was facing the same issue,What helped me is to initialise pdf=FPDF() every time either inside a function or while calling recursively
Solution 2:[2]
I used FPDF2 for image processing and solved it with the way of initialization. Thanks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pawan Kumar |
| Solution 2 | Lyjen Lovendino |
