Django PDF generate -> print -> redirect

Hi Everyone,

I want my visitors when arrive on site to sign-in with their details then a visitor pass printed from a label printer. I am stuck at generate pdf; even though it is able to generate pdf with visitor name.

My issue is after pdf is generated it does not redirect to a different page. My understanding is you do 1 request and browser returns 1 response. Is there any work around?

I am also looking for away to print visitor pass as soon as the form.is_valid() is true.

Thanks for your help.
Views.py

def visitor_vic_signin(request):
    if request.method == "POST":
        form = VisitorFormVic(request.POST)
        if form.is_valid():
            form.save()
            #print visitor tag
            visitorData = form.cleaned_data
            visitorName = visitorData['name']
            #Create Byrtestream buffer
            buf = io.BytesIO()
            #Create a canvas
            c = canvas.Canvas(buf, pagesize = letter, bottomup=0)
            #create a text object
            textob = c.beginText()
            textob.setTextOrigin(inch, inch)
            textob.setFont("Helvetica",14)
            
            #add some lines of text
            lines = [visitorName]
            
            for line in lines:
                textob.textLine(line)
               
            #finished up
            c.drawText(textob)
            c.showPage()
            c.save()
            buf.seek(0)
            
            return FileResponse(buf, as_attachment=True, filename='something.pdf')    
            return redirect(visitor_vic_menu)
            
    else:
        form = VisitorFormVic()
        context={'form': form}
    return render(request, "visitor_app/visitor_vic_signin.html",context)

Correct.

The first return statement is executed - the flow of control leaves the function at that point, and the second return statement is never seen.

That is also correct. That’s fundamentally how the HTTP protocol works.

I’m not following what you’re trying to ask for here.

Are you printing the file on the server? Or from the browser?
The server has no control over what happens in the browser - all it can do is send data to the browser. What the browser does with that data is not something the server can control.

You can probably write some JavaScript that is sent to the browser that opens the print dialog, but I’m not sure you can do much more than that.

Hi Ken,

My breakdown is following:
As my visitor detail is saved to database as below code:

def visitor_vic_signin(request):
    if request.method == "POST":
        form = VisitorFormVic(request.POST)
        if form.is_valid():
            form.save()   
            return redirect(visitor_vic_menu)      
    else:
        form = VisitorFormVic()
        context={'form': form}
    return render(request, "visitor_app/visitor_vic_signin.html",context)

after saving their details, a label is printed with their details. I am thinking using reportlab to generate a pdf filled with visitor’s details; however, I am not sure how to tell django to do this (data is saved → pdf generated → print label)

At this stage, I am able to saved visitor’s details to database. How do I send the next request to generate pdf?

My views.py for pdf generate as below:

def print_visitor_tag(request,name):
    #Create Byrtestream buffer
	buf = io.BytesIO()
	#Create a canvas
	c = canvas.Canvas(buf, pagesize = letter, bottomup=0)
	#create a text object
	textob = c.beginText()
	textob.setTextOrigin(inch, inch)
	textob.setFont("Helvetica",14)
	#add some lines of text
	lines = [name]
	
	for line in lines:
		textob.textLine(line) 
		
	#finished up
	c.drawText(textob)
	c.showPage()
	c.save()
	buf.seek(0)
	
	return FileResponse(buf, as_attachment=True, filename='something.pdf')

after pdf is generated, another request should to print to a label printer.

My question is how to do 3 requests, one after another with django.