Hello I installed the fpdf2 library.
I created a function to do some testing that is called from a path.
Using the library example
from fpdf import FPDF
def StampaIscr(request, pk):
# Si posiziona sull'Associato
associato = get_object_or_404(Associati, pk=pk)
pdf = FPDF('P', 'mm', 'Letter')
pdf.add_page()
pdf.set_font('helvetica', '', size=12)
pdf.cell(40,10, "hello world")
pdf.output("hello_world.pdf") returns the error that ik response or httpresponse is missing I tried inserting these lines
return HttpResponse
return response
But it returns other errors.
Maybe it cannot be used in a function ?
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt="Benvenuto nel tuo PDF!", ln=True, align='C')
# Finalizza il documento e lo invia al browser
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename="mio_documento.pdf"'
pdf.output(name=response, dest='S')
return response
it creates the pdf file for me but displays an empty file
There is some other thing that is not clear to me
First, you’re responding with binary data - this means you’ll want to use either a StreamingHttpResponse or a FileResponse. Then, you need to create a binary I/O object containing the PDF data generated by the output function.
This means that your response needs to look something like this:
output_pdf = pdf.output()
buffer = io.BytesIO(output_pdf)
return FileResponse(
buffer, as_attachment=True, # or False, depending upon the desired behavior
filename='mio_documento.pdf', content_type='application/pdf'
)
From the link you pointed me to I read the example and changed the library, using reportLab
I am trying to use a font that is probably not installed on the pc
The code in the example is this
import reportlab.rl_config
reportlab.rl_config.warnOnMissingFontGlyphs = 0
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFonts
pdfmetrics.registerFont(TTFont('Vera', 'Vera.ttf'))
pdfmetrics.registerFont(TTFont('VeraBd', 'VeraBd.ttf'))
pdfmetrics.registerFont(TTFont('VeraIt', 'VeraIt.ttf'))
pdfmetrics.registerFont(TTFont('VeraBI', 'VeraBI.ttf'))
canvas.setFont('Vera', 32)
canvas.drawString(10, 150, “Some text encoded in UTF-8”)
canvas.drawString(10, 100, “In the Vera TT Font!”)
The code used to set the Hummingbird font is this:
Ok, I’m not sure why you changed libraries, but that’s your choice.
However, if you’re looking for assistance with this, please post the complete view and provide a more detailed explanation about the current issue being faced other than “it doesn’t work”.
Yes it was my choice as the example on django project was on and not fodf2.
For me one or the other is indifferent, the important thing is that I can achieve what I need.
I’ll post the code, what I can’t achieve is the Calibri font.
The code is in a view
This is the wording I see in the terminal
Calibri.TTF is missing
On googlefonts it is not presnete
I did a test by downloading Roboto from googlefonts, for dlele testing, but I have a doubt, after downoLoad there are several .ttf files for normal, bold, italic and other font. Does this mean that I have to register all the ttf files to then be able to use them ?
Does registering the font take up space or slow down the process ?
Is it possible to insert a link in the head, or is there another method to use ?
I had experience with reportLab but it doesn’t allow various things and I went back to fpdf2. Now maybe I understand what you wanted to tell me about ‘…your choice…’