Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

# -*- coding: UTF-8 -*- 

# Copyright 2016 Luc Saffre 

# License: BSD (see file COPYING for details) 

"""Choicelists for `lino.modlib.wkhtmltopdf`. 

""" 

 

from __future__ import unicode_literals 

from __future__ import absolute_import 

from builtins import str 

 

import logging 

logger = logging.getLogger(__name__) 

 

import os 

 

from django.conf import settings 

from django.utils import translation 

 

from lino.modlib.printing.choicelists import DjangoBuildMethod, BuildMethods 

 

 

from wkhtmltopdf.utils import render_pdf_from_template 

 

 

class WkBuildMethod(DjangoBuildMethod): 

    """ 

    """ 

 

    template_ext = '.wk.html' 

    templates_name = 'wk' 

    default_template = 'default.wk.html' 

    target_ext = '.pdf' 

    # cache_name = 'wkhtmltopdf' 

 

    def build(self, ar, action, elem): 

        filename = action.before_build(self, elem) 

        if filename is None: 

            return 

        tpl = self.get_template(action, elem) 

        htpl = None 

        ftpl = None 

 

        lang = str(elem.get_print_language() 

                   or settings.SITE.DEFAULT_LANGUAGE.django_code) 

        logger.info("wkhtmltopdf render %s -> %s (%r)", tpl, filename, lang) 

 

        with translation.override(lang): 

 

            context = elem.get_printable_context(ar) 

            html = render_pdf_from_template(tpl, htpl, ftpl, context) 

            # html = html.encode("utf-8") 

            file(filename, 'w').write(html) 

            return os.path.getmtime(filename) 

 

add = BuildMethods.add_item_instance 

add(WkBuildMethod('wkhtmltopdf'))