1 import os.path as osp |
|
2 from tempfile import NamedTemporaryFile |
|
3 from subprocess import Popen as sub |
|
4 from xml.etree.cElementTree import ElementTree, fromstring, tostring, dump |
|
5 |
|
6 from logilab.common.testlib import TestCase, unittest_main |
|
7 |
|
8 from cubicweb.utils import can_do_pdf_conversion |
|
9 from cubicweb.ext.xhtml2fo import ReportTransformer |
|
10 |
|
11 DATADIR = osp.join(osp.dirname(__file__), 'data') |
|
12 |
|
13 class PDFTC(TestCase): |
|
14 |
|
15 def test_xhtml_to_fop_to_pdf(self): |
|
16 if not can_do_pdf_conversion(): |
|
17 self.skip('dependencies not available : check pysixt and fop') |
|
18 xmltree = ElementTree() |
|
19 xmltree.parse(osp.join(DATADIR, 'sample1.xml')) |
|
20 foptree = ReportTransformer(u'contentmain').transform(xmltree) |
|
21 # next |
|
22 foptmp = NamedTemporaryFile() |
|
23 foptree.write(foptmp) |
|
24 foptmp.flush() |
|
25 pdftmp = NamedTemporaryFile() |
|
26 fopproc = sub(['/usr/bin/fop', foptmp.name, pdftmp.name]) |
|
27 fopproc.wait() |
|
28 del foptmp |
|
29 if fopproc.returncode: |
|
30 self.skip('fop returned status %s' % fopproc.returncode) |
|
31 pdftmp.seek(0) # a bit superstitious |
|
32 reference = open(osp.join(DATADIR, 'sample1.pdf'), 'r').read() |
|
33 output = pdftmp.read() |
|
34 # XXX almost equals due to ID, creation date, so it seems to fail |
|
35 self.assertEquals( len(output), len(reference) ) |
|
36 # cut begin & end 'cause they contain variyng data |
|
37 self.assertTextEquals(output[150:1500], reference[150:1500]) |
|
38 |
|
39 if __name__ == '__main__': |
|
40 unittest_main() |
|
41 |
|