0

I would like download a number of pdfs which correspond to len(list_element). I create pdf with one list (I use 2 elements on this list to "write" a pdf) but I can download the first pdf, not others. Thanks for your help.

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
Nosmoz RG
  • 15
  • 3

1 Answers1

-1

In your last while loop you do a return response. This will happen on the first iteration. So you just return one pdf response. The rest is never processed.

But you can't return multiple documents in one response anyway. To do that you could for eg. create a zip file containing multiple files.

Those while loops in your code should be replace by shorter, better readable and less error prone for loops.

tobltobs
  • 2,782
  • 1
  • 27
  • 33