有没有办法使用 FPDF 从 Django 中的表单生成 PDF 文件?
Is there a way of using FPDF to generate a PDF file from forms in Django?
我正在尝试生成一个 PDF 文件并使用 Django 4.0.2 为其指定一个自定义名称我有 2 个输入,一个用于名称,一个用于照片,我希望能够在不保存的情况下生成 PDF它在我的数据库中,但 return 它返回给用户。我没有为此使用任何模型,普通 html 和 python。
我有输入:
<input
type="text"
id="inputPassword6"
class="form-control"
aria-describedby="passwordHelpInline"
name="name"
/>
<input
class="form-control"
type="file"
id="formFileMultiple"
multiple
accept=".png, .jpeg, .jpg"
name="photos"
/>
<div class="d-grid gap-2 mt-3">
<button class="btn btn-success" type="submit">Save PDF</button>
</div>
我正在尝试合并 return PDF 文件,如下所示:
if request.method == "POST" or "FILES":
name = request.POST["name"]
photos = request.FILES["photos"]
# Convert photos to PDF
pdf = FPDF()
# imagelist is the list with all image filenames
for image in photos:
pdf.add_page()
pdf.image(image, 0,0,210,297)
pdf.output(f"{name} AIA 114.pdf", "F")
当前错误:
Exception Type: TypeError
Exception Value:
argument should be integer or bytes-like object, not 'str'
FPDF 似乎无法查看我提供的图像。我试图解码图像,但我遇到了另一个错误:
'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
有人可以帮我解决这个问题或提出另一种解决方法吗?
下面是不使用模型对我有用的解决方案,但是这是硬编码的,因为我后来发现,我可以使用 django 中内置的 default_storage
和 ContentFile
,因为文件已在内存中 (InMemoryFile
)。
def index(request):
if request.method == "GET":
return render(request, 'index.html')
if request.method == "POST" or "FILES":
try:
# Get data from inputs
name = request.POST["name"]
photos = request.FILES.getlist('photos')
count = 1
if (len(photos) > 1):
# For multiple photos
imglist = []
counter = 0
# Write buffers and append to the list
for photo in photos:
bytes = photo.read() # Read files in memory
path = django_settings.MEDIA_ROOT + f"buffer{counter}.png" # auto path
counter = counter + 1 # counter
file = open(path, 'wb')
file.write(bytes)
file.close()
imglist.append(file)
# CONVERT
listconv = []
counter = 0
for img in imglist:
path = django_settings.MEDIA_ROOT + f"buffer{counter}.png"
img = Image.open(path)
img = img.convert('RGB')
listconv.append(img)
counter=counter+1
img.save(django_settings.MEDIA_ROOT + f"{name}.pdf", save_all=True, append_images=listconv[:-1])
# Return response
img = open(django_settings.MEDIA_ROOT + f"{name}.pdf", 'rb')
response = FileResponse(img)
return response
elif (len(photos) == 1):
# For single photo
photos = request.FILES["photos"]
# WRITE FILE
bytes = photos.read() # CONTENT UPLOADED FILE
path = django_settings.MEDIA_ROOT + "buffer"
file=open(path,'wb')
file.write(bytes)
file.close()
# CONVERT
image1 = Image.open(path)
im1 = image1.convert('RGB')
im1.save(django_settings.MEDIA_ROOT + f"{name}.pdf")
img = open(django_settings.MEDIA_ROOT + f"{name}.pdf", 'rb')
# Return response
response = FileResponse(img)
return response
else:
print("ERROR")
except UnidentifiedImageError:
return render(request, 'error.html')
return render(request, 'index.html')
我正在尝试生成一个 PDF 文件并使用 Django 4.0.2 为其指定一个自定义名称我有 2 个输入,一个用于名称,一个用于照片,我希望能够在不保存的情况下生成 PDF它在我的数据库中,但 return 它返回给用户。我没有为此使用任何模型,普通 html 和 python。 我有输入:
<input
type="text"
id="inputPassword6"
class="form-control"
aria-describedby="passwordHelpInline"
name="name"
/>
<input
class="form-control"
type="file"
id="formFileMultiple"
multiple
accept=".png, .jpeg, .jpg"
name="photos"
/>
<div class="d-grid gap-2 mt-3">
<button class="btn btn-success" type="submit">Save PDF</button>
</div>
我正在尝试合并 return PDF 文件,如下所示:
if request.method == "POST" or "FILES":
name = request.POST["name"]
photos = request.FILES["photos"]
# Convert photos to PDF
pdf = FPDF()
# imagelist is the list with all image filenames
for image in photos:
pdf.add_page()
pdf.image(image, 0,0,210,297)
pdf.output(f"{name} AIA 114.pdf", "F")
当前错误:
Exception Type: TypeError
Exception Value:
argument should be integer or bytes-like object, not 'str'
FPDF 似乎无法查看我提供的图像。我试图解码图像,但我遇到了另一个错误:
'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
有人可以帮我解决这个问题或提出另一种解决方法吗?
下面是不使用模型对我有用的解决方案,但是这是硬编码的,因为我后来发现,我可以使用 django 中内置的 default_storage
和 ContentFile
,因为文件已在内存中 (InMemoryFile
)。
def index(request):
if request.method == "GET":
return render(request, 'index.html')
if request.method == "POST" or "FILES":
try:
# Get data from inputs
name = request.POST["name"]
photos = request.FILES.getlist('photos')
count = 1
if (len(photos) > 1):
# For multiple photos
imglist = []
counter = 0
# Write buffers and append to the list
for photo in photos:
bytes = photo.read() # Read files in memory
path = django_settings.MEDIA_ROOT + f"buffer{counter}.png" # auto path
counter = counter + 1 # counter
file = open(path, 'wb')
file.write(bytes)
file.close()
imglist.append(file)
# CONVERT
listconv = []
counter = 0
for img in imglist:
path = django_settings.MEDIA_ROOT + f"buffer{counter}.png"
img = Image.open(path)
img = img.convert('RGB')
listconv.append(img)
counter=counter+1
img.save(django_settings.MEDIA_ROOT + f"{name}.pdf", save_all=True, append_images=listconv[:-1])
# Return response
img = open(django_settings.MEDIA_ROOT + f"{name}.pdf", 'rb')
response = FileResponse(img)
return response
elif (len(photos) == 1):
# For single photo
photos = request.FILES["photos"]
# WRITE FILE
bytes = photos.read() # CONTENT UPLOADED FILE
path = django_settings.MEDIA_ROOT + "buffer"
file=open(path,'wb')
file.write(bytes)
file.close()
# CONVERT
image1 = Image.open(path)
im1 = image1.convert('RGB')
im1.save(django_settings.MEDIA_ROOT + f"{name}.pdf")
img = open(django_settings.MEDIA_ROOT + f"{name}.pdf", 'rb')
# Return response
response = FileResponse(img)
return response
else:
print("ERROR")
except UnidentifiedImageError:
return render(request, 'error.html')
return render(request, 'index.html')