K
K
Konstantin Malyarov2019-01-27 15:34:47
Django
Konstantin Malyarov, 2019-01-27 15:34:47

How to import TFF font into reportlab?

Задал свой TFF стиль шрифта:
# Create the HttpResponse object with the appropriate PDF headers.
        response = HttpResponse(content_type='application/pdf')
        response['Content-Disposition'] = 'inline; filename="somefilename.pdf"'

        # Create the PDF object, using the response object as its "file."
        doc = SimpleDocTemplate(response,
                                pagesize=A5,
                                leftMargin=5,
                                rightMargin=5,
                                topMargin=5,
                                bottomMargin=5)
        styles = getSampleStyleSheet()
        pdfmetrics.registerFont(TTFont('Times', str(settings.BASE_DIR) + '/pdf/static/pdf/TIMES.TTF', 'UTF-8'))
        styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY, fontName='Times', fontSize=10))
        styles.add(ParagraphStyle(name='Justify-Bold', alignment=TA_JUSTIFY, fontName='Times-Bold'))
        flowables = []
        text = "Приложение 5" \
               "\n к Приказу" \
               "\n Министерства здравоохранения" \
               "\n и социального развития" \
               "\n Российской Федерации" \
               "\n от 22.11.2004 N 255"
        para = Paragraph(text.encode('utf-8'), style=styles["Justify"])

but on the line
pdfmetrics.registerFont(TTFont('Times', str(settings.BASE_DIR) + '/pdf/static/pdf/TIMES.TTF', 'UTF-8'))
gives an error
TTF file "/home/pi/mis_asp/mis_asp/pdf/static/pdf/TIMES.TTF": invalid checksum 0XFEDD8420 (expected 0xB1B0AFBA) len: 82010 &3: 2

How to import TFF style correctly?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question