def test2(self): "Test generating several 'n-up' docs in 'legal' format." # minipages are squeezed, i.e. they lose their original page ratio... # needs to be addressed later... for path0 in ("samples/test-legal-p.pdf",): for n in (2, 4, 8, 9): outName = os.path.splitext(path0)[0] + "-%dup.pdf" % n path1 = os.path.join(".", outName) generateNup(path0, n, path1, verbose=False) # , dirs="UL") # assert output has correct number of pages input = PdfFileReader(file(path0, "rb")) np0 = input.getNumPages() input = PdfFileReader(file(path1, "rb")) np1 = input.getNumPages() self.assertEqual(np1, math.ceil(np0 / float(n))) # assert output page(s) has/have correct text content for pn in range(np1): page = input.getPage(pn) text = page.extractText().split() exp = group([str(num) for num in range(np0)], n)[pn] self.assertEqual(text, exp)
def test2(self): "Test generating several 'n-up' docs in 'legal' format." # minipages are squeezed, i.e. they lose their original page ratio... # needs to be addressed later... for path0 in ("samples/test-legal-p.pdf", ): for n in (2, 4, 8, 9): outName = os.path.splitext(path0)[0] + "-%dup.pdf" % n path1 = os.path.join(".", outName) generateNup(path0, n, path1, verbose=False) # , dirs="UL") # assert output has correct number of pages input = PdfFileReader(file(path0, "rb")) np0 = input.getNumPages() input = PdfFileReader(file(path1, "rb")) np1 = input.getNumPages() self.assertEqual(np1, math.ceil(np0 / float(n))) # assert output page(s) has/have correct text content for pn in range(np1): page = input.getPage(pn) text = page.extractText().split() exp = group([str(num) for num in range(np0)], n)[pn] self.assertEqual(text, exp)
def test0(self): "Test using file input and filename output document." n = 4 path0 = "samples/test-a4-l.pdf" f = open(path0, "rb") outName = os.path.splitext(path0)[0] + "-%dup-fromFileObj.pdf" % n path1 = os.path.join(".", outName) generateNup(f, n, path1, verbose=False)
def test1(self): "Test using StringIO input and filename output document." n = 4 path0 = "samples/test-a4-l.pdf" pdfCode = open(path0, "rb").read() f = StringIO(pdfCode) outName = os.path.splitext(path0)[0] + "-%dup-fromStringIO.pdf" % n path1 = os.path.join(".", outName) generateNup(f, n, path1, verbose=False)
def test1(self): "Test using filename input and file output document." n = 4 path0 = "samples/test-a4-l.pdf" path1 = "samples/test-a4-l-toFileObj.pdf" output = open(path1, "wb") generateNup(path0, n, output, verbose=False) output.close() data = open(path1).read() self.assertTrue(data.startswith("%PDF")) self.assertTrue(data.rstrip().endswith("%%EOF"))
def test1(self): "Test using filename input and file output document." n = 4 path0 = "samples/test-a4-l.pdf" path1 = "samples/test-a4-l-toFileObj.pdf" output = open(path1, "wb") generateNup(path0, n, output, verbose=False) output.close() data = open(path1).read() self.assert_(data.startswith("%PDF")) self.assert_(data.rstrip().endswith("%%EOF"))
def test0(self): "Test using filename input and StringIO output document." n = 4 path0 = "samples/test-a4-l.pdf" path1 = "samples/test-a4-l-toStringIO.pdf" output = StringIO() generateNup(path0, n, output, verbose=False) output.seek(0) data = output.read() self.assertTrue(data.startswith("%PDF")) self.assertTrue(data.rstrip().endswith("%%EOF")) open(path1, "wb").write(data)
def test0(self): "Test using filename input and StringIO output document." n = 4 path0 = "samples/test-a4-l.pdf" path1 = "samples/test-a4-l-toStringIO.pdf" output = StringIO() generateNup(path0, n, output, verbose=False) output.seek(0) data = output.read() self.assert_(data.startswith("%PDF")) self.assert_(data.rstrip().endswith("%%EOF")) open (path1, "wb").write(data)
def test1(self): "Test on rotated pages in landscape format." output = PdfFileWriter() input1 = PdfFileReader(file("samples/test-a4-l.pdf", "rb")) numPages = input1.getNumPages() for i in range(numPages): p = input1.getPage(i) p.rotateClockwise((i % 4) * 90) output.addPage(p) outPath = "samples/test-a4-lr.pdf" outputStream = file(outPath, "wb") output.write(outputStream) outputStream.close() for j in (2, 4, 8, 9): generateNup(outPath, j, verbose=False)
def test1(self): "Test generating several 'n-up' docs, n = (m**2) / 2..." for path0 in ("samples/test-a4-l.pdf", "samples/test-a4-p.pdf"): for n in (2, 8, 18): outName = os.path.splitext(path0)[0] + "-%dup.pdf" % n path1 = os.path.join(".", outName) generateNup(path0, n, path1, verbose=False) # , dirs="UL") # assert output has correct number of pages input = PdfFileReader(file(path0, "rb")) np0 = input.getNumPages() input = PdfFileReader(file(path1, "rb")) np1 = input.getNumPages() self.assertEqual(np1, math.ceil(np0 / float(n))) # assert output page(s) has/have correct text content for pn in range(np1): page = input.getPage(pn) text = page.extractText().split() exp = group([str(num) for num in range(np0)], n)[pn] self.assertEqual(text, exp)
pdf_in = open(root.filename, 'rb') print('Found PDF, executing...') print('Step 1 of 2: Rotating') pdf_reader = PyPDF2.PdfFileReader(pdf_in) pdf_writer = PyPDF2.PdfFileWriter() for pagenum in range(pdf_reader.numPages): page = pdf_reader.getPage(pagenum) if pagenum % 2: page.rotateClockwise(180) pdf_writer.addPage(page) pdf_out = open('output-rotated.pdf', 'wb') pdf_writer.write(pdf_out) pdf_out.close() pdf_in.close() print('Step 2 of 2: Nupping') generateNup("output-rotated.pdf", 2, verbose=True) # Delete temporary pdf print("Clearing Cache"); os.remove("output-rotated.pdf"); print("Done");
def merge_PDFs_mult_on_page(PDFs_L, out_F, direc="horizontal"): """ - Merges N PDFs (absolute paths to PDFs in PDFs_L) into the out_F. PDF_1 PDF_2 PDF_3 PDF_4 - If there are 2 PDFs to combine, if direc == "horizontal": PDF_1 PDF_2 "vertical": PDF_1 PDF_2 """ assert (len(PDFs_L) <= 16) if (len(PDFs_L) <= 2): Nsquared_or_div2 = 2 elif (len(PDFs_L) <= 4): Nsquared_or_div2 = 4 elif (len(PDFs_L) <= 6): Nsquared_or_div2 = 6 elif (len(PDFs_L) <= 8): Nsquared_or_div2 = 8 elif (len(PDFs_L) <= 9): Nsquared_or_div2 = 9 elif (len(PDFs_L) <= 12): Nsquared_or_div2 = 12 else: Nsquared_or_div2 = 16 from PyPDF2 import PdfFileReader, PdfFileMerger, PdfFileWriter from PyPDF2.generic import RectangleObject from pdfnup import generateNup output = PdfFileWriter() for F in PDFs_L: input = PdfFileReader(file(F, "rb")) img = input.getPage(0) output.addPage(img) outputStream = file(out_F, "wb") output.write(outputStream) outputStream.close() combined_PDF_F = out_F.split(".pdf")[0] + "-{}up.pdf".format( Nsquared_or_div2) if (Nsquared_or_div2 == 4): cmd = "pdfnup -n 4 --output {0} {1}".format(combined_PDF_F, out_F) #print cmd os.system(cmd) #print "\n\tDONE!\n\n" elif (Nsquared_or_div2 == 2): if (direc == "vertical"): cmd = "pdfnup -n 2 --output {0} {1}".format(combined_PDF_F, out_F) elif (direc == "horizontal"): cmd = "pdfnup -n 2 --output {0} {1}".format(combined_PDF_F, out_F) #print cmd os.system(cmd) #print "\n\tDONE!\n\n" else: generateNup(out_F, Nsquared_or_div2) print cmd return combined_PDF_F