def test_merge_pdfrw(self): """Merge multiple PDF files into a single PDF using the `pdfrw` library.""" merged = Merge(self.pdfs, output_name='merged_pdfrw', output_dir=self.temp.name, method='pdfrw') # Assert merged file exists self.assertTrue(os.path.exists(merged.file)) # Assert sum of pages in original pdf files equals sum of pages in merged pdf self.assertEqual(sum([Info(pdf).pages for pdf in self.pdfs]), Info(merged.file).pages) return merged
def test_downscale_pdfrw_20x(self): """Resize a PDF file to 3.0x times the original scale.""" s = 1 / 2 upscaled = Upscale(pdf_path, scale=s, suffix='downscaled_2.0_pdfrw', tempdir=self.temp.name, method='pdfrw').file # Assert upscaled file exists self.assertTrue(os.path.isfile(upscaled)) # Assert upscaled pdf file is the correct size self.assertEqual( Info(upscaled).size, tuple([i * s for i in Info(pdf_path).size])) return upscaled
def main(): print('Testing Encrypt reliability') owner_pw = 'foo' user_pw = 'baz' p = Encrypt(pdf_path, user_pw, owner_pw) print(p.output) security = Info(p.output, user_pw).security try: assert Info(p.output, user_pw).encrypted is True assert security['/Length'] == 128 assert security['/P'] == -1852 print('Success!') except AssertionError: print('Failed!')
def test_rotate_pypdf3_90(self): """Rotate a PDF file by 90 degrees using the `pypdf3` library.""" rotation = 90 rotated = Rotate(self.pdf_path, rotation, suffix='rotated_pdfrw', tempdir=self.temp.name, method='pypdf3').file # Assert rotated pdf file exists self.assertTrue(os.path.isfile(rotated)) # Assert pdf file was rotated by the correct amount of degrees self.assertEqual(Info(rotated).rotate, rotation) return rotated
def test_slice2(self): """Slice a page range from a PDF to create a new 'trimmed' pdf file.""" fp = 4 lp = 7 sliced = slicer(self.pdf_path, first_page=fp, last_page=lp, tempdir=self.temp.name) # Assert sliced file exists self.assertTrue(os.path.isfile(sliced)) # Confirm slicer sliced the correct number of pages self.assertEqual(Info(sliced).pages, len(range(fp, lp + 1))) return sliced
def test_flatten(self): """Create a 'flattened' pdf file without layers.""" flat = Flatten(self.pdf_path, suffix='flat').save() # Assert pdf file exists self.assertTrue(os.path.exists(flat)) # Assert there are the same number of pages in the 'original' and 'flattened' pdf self.assertEqual(Info(self.pdf_path).pages, Info(flat).pages) # Confirm that PDF page sizes have not increased self.assertTrue( abs(Info(self.pdf_path).size[0] / Info(flat).size[0]) <= 1) self.assertTrue( abs(Info(self.pdf_path).size[1] / Info(flat).size[1]) <= 1) self.flat = flat return flat