Hello I'm trying to view pdf on browser new tab via FPDF. getting error
here my code.
class PDFWithHeaderFooter(FPDF):
def header(self):
# Add your header content here
self.set_font('Arial', 'B', 12)
self.cell(0, 10, 'Your Header Text', 0, 1, 'C')
def footer(self):
# Add your footer content here
self.set_y(-15)
self.set_font('Arial', 'I', 8)
self.cell(0, 10, 'Page %s' % self.page_no(), 0, 0, 'C')
@login_required(login_url='/')
def Print_Quote(request, pk):
id = Quote.objects.get(id = pk)
term = id.terms.all()
quote_item = Quote_item.objects.filter( quotes = id )
sub_total = quote_item.aggregate(Sum('sub_total'))
gst_total = quote_item.aggregate(Sum('gst_total'))
total = quote_item.aggregate(Sum('total'))
pdf = FPDF(format='letter')
pdf = PDFWithHeaderFooter()
# Add content to the PDF
pdf.add_page()
pdf.set_font('Arial', 'B', 16)
pdf.cell(40, 10, 'Hello, World!')
# Save the PDF to a HttpResponse
pdf.output()
return HttpResponse(pdf, content_type='application/pdf')
I'm view pdf on new tab but it's show me error.
Unfortunately, we do not know what your error is. But try to return the pdf file via binary stream (irrelevant part skipped):
import io
from django.http import FileResponse
...
pdf.set_font('Arial', 'B', 16)
pdf.cell(40, 10, 'Hello, World!')
file_stream = io.BytesIO(pdf.output())
return FileResponse(file_stream, as_attachment=False, filename="test.pdf")