Python 如何将io.BytesIO pdfrw PDF保存到Django文件字段
我想做的基本上是:Python 如何将io.BytesIO pdfrw PDF保存到Django文件字段,python,django,pdf,bytesio,pdfrw,Python,Django,Pdf,Bytesio,Pdfrw,我想做的基本上是: 从URL获取PDF 通过pdfrw修改它 将其存储在内存中作为 拜特西奥公司 通过将其上载到Django文件字段 Model.objects.create(form=pdf\u file,name=“Some name”) 我的问题是当create()方法运行时,它会保存除表单之外的所有字段 helpers.py import io import tempfile from contextlib import contextmanager import requests i
Model.objects.create(form=pdf\u file,name=“Some name”)
create()
方法运行时,它会保存除表单
之外的所有字段
helpers.py
import io
import tempfile
from contextlib import contextmanager
import requests
import pdfrw
@contextmanager
def as_file(url):
with tempfile.NamedTemporaryFile(suffix='.pdf') as tfile:
tfile.write(requests.get(url).content)
tfile.flush()
yield tfile.name
def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict):
template_pdf = pdfrw.PdfReader(input_pdf_path)
## PDF is modified here
buf = io.BytesIO()
print(buf.getbuffer().nbytes). # Prints "0"!
pdfrw.PdfWriter().write(buf, template_pdf)
buf.seek(0)
return buf
from django.core.files import File
class FormView(View):
def get(self, request, *args, **kwargs):
form_url = 'http://some-pdf-url.com'
with as_file(form_url) as temp_form_path:
submitted_form = write_fillable_pdf(temp_form_path, temp_form_path, {"name": "John Doe"})
print(submitted_form.getbuffer().nbytes). # Prints "994782"!
FilledPDF.objects.create(form=File(submitted_form), name="Test PDF")
return render(request, 'index.html', {})
视图.py
import io
import tempfile
from contextlib import contextmanager
import requests
import pdfrw
@contextmanager
def as_file(url):
with tempfile.NamedTemporaryFile(suffix='.pdf') as tfile:
tfile.write(requests.get(url).content)
tfile.flush()
yield tfile.name
def write_fillable_pdf(input_pdf_path, output_pdf_path, data_dict):
template_pdf = pdfrw.PdfReader(input_pdf_path)
## PDF is modified here
buf = io.BytesIO()
print(buf.getbuffer().nbytes). # Prints "0"!
pdfrw.PdfWriter().write(buf, template_pdf)
buf.seek(0)
return buf
from django.core.files import File
class FormView(View):
def get(self, request, *args, **kwargs):
form_url = 'http://some-pdf-url.com'
with as_file(form_url) as temp_form_path:
submitted_form = write_fillable_pdf(temp_form_path, temp_form_path, {"name": "John Doe"})
print(submitted_form.getbuffer().nbytes). # Prints "994782"!
FilledPDF.objects.create(form=File(submitted_form), name="Test PDF")
return render(request, 'index.html', {})
正如您所看到的,
print()
在填充BytesIO时会给出两个不同的值,这让我相信大小的增加意味着其中实际上有数据。它没有正确保存到django模型实例中有什么原因吗?另外,如果有人知道更有效的方法,请告诉我 以下是有关的文档
您可以在代码中使用
ContentFile
类。我在您的视图中做了相应的修改,将您的文件保存在filefield中
from django.core.files.base import ContentFile
class FormView(View):
def get(self, request, *args, **kwargs):
form_url = 'http://some-pdf-url.com'
with as_file(form_url) as temp_form_path:
submitted_form = write_fillable_pdf(temp_form_path, temp_form_path, {"name": "John Doe"})
pdf_content = ContentFile(submitted_form.getvalue(), 'sample.pdf')
FilledPDF.objects.create(form=pdf_content, name="Test PDF")
return render(request, 'index.html', {})
您还可以使用save
方法使用ContentFile
类存储文件
from django.core.files.base import ContentFile
class FormView(View):
def get(self, request, *args, **kwargs):
form_url = 'http://some-pdf-url.com'
with as_file(form_url) as temp_form_path:
submitted_form = write_fillable_pdf(temp_form_path, temp_form_path, {"name": "John Doe"})
pdf_content = ContentFile(submitted_form.getvalue())
filled_pdf = FilledPDF()
filled_pdf.name = "Test PDF"
filled_pdf.form.save("sample.pdf", pdf_content, save=False)
filled_pdf.save()
return render(request, 'index.html', {})
不幸的是,这不起作用:(我的模型实例保存了,但是“表单”字段显示没有上传files@Hybrid,忘记在
ContentFile
实例中添加文件名。请更新它,并添加另一种存储文件的方法。这将起作用。:)谢谢!由于我忘记了文件名,文件似乎没有以正常方式保存。