0
我想下載一些pdfs,對應於len(list_element)
。我用一個列表創建PDF(我使用這個列表中的2個元素來「寫」pdf),但我可以下載第一個pdf,而不是其他列表。謝謝你的幫助。Django - 多PDFTemplateResponse(wkhtmltopdf)
class CreatePDFCSVView(PDFTemplateView):
"""View to create PDF."""
context = {}
def get(self, request, *args, **kwargs):
"""Create the PDF for the model."""
self.context['object'] = get_object_or_404(LabelModel, pk=kwargs['pk'])
object_label = self.context['object'] or False
list_element_csv = []
list_element = []
with io.open("media/list_file", "r") as infile:
str_path = infile.read()
csvname = open(str_path, 'rb')
reader = csv.reader(codecs.iterdecode(csvname, 'utf-8'), delimiter=";")
for row in reader:
list_element_csv.append(row)
i=0
while i < len(list_element_csv):
if i==0:
header = list_element_csv[i]
i+=1
y=0
while y < len(list_element_csv[i]):
list_element.append(list_element_csv[i][y])
y+=1
i+=1
imgdatabase = Image.objects.all()
while len(list_element) != 0 :
list_csv_pdf = []
list_csv_pdf.append(list_element[0])
list_csv_pdf.append(list_element[1])
filename = 'model.pdf'
cmd_options = {
'page-height' : object_label.format_label.height,
'page-width' : object_label.format_label.width,
'margin-top' : object_label.format_label.margin_top,
'margin-left' : object_label.format_label.margin_left,
'margin-right' : object_label.format_label.margin_right,
'margin-bottom' : object_label.format_label.margin_bottom
}
if object_label:
filename = '%s_%s.pdf' %(object_label.labelModel_name, len(list_element))
template_name = object_label.template_model.path
self.context['object'] = object_label
self.context['list_csv_pdf'] = list_csv_pdf
for queryset in imgdatabase:
str_img = "%s_%s" %('Image', queryset.name_img)
absolute_path = os.path.join(PATH_IMAGE_HTML, queryset.link_img.url)
self.context[str_img] = absolute_path
response = PDFTemplateResponse(
request = request,
template = template_name,
filename = filename,
context = self.context,
show_content_in_browser = False,
cmd_options = cmd_options
)
list_element.pop(0)
list_element.pop(0)
return response