File: formats.py

package info (click to toggle)
trac-wikiprint 4.0.0%2Bsvn18614%2Breally3.0.0%2Bsvn18295-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 168 kB
  • sloc: python: 980; javascript: 32; makefile: 3
file content (64 lines) | stat: -rw-r--r-- 2,401 bytes parent folder | download
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
"""
Copyright (C) 2008 Prognus Software Livre - www.prognus.com.br
Author: Diorgenes Felipe Grzesiuk <diorgenes@prognus.com.br>
Modified by: Alvaro Iradier <alvaro.iradier@polartech.es>
"""

from trac.core import Component, implements
from trac.web.api import RequestDone
from trac.wiki.model import WikiPage

from .api import IWikiPrintFormat
from .wikiprint import WikiPrint


class WikiPrintOutput(Component):
    """Add output formats PDF using PISA (xhtml2pdf) or Printable HTML to
    the WikiPrint plugin."""

    implements(IWikiPrintFormat)

    def wikiprint_formats(self, req):
        yield 'pdfarticle', 'PDF Article'
        yield 'pdfbook', 'PDF Book'
        yield 'printhtml', 'HTML'

    def process_wikiprint(self, req, format, title, subject, pages, version,
                          date, pdfname):

        wikiprint = WikiPrint(self.env)

        html_pages = [
            wikiprint.wikipage_to_html(WikiPage(self.env, p).text, p, req)
            for p in pages
        ]

        # Send the output
        req.send_response(200)
        req.send_header('Content-Type', {
            'pdfbook': 'application/pdf',
            'pdfarticle': 'application/pdf',
            'printhtml': 'text/html'}[format])

        out = ''
        if format == 'pdfbook':
            out = wikiprint.html_to_pdf(req, html_pages, book=True,
                                        title=title, subject=subject,
                                        version=version, date=date)
            req.send_header('Content-Disposition', 'attachment; filename='
                                                   + pdfname + '.pdf')
        elif format == 'pdfarticle':
            out = wikiprint.html_to_pdf(req, html_pages, book=False,
                                        title=title, subject=subject,
                                        version=version, date=date)
            req.send_header('Content-Disposition', 'attachment; filename='
                                                   + pdfname + '.pdf')
        elif format == 'printhtml':
            out = wikiprint.html_to_printhtml(req, html_pages, title=title,
                                              subject=subject, version=version,
                                              date=date)

        req.send_header('Content-Length', len(out))
        req.end_headers()
        req.write(out)
        raise RequestDone