Beispiel #1
0
class AddJsTestCase(unittest.TestCase):
    """ [EXPLAIN THIS CLASS.] """
    def setUp(self):
        """ [EXPLAIN THIS CONVENIENCE.] """
        reader = PdfFileReader(join(TEST_DATA_ROOT, "crazyones.pdf"))
        self.writer = PdfFileWriter(BytesIO(b""))
        self.writer.appendPagesFromReader(reader)

    def testAdd(self):
        """ [EXPLAIN THIS TEST.] """
        self.writer.addJS(
            "this.print({bUI:true,bSilent:false,bShrinkToFit:true});")

        self.assertIn(
            "/Names",
            self.writer._rootObject,
            "addJS should add a name catalog in the root object.",
        )
        self.assertIn(
            "/JavaScript",
            self.writer._rootObject["/Names"],
            "addJS should add a JavaScript name tree under the name catalog.",
        )
        self.assertIn(
            "/JavaScript",
            self.writer._rootObject,
            "addJS should add a JavaScript action to the catalog.",
        )

    def testOverwrite(self):
        """ [EXPLAIN THIS TEST.] """
        self.writer.addJS(
            "this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
        first_js = self._getJavascriptName()

        self.writer.addJS(
            "this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
        second_js = self._getJavascriptName()

        self.assertNotEqual(
            first_js,
            second_js,
            "addJS should overwrite the previous script in the catalog.",
        )

    def _getJavascriptName(self):
        self.assertIn("/Names", self.writer._rootObject)
        self.assertIn("/JavaScript", self.writer._rootObject["/Names"])
        self.assertIn("/Names",
                      self.writer._rootObject["/Names"]["/JavaScript"])
        return self.writer._rootObject["/Names"]["/JavaScript"]["/Names"][0]
Beispiel #2
0
class AddJsTestCase(unittest.TestCase):
    def setUp(self):
        reader = PdfFileReader(join(TEST_DATA_ROOT, 'crazyones.pdf'))
        self.writer = PdfFileWriter(BytesIO(b""))
        self.writer.appendPagesFromReader(reader)

    def testAdd(self):
        self.writer.addJS(
            "this.print({bUI:true,bSilent:false,bShrinkToFit:true});")

        self.assertIn('/Names', self.writer._rootObject,
                      "addJS should add a name catalog in the root object.")
        self.assertIn(
            '/JavaScript', self.writer._rootObject['/Names'],
            "addJS should add a JavaScript name tree under the name catalog.")
        self.assertIn('/OpenAction', self.writer._rootObject,
                      "addJS should add an OpenAction to the catalog.")

    def testOverwrite(self):
        self.writer.addJS(
            "this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
        first_js = self._getJavascriptName()

        self.writer.addJS(
            "this.print({bUI:true,bSilent:false,bShrinkToFit:true});")
        second_js = self._getJavascriptName()

        self.assertNotEqual(
            first_js, second_js,
            "addJS should overwrite the previous script in the catalog.")

    def _getJavascriptName(self):
        self.assertIn('/Names', self.writer._rootObject)
        self.assertIn('/JavaScript', self.writer._rootObject['/Names'])
        self.assertIn('/Names',
                      self.writer._rootObject['/Names']['/JavaScript'])
        return self.writer. \
            _rootObject['/Names']['/JavaScript']['/Names'][0]