Beispiel #1
0
    def testDel(self):
        """
        Tests the ``__del__()`` method of ``PdfFileReader`` and
        ``PdfFileWriter`` ensuring that no exceptions are raised.
        """
        r = PdfFileReader(join(TEST_DATA_ROOT, "crazyones.pdf"))
        w = PdfFileWriter()

        try:
            # This may generate some collateral warnings in stderr when del r
            # is performed by the GC
            r.__del__()
            self.assertTrue(True)
        except Exception as e:
            self.assertTrue(
                False, "Exception '%s' was raised in %s.__del__()" %
                (e, PdfFileReader.__name__))

        try:
            w.__del__()
            self.assertTrue(True)
        except Exception as e:
            self.assertTrue(
                False, "Exception '%s' was raised in %s.__del__()" %
                (e, PdfFileWriter.__name__))
Beispiel #2
0
    def testDel(self):
        """
        Tests the ``__del__()`` method of ``PdfFileReader`` and
        ``PdfFileWriter`` ensuring that no exceptions are raised.
        """
        r = PdfFileReader(join(TEST_DATA_ROOT, "crazyones.pdf"))
        w = PdfFileWriter(BytesIO(b""))

        try:
            r.__del__()
            self.assertTrue(True)
        except Exception as e:  # pylint: disable=broad-except
            self.assertTrue(
                False,
                "Exception '%s' was raised in %s.__del__()" %
                (e, PdfFileReader.__name__),
            )

        try:
            w.__del__()
            self.assertTrue(True)
        except Exception as e:  # pylint: disable=broad-except
            self.assertTrue(
                False,
                "Exception '%s' was raised in %s.__del__()" %
                (e, PdfFileWriter.__name__),
            )
Beispiel #3
0
    def testAttachFiles(self):
        """
        Tests the addAttachment function for attaching multiple files.

        Since the Names array in the EmbeddedFiles dictionary contains both the
        name (string) and indirect object (dictionary) for each file, we have
        to check for two entries per attached file.
        """

        numAttachments = 3
        _, testfile = tempfile.mkstemp()

        try:
            # Make PDF with attachment
            with PdfFileReader(join(TEST_DATA_ROOT, 'jpeg.pdf')) as reader:
                with PdfFileWriter(testfile) as writer:
                    writer.appendPagesFromReader(reader)

                    writer.attachFiles(
                        [join(TEST_DATA_ROOT, 'attachment_small.png')] *
                        numAttachments)
                    writer.write()

            # Check for attachment entries
            with PdfFileReader(testfile) as pdf:
                pdf.numPages  # For caching _cachedObjects data
                for k, v in pdf._cachedObjects.items():
                    if '/Type' in v:
                        if v['/Type'] == '/Catalog':
                            self.assertIsNotNone(v['/Names']['/EmbeddedFiles'])
                            real = len(v['/Names']['/EmbeddedFiles']['/Names'])
                            self.assertEqual(numAttachments * 2, real)
        finally:
            os.remove(testfile)
Beispiel #4
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 #5
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]
Beispiel #6
0
    def testAddAttachment(self):
        """
        Tests the addAttachment function for attaching a single file.

        Since the Names array in the EmbeddedFiles dictionary contains both the
        name (string) and indirect object (dictionary) for each file, we have
        to check for two entries per attached file.
        """

        _, testfile = tempfile.mkstemp()

        try:
            # Make PDF with attachment
            with PdfFileReader(join(TEST_DATA_ROOT, "jpeg.pdf")) as reader:
                with PdfFileWriter(testfile) as writer:
                    writer.appendPagesFromReader(reader)
                    with open(
                            join(  # pylint: disable=bad-continuation
                                TEST_DATA_ROOT, "attachment_small.png"),
                            "rb",  # pylint: disable=bad-continuation  # pylint: disable=bad-continuation
                    ) as attachment_stream:
                        read_data = attachment_stream.read()
                        writer.addAttachment("attachment_small.png", read_data)
                    writer.write()

            # Check for attachment entries
            with PdfFileReader(testfile) as pdf:
                # For caching _cachedObjects data
                pdf.numPages  # pylint: disable=pointless-statement
                for _k, v in pdf._cachedObjects.items():
                    if "/Type" in v:
                        if v["/Type"] == "/Catalog":
                            self.assertIsNotNone(v["/Names"]["/EmbeddedFiles"])
                            real = len(v["/Names"]["/EmbeddedFiles"]["/Names"])
                            self.assertEqual(2, real)
        finally:
            os.remove(testfile)
Beispiel #7
0
 def setUp(self):
     reader = PdfFileReader(join(TEST_DATA_ROOT, 'crazyones.pdf'))
     self.writer = PdfFileWriter(BytesIO(b""))
     self.writer.appendPagesFromReader(reader)
Beispiel #8
0
def main():
    pagesRequired = 5
    output = "PyPDF-Features-Output.pdf"

    if set(argv) & FLAG_HELP:
        print(USAGE)
        exit(0)
    elif len(argv) < 2:
        print(USAGE)
        exit(1)
    else:
        inputpath = argv[1].strip()
        filename = basename(inputpath)

        if len(argv) > 2:
            output = argv[2].strip()

    # We can instantiate a PdfFileReader/Writer by giving in a stream object
    # or a path string
    reader = PdfFileReader(open(inputpath, "rb"))
    writer = PdfFileWriter(output)

    # Check that the PDF file has the required number of pages
    if reader.numPages < pagesRequired:
        print(
            "We require a document with %d pages at least, %s has %d"
            % (pagesRequired, filename, reader.numPages),
            file=stderr,
        )
        exit(1)
    else:
        print("'%s' has %d pages... OK" % (filename, reader.numPages))

    # Add page 1 from reader to output document, unchanged
    writer.addPage(reader.getPage(0))

    # Add page 2 from reader, but rotated clockwise 90 degrees
    writer.addPage(reader.getPage(1).rotateClockwise(90))

    # Add page 3 from reader, rotated the other way:
    writer.addPage(reader.getPage(2).rotateCounterClockwise(90))
    # Alt.: writer.addPage(reader.getPage(2).rotateClockwise(270))

    # Add page 4 from reader, but first add a watermark from another PDF:
    page4 = reader.getPage(3)
    watermark = PdfFileReader(open(join(SAMPLE_PDF_ROOT, "AutoCad_Diagram.pdf"), "rb"))
    page4.mergePage(watermark.getPage(0))
    writer.addPage(page4)

    # Add page 5 from reader, but crop it to half size:
    page5 = reader.getPage(4)
    page5.mediaBox.upperRight = (
        page5.mediaBox.getUpperRight_x() / 2,
        page5.mediaBox.getUpperRight_y() / 2,
    )
    writer.addPage(page5)

    # Add some Javascript to launch the print window on opening this PDF.
    # The password dialog may prevent the print dialog from being shown.
    # Comment the encrypted lines, if that's the case, to try this out
    writer.addJS("this.print({bUI:true,bSilent:false,bShrinkToFit:true});")

    # Encrypt your new PDF and add a password
    password = "******"
    writer.encrypt(password)

    # Finally, write the resulting PDF document to ``output``
    writer.write()

    print("Output successfully written to", output)

    reader.close()
    writer.close()
Beispiel #9
0
 def setUp(self):
     ipdf = PdfFileReader(join(TEST_DATA_ROOT, 'crazyones.pdf'))
     self.pdfFileWriter = PdfFileWriter()
     self.pdfFileWriter.appendPagesFromReader(ipdf)
Beispiel #10
0
 def setUp(self):
     """ [EXPLAIN THIS CONVENIENCE.] """
     reader = PdfFileReader(join(TEST_DATA_ROOT, "crazyones.pdf"))
     self.writer = PdfFileWriter(BytesIO(b""))
     self.writer.appendPagesFromReader(reader)