djangofontspdf-generationpisa

Add 'comic sans ms' font in pdf using pisa library in django


i want to add "comic" font in pdf file by using Pisa in django. Pisa default font is Helvetica for text, but i want more fonts in my generated pdf, like arial,comic, symbol, verdana etc.

Please help!


Solution

  • hi i got the answer, if you are using editor, then save its out put in "contents" string. Then following code will work.

    contents = "<html><head><style type='text/css'>@font-face {font-family: comic sans ms; src: url(media/fonts/comic.ttf);}</style></head><body> " + contents + " </body></html>"
    template = Template(contents)
    
    context = Context()
    template_rendered = template.render(context)
    
    name="mypdf.pdf"
    
    import ho.pisa as pisa
    pfile = file(name, 'wb')
    pisa.CreatePDF(template_rendered.encode("UTF-8"), pfile ,encoding='UTF-8')
    pfile.close()
    

    Hopefully many others answers exist but in my case, using tinymce editor, this solution works.