Friday, December 4, 2009

Django Pisa (polskie czcionki)

I use Pisa to generate reports. I had problem with polish lettersm solution is to replace included fonts (arial, helvetica) with own.
Copy fonts to /project/directory/site_media/font.

#-*- coding: utf-8 -*- 
import cStringIO as StringIO

import ho.pisa as pisa

from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.template import Context
from django.template.loader import get_template
from django.utils.translation import ugettext_lazy as _

from arap.report.models import Review
from arap.report.models import Type
from arap.settings import  MEDIA_ROOT

@login_required
def print_report(request, id):
    """
    Generuje raport z przeglądu.
    """
    template = get_template('report/print.html')
    review = get_object_or_404(Review, id=id)
    context = Context({'pagesize':'A4', 'review':review, 'MEDIA_ROOT':MEDIA_ROOT})
    html  = template.render(context)
    result = StringIO.StringIO()
    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode('UTF-8')), result)
    if not pdf.err:
        response = HttpResponse( result.getvalue() )
        
        response['Content-Type'] = 'application/pdf'
        response['Content-Disposition'] = 'attachment; filename="%s-%s-%s.pdf";' \
            %(review.server,review.report_type, review.created)
        return response
    return HttpResponse(u"Coś nie bangla.", mimetype='text/html')

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<style type="text/css"> 
@font-face {
   font-family: "arial","helvetica","symbol";
   src: url({{ MEDIA_ROOT}}/font/arial.ttf);
}

@font-face {
   font-family: "arial black";
   src: url({{ MEDIA_ROOT}}/font/arialbd.ttf);
}

@font-face {
   font-family: "verdana";
   src: url({{ MEDIA_ROOT}}/font/verdana.ttf);
} 


@font-face {
   font-family: "Times New Roman" ,"wingdings";
   src: url({{ MEDIA_ROOT}}/font/Times_New_Roman.ttf);
}

@font-face {
   font-family: "Comic Sans Ms";
   src: url({{ MEDIA_ROOT}}/font/Comic_Sans_MS.ttf);
}

@page {
  margin: 1cm;
  margin-bottom: 2.5cm;
  @frame footer {
    -pdf-frame-content: footerContent;
    bottom: 2cm;
    margin-left: 1cm;
    margin-right: 1cm;
    height: 1cm;
  }
} 

html, body, table, caption, tbody, tfoot, thead, tr, th, td {
    font-family: "Times New Roman";
}
</style>

<title>Raport {{ review.report_type }} {{ review.server }} {{ review.created }}</title>
</head>
<body> 
  {{ review.report|safe }} 
  <div id="footerContent">
    {%block pager %}
      <pdf:pagenumber>
    {%endblock%}
  </div>
</body>
</html>

8 comments:

  1. Hi,

    I'm hungarian and I have the same problem with hungarian fonts like Őűőí and so on. I have tried your solution but it still doesn't work.

    I generate the pdf text like this:

    pdf = pisa.pisaDocument(StringIO.StringIO(
    html.encode("UTF-8")), result, link_callback=fetch_resources,
    encoding="utf8", xhtml=True)

    but also tried the way as you wrote:

    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode('UTF-8')), result)

    I have also inserted the following code to my template:

    @font-face {
    font-family: "arial";
    src: url("http://www.toolpart.hu/static/media/medialibrary/2010/04/arial.ttf");
    }

    html {
    font-family: "arial";
    }

    Do you have an idea what else can I try?

    Thanks for your help in advance.

    Laszlo

    ReplyDelete
  2. Put fonts on disk instead off http:// ...

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete