Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/exportpdf.py
blob: 6288c52ab2d5b273f5f9c10db765945ccdf7c741 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# -*- coding: utf-8 -*-
#Copyright (c) 2011 Walter Bender

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU General Public License
# along with this library; if not, write to the Free Software
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA

import os.path
import time
import json

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gdk
from gi.repository import GdkPixbuf
from glib import GError
from gi.repository import Pango
from gi.repository import PangoCairo
import cairo

from gettext import gettext as _

from utils import get_pixbuf_from_journal, parse_comments

import logging
_logger = logging.getLogger("portfolio-activity")


PAGE_WIDTH = 504
PAGE_HEIGHT = 648
LEFT_MARGIN = 10
TOP_MARGIN = 20


def save_pdf(activity, nick, description=None):
    ''' Output a PDF document from the title, pictures, and descriptions '''

    if len(activity.dsobjects) == 0:
        return None

    head = activity.title_size
    body = activity.desc_size / 2

    tmp_file = os.path.join(activity.datapath, 'output.pdf')
    pdf_surface = cairo.PDFSurface(tmp_file, 504, 648)

    fd = Pango.FontDescription('Sans')
    cr = cairo.Context(pdf_surface)
    cr.set_source_rgb(0, 0, 0)

    show_text(cr, fd, nick, head, LEFT_MARGIN, TOP_MARGIN)
    show_text(cr, fd, time.strftime('%x', time.localtime()),
              body, LEFT_MARGIN, TOP_MARGIN + 3 * head)
    if description is not None:
        show_text(cr, fd, description,
                  body, LEFT_MARGIN, TOP_MARGIN + 4 * head)
    cr.show_page()

    for i, dsobj in enumerate(activity.dsobjects):
        if dsobj.metadata['keep'] == '0':
            continue
        if 'title' in dsobj.metadata:
            show_text(cr, fd, dsobj.metadata['title'], head, LEFT_MARGIN,
                      TOP_MARGIN)
        else:
            show_text(cr, fd, _('untitled'), head, LEFT_MARGIN,
                      TOP_MARGIN)

        w = 0
        h = 0
        pixbuf = None
        if os.path.exists(dsobj.file_path):
            print dsobj.file_path
            try:
                w = int(PAGE_WIDTH - LEFT_MARGIN * 2)
                h = int(w * 3 / 4)
                pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(
                    dsobj.file_path, w, h)
            except: # (GError, IOError):
                try:
                    w = 300
                    h = 225
                    pixbuf = get_pixbuf_from_journal(dsobj, w, h)
                except: # (GError, IOError):
                    pass

        if pixbuf is not None:
            cr.save()
            Gdk.cairo_set_source_pixbuf(
                cr, pixbuf, LEFT_MARGIN, TOP_MARGIN + 150)
            cr.rectangle(LEFT_MARGIN, TOP_MARGIN + 150, w, h)
            cr.fill()
            cr.restore()

        text = ''
        if 'description' in dsobj.metadata:
            text += dsobj.metadata['description']
        if 'comments' in dsobj.metadata:
            text += '\n'
            text += parse_comments(json.loads(dsobj.metadata['comments']))
        show_text(cr, fd, text, body, LEFT_MARGIN, h + 175)

        cr.show_page()

    return tmp_file


def show_text(cr, fd, label, size, x, y):
    pl = PangoCairo.create_layout(cr)
    fd.set_size(int(size * Pango.SCALE))
    pl.set_font_description(fd)
    if type(label) == str or type(label) == unicode:
        pl.set_text(label.replace('\0', ' '), -1)
    else:
        pl.set_text(str(label), -1)
    pl.set_width((PAGE_WIDTH - LEFT_MARGIN * 2) * Pango.SCALE)
    cr.save()
    cr.translate(x, y)
    PangoCairo.update_layout(cr, pl)
    PangoCairo.show_layout(cr, pl)
    cr.restore()