|
1 from unittest import TestCase |
|
2 import os.path as osp |
|
3 from xml.etree.cElementTree import ElementTree, fromstring, tostring, dump |
|
4 |
|
5 from tempfile import NamedTemporaryFile |
|
6 from subprocess import Popen as sub |
|
7 |
|
8 from cubicweb.utils import can_do_pdf_conversion |
|
9 |
|
10 from cubicweb.web.xhtml2fo import ReportTransformer |
|
11 |
|
12 DATADIR = osp.join(osp.dirname(__file__), 'data') |
|
13 |
|
14 class PDFTC(TestCase): |
|
15 |
|
16 def test_xhtml_to_fop_to_pdf(self): |
|
17 if not can_do_pdf_conversion(): |
|
18 self.skip('dependencies not available : check pysixt and fop') |
|
19 xmltree = ElementTree() |
|
20 xmltree.parse(osp.join(DATADIR, 'sample1.xml')) |
|
21 foptree = ReportTransformer(u'contentmain').transform(xmltree) |
|
22 # next |
|
23 foptmp = NamedTemporaryFile() |
|
24 foptree.write(foptmp) |
|
25 foptmp.flush() |
|
26 pdftmp = NamedTemporaryFile() |
|
27 fopproc = sub(['/usr/bin/fop', foptmp.name, pdftmp.name]) |
|
28 fopproc.wait() |
|
29 del foptmp |
|
30 pdftmp.seek(0) # a bit superstitious |
|
31 reference = open(osp.join(DATADIR, 'sample1.pdf'), 'r').read() |
|
32 output = pdftmp.read() |
|
33 # XXX almost equals due to ID, creation date, so it seems to fail |
|
34 self.assertTextEquals(output, reference) |