web/test/unittest_pdf.py
brancholdstable
changeset 5993 50e1a6ad3e98
parent 5487 3ab2682a4b37
parent 5976 00b1b6b906cf
child 6018 f4d1d5d9ccbb
equal deleted inserted replaced
5487:3ab2682a4b37 5993:50e1a6ad3e98
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 import os.path as osp
       
    19 from tempfile import NamedTemporaryFile
       
    20 from subprocess import Popen as sub
       
    21 from xml.etree.cElementTree import ElementTree, fromstring, tostring, dump
       
    22 
       
    23 from logilab.common.testlib import TestCase, unittest_main
       
    24 
       
    25 from cubicweb.utils import can_do_pdf_conversion
       
    26 from cubicweb.ext.xhtml2fo import ReportTransformer
       
    27 
       
    28 DATADIR = osp.join(osp.dirname(__file__), 'data')
       
    29 
       
    30 class PDFTC(TestCase):
       
    31 
       
    32     def test_xhtml_to_fop_to_pdf(self):
       
    33         if not can_do_pdf_conversion():
       
    34             self.skip('dependencies not available : check pysixt and fop')
       
    35         xmltree = ElementTree()
       
    36         xmltree.parse(osp.join(DATADIR, 'sample1.xml'))
       
    37         foptree = ReportTransformer(u'contentmain').transform(xmltree)
       
    38         # next
       
    39         foptmp = NamedTemporaryFile()
       
    40         foptree.write(foptmp)
       
    41         foptmp.flush()
       
    42         pdftmp = NamedTemporaryFile()
       
    43         fopproc = sub(['/usr/bin/fop', foptmp.name, pdftmp.name])
       
    44         fopproc.wait()
       
    45         del foptmp
       
    46         if fopproc.returncode:
       
    47             self.skip('fop returned status %s' % fopproc.returncode)
       
    48         pdftmp.seek(0) # a bit superstitious
       
    49         reference = open(osp.join(DATADIR, 'sample1.pdf'), 'r').read()
       
    50         output = pdftmp.read()
       
    51         # XXX almost equals due to ID, creation date, so it seems to fail
       
    52         self.assertEquals( len(output), len(reference) )
       
    53         # cut begin & end 'cause they contain variyng data
       
    54         self.assertTextEquals(output[150:1500], reference[150:1500])
       
    55 
       
    56 if __name__ == '__main__':
       
    57     unittest_main()
       
    58