def main(incoming_args): backendName = os.path.basename(incoming_args[0]) destination = getDestination() logFile = os.path.join(destination, backendName) + ".log" if len(incoming_args) == 1: logger("direct %s \"Unknown\" \"Save to PDF\"\n" % backendName, None) sys.stdout.flush() sys.exit(0) if len(incoming_args) not in (6,7): sys.stdout.flush() logger("Wrong number of arguments. Usage: %s job-id user title copies options [file]\n" % backendName, None) sys.exit(1) global user jobID, user, title, copies, options = (incoming_args[1:6]) outFilename = os.path.join(destination, title) outFilename = os.path.splitext(outFilename)[0] logger(jobID+"\n", logFile) logger(user+"\n", logFile) logger(title+"\n", logFile) logger(copies+"\n", logFile) logger(options+"\n", logFile) # If 5 arguments, take PDF/PS file from stdin; if 6, take filename. if len(incoming_args) == 7: inFilename = incoming_args[6] fileRef = open(inFilename, 'r') fileType = fileRef.read(4) outFilename += getType(fileType) pdfURL = NSURL.fileURLWithPath_(inFilename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) encrypt(inFilename, pdfDoc) os.rename(inFilename, outFilename) else: fileType = sys.stdin.read(4) outFilename += getType(fileType) global myDataObject myDataObject = NSMutableData.alloc().initWithLength_(0) myDataObject.appendBytes_length_(fileType, len(fileType)) for myChunk in sys.stdin: myDataObject.appendBytes_length_(myChunk, len(myChunk)) pdfDoc = PDFDocument.alloc().initWithData_(myDataObject) encrypt(outFilename, pdfDoc) fileRef = open(outFilename, "w") fileRef.write(myDataObject.getBytes_length_(None, myDataObject.length())) fileRef.close # Fix file permissions of PDF for user fixPerms(outFilename) # Make sure everyone can read log. os.chmod(logFile, 0744)
def pageCount(pdfPath): # The first way of counting pages, using PDFDocument. pdfPath = pdfPath.decode('utf-8') pdfURL = NSURL.fileURLWithPath_(pdfPath) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: return pdfDoc.pageCount()
def create_pdf_for_page(pdf, page_number): page_pdf = PDFDocument.alloc().init() page_pdf.insertPage_atIndex_(pdf.pageAtIndex_(page_number), 0) f, pdf_path = tempfile.mkstemp(prefix="pdf2keynote", suffix=".pdf") page_pdf.writeToFile_(pdf_path) return pdf_path
def reverse_pdf(pdf_path): # build output PDF filename base_path, ext = os.path.splitext(pdf_path) resersed_pdf_path = base_path + '_reversed' + ext pdf_nsurl = NSURL.fileURLWithPath_(pdf_path) pdf = PDFDocument.alloc().initWithURL_(pdf_nsurl) reversed_pdf = PDFDocument.alloc().init() page_count = pdf.pageCount() pdf_page = PDFPage # n is sequential page increase, r is the reversed page number for n, r in enumerate(reversed(range(0, page_count))): pdf_page = pdf.pageAtIndex_(r) reversed_pdf.insertPage_atIndex_(pdf_page, n) reversed_pdf.writeToFile_(resersed_pdf_path) return resersed_pdf_path
def removePage(filename): pdfURL = NSURL.fileURLWithPath_(filename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: pageNum = pdfDoc.pageCount() if pageNum > 1: pdfDoc.removePageAtIndex_(0) pdfDoc.writeToFile_(filename) return
def main(): for filename in sys.argv[1:]: shortName = os.path.splitext(filename)[0] outputfile = shortName+" text.txt" pdfURL = NSURL.fileURLWithPath_(filename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc : pdfString = NSString.stringWithString_(pdfDoc.string()) pdfString.writeToFile_atomically_encoding_error_(outputfile, True, NSUTF8StringEncoding, None)
def removePage(filename): filename = filename.decode('utf-8') pdfURL = NSURL.fileURLWithPath_(filename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: pageNum = pdfDoc.pageCount() if pageNum > 1: pdfDoc.removePageAtIndex_(0) pdfDoc.writeToFile_(filename) return
def main(): for filename in sys.argv[1:]: inputfile =filename.decode('utf-8') shortName = os.path.splitext(filename)[0] outputfile = shortName+" text.txt" pdfURL = NSURL.fileURLWithPath_(inputfile) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc : pdfString = NSString.stringWithString_(pdfDoc.string()) pdfString.writeToFile_atomically_encoding_error_(outputfile, True, NSUTF8StringEncoding, None)
def debooklet(filename): shortName = os.path.splitext(filename)[0] outFilename = shortName + " paged.pdf" # If running python2, uncomment the following line: # filename = filename.decode('utf-8') pdfURL = NSURL.fileURLWithPath_(filename) leftPDF = PDFDocument.alloc().initWithURL_(pdfURL) rightPDF = PDFDocument.alloc().initWithURL_(pdfURL) newPDF = PDFDocument.alloc().init() if leftPDF: if not (doPageOne): leftPage = leftPDF.pageAtIndex_(0) newPDF.insertPage_atIndex_(leftPage, 0) pages = leftPDF.pageCount() startPage = int(not (doPageOne)) for p in range(startPage, pages): outPageCount = newPDF.pageCount() leftPage = leftPDF.pageAtIndex_(p) rightPage = rightPDF.pageAtIndex_(p) mediaBoxSize = leftPage.boundsForBox_(mediabox) rotation = leftPage.rotation() if (rotation == 0) or (rotation == 180): halfway = (mediaBoxSize.size.width / 2) pageHeight = mediaBoxSize.size.height leftHandCrop = CGRectMake(0, 0, halfway, pageHeight) rightHandCrop = CGRectMake(halfway, 0, halfway, pageHeight) leftPage.setBounds_forBox_(leftHandCrop, mediabox) rightPage.setBounds_forBox_(rightHandCrop, mediabox) else: halfway = (mediaBoxSize.size.height / 2) pageWidth = mediaBoxSize.size.width topCrop = CGRectMake(0, 0, pageWidth, halfway) bottomCrop = CGRectMake(0, halfway, pageWidth, halfway) leftPage.setBounds_forBox_(topCrop, mediabox) rightPage.setBounds_forBox_(bottomCrop, mediabox) newPDF.insertPage_atIndex_(leftPage, outPageCount) newPDF.insertPage_atIndex_(rightPage, outPageCount + 1) newPDF.writeToFile_(outFilename)
def addPage(filename): filename = filename.decode('utf-8') pdfURL = NSURL.fileURLWithPath_(filename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: pageNum = pdfDoc.pageCount() page = pdfDoc.pageAtIndex_(0) pageSize = page.boundsForBox_(mediabox) blankPage = PDFPage.alloc().init() blankPage.setBounds_forBox_(pageSize, mediabox) pdfDoc.insertPage_atIndex_(blankPage, pageNum) pdfDoc.writeToFile_(filename) return
def doRotate(filename): shortName = os.path.splitext(filename)[0] outFilename = shortName + "+90.pdf" pdfURL = NSURL.fileURLWithPath_(filename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: pages = pdfDoc.pageCount() for p in range(0, pages): page = pdfDoc.pageAtIndex_(p) existingRotation = page.rotation() newRotation = existingRotation + 90 page.setRotation_(newRotation) pdfDoc.writeToFile_(outFilename)
def merge_pdfs(first_pdf_path, second_pdf_path): # build output PDF filename merged_pdf_path = os.path.dirname(first_pdf_path) + '/merged.pdf' merged_pdf = PDFDocument.alloc().init() first_pdf_pages = get_pdf_pages(first_pdf_path) second_pdf_pages = get_pdf_pages(second_pdf_path) for index, page in enumerate(first_pdf_pages + second_pdf_pages): merged_pdf.insertPage_atIndex_(page, index) merged_pdf.writeToFile_(merged_pdf_path) return merged_pdf_path
def trimPDF(filename): filename = filename.decode('utf-8') shortName = os.path.splitext(filename)[0] outFilename = shortName + " TPS.pdf" pdfURL = NSURL.fileURLWithPath_(filename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: pages = pdfDoc.pageCount() for p in range(0, pages): page = pdfDoc.pageAtIndex_(p) mediaBoxSize = page.boundsForBox_(mediabox) trimBoxSize = CGRectMake(margins[0], margins[1], (mediaBoxSize.size.width - margins[2] - margins[0]), (mediaBoxSize.size.height - margins[3] - margins[1])) page.setBounds_forBox_(trimBoxSize, mediabox) pdfDoc.writeToFile_(outFilename)
def trimPDF(filename): # filename = filename.decode('utf-8') shortName = os.path.splitext(filename)[0] outFilename = shortName + " TPS.pdf" pdfURL = NSURL.fileURLWithPath_(filename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: pages = pdfDoc.pageCount() for p in range(0, pages): page = pdfDoc.pageAtIndex_(p) mediaBoxSize = page.boundsForBox_(mediabox) trimBoxSize = CGRectMake(margins[0], margins[1], (mediaBoxSize.size.width - margins[2] - margins[0]), (mediaBoxSize.size.height - margins[3] - margins[1])) page.setBounds_forBox_(trimBoxSize, mediabox) pdfDoc.writeToFile_(outFilename)
def pdfSearch(filepath, searchString): pdfURL = NSURL.fileURLWithPath_(filepath) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: searchResults = (pdfDoc.findString_withOptions_(searchString, 0)) if searchResults: for result in searchResults: eachPage = result.pages() print("\'" + searchString + "\' was found on page: " + str(pdfDoc.indexForPage_(eachPage[0]) + 1)) else: print("Nothing found.") else: print("Not a valid PDF.") return
def encrypt(filename): if not filename: print ('Unable to open input file') sys.exit(2) shortName = os.path.splitext(filename)[0] outputfile = shortName+" locked.pdf" pdfURL = NSURL.fileURLWithPath_(filename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc : options = { kCGPDFContextAllowsCopying: False, kCGPDFContextAllowsPrinting: False, kCGPDFContextOwnerPassword: copyPassword, kCGPDFContextUserPassword: openPassword} pdfDoc.writeToFile_withOptions_(outputfile, options) return
def trimPDF(filename): hasBeenChanged = False filename = filename.decode('utf-8') shortName = os.path.splitext(filename)[0] outFilename = shortName + " TPS.pdf" pdfURL = NSURL.fileURLWithPath_(filename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: pages = pdfDoc.pageCount() for p in range(0, pages): page = pdfDoc.pageAtIndex_(p) mediaBoxSize = page.boundsForBox_(mediabox) trimBoxSize = page.boundsForBox_(trimbox) if not CGRectEqualToRect(mediaBoxSize, trimBoxSize): page.setBounds_forBox_(trimBoxSize, mediabox) hasBeenChanged = True if hasBeenChanged: pdfDoc.writeToFile_(outFilename)
def merge_pdfs(first_pdf_path, second_pdf_path): # build output PDF filename merged_pdf_path = os.path.dirname(first_pdf_path) + '/merged.pdf' merged_pdf = PDFDocument.alloc().init() first_pdf_pages = get_pdf_pages(first_pdf_path) second_pdf_pages = get_pdf_pages(second_pdf_path) if len(first_pdf_pages) != len(second_pdf_pages): raise Exception('Number of pages in PDFs must be equal') for index, page in enumerate( flatten(zip(first_pdf_pages, second_pdf_pages))): merged_pdf.insertPage_atIndex_(page, index) merged_pdf.writeToFile_(merged_pdf_path) return merged_pdf_path
def main(): inputfile = "" outputfile = "" for filename in sys.argv[1:]: inputfile =filename.decode('utf-8') if not inputfile: print 'Unable to open input file' sys.exit(2) shortName = os.path.splitext(filename)[0] outputfile = shortName+" locked.pdf" pdfURL = NSURL.fileURLWithPath_(inputfile) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc : options = { kCGPDFContextAllowsCopying: False, kCGPDFContextAllowsPrinting: False, kCGPDFContextOwnerPassword: password, kCGPDFContextUserPassword: ""} pdfDoc.writeToFile_withOptions_(outputfile, options)
def main(): if len(sys.argv) != 4: print("Usage: {} in.pdf out.pdf \"creator string\"".format(__file__)) sys.exit(1) in_PDF = os.path.expanduser(sys.argv[1]) out_PDF = os.path.expanduser(sys.argv[2]) creator_str = sys.argv[3] fn = os.path.expanduser(in_PDF) url = NSURL.fileURLWithPath_(fn) pdfdoc = PDFDocument.alloc().initWithURL_(url) attrs = (NSMutableDictionary.alloc() .initWithDictionary_(pdfdoc.documentAttributes())) attrs[PDFDocumentCreatorAttribute] = creator_str pdfdoc.setDocumentAttributes_(attrs) pdfdoc.writeToFile_(out_PDF)
def main(argv): inputfile = "" outputfile = "" filter = "" try: opts, args = getopt.getopt(sys.argv[1:], "ifo", ["input", "filter", "output"]) except getopt.GetoptError as err: print(err) usage() sys.exit(2) if len(args) != 3: print("Not enough arguments") sys.exit(2) inputfile = args[0].decode('utf-8') if not inputfile: print 'Unable to open input file' sys.exit(2) filter = args[1].decode('utf-8') filter = checkFilter(filter) if not filter: print 'Unable to find Quartz Filter' sys.exit(2) outputfile = args[2].decode('utf-8') if not outputfile: print 'No valid output file specified' sys.exit(2) # You could just take the inputfile as the outputfile if not explicitly given. # outputfile = inputfile pdfURL = NSURL.fileURLWithPath_(inputfile) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) filterURL = NSURL.fileURLWithPath_(filter) value = QuartzFilter.quartzFilterWithURL_(filterURL) dict = {'QuartzFilter': value} pdfDoc.writeToFile_withOptions_(outputfile, dict)
def main(argv): inputfile = "" outputfile = "" filter = "" try: opts, args = getopt.getopt(sys.argv[1:], "ifo", ["input", "filter", "output"]) except getopt.GetoptError as err: print(err) usage() sys.exit(2) if len(args) != 3: print("Not enough arguments") sys.exit(2) inputfile =args[0].decode('utf-8') if not inputfile: print 'Unable to open input file' sys.exit(2) filter = args[1].decode('utf-8') filter = checkFilter(filter) if not filter: print 'Unable to find Quartz Filter' sys.exit(2) outputfile = args[2].decode('utf-8') if not outputfile: print 'No valid output file specified' sys.exit(2) # You could just take the inputfile as the outputfile if not explicitly given. # outputfile = inputfile pdfURL = NSURL.fileURLWithPath_(inputfile) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) filterURL = NSURL.fileURLWithPath_(filter) value = QuartzFilter.quartzFilterWithURL_(filterURL) dict = { 'QuartzFilter': value } pdfDoc.writeToFile_withOptions_(outputfile, dict)
def pdf_to_keynote(path_to_pdf, path_to_keynote=None): """ """ path_to_pdf = os.path.abspath(path_to_pdf) print("reading", path_to_pdf) if path_to_keynote is None: root, ext = os.path.splitext(path_to_pdf) path_to_keynote = root + ".key" url = NSURL.fileURLWithPath_(_s(path_to_pdf)) pdf = PDFDocument.alloc().initWithURL_(url) if not pdf: exit_usage("'%s' does not seem to be a pdf." % url.path(), 1) if pdf.pageCount() > 0: w, h = get_pdf_dimensions(pdf) print(w, h) create_keynote_document(w, h) for page_number in range(pdf.pageCount()): slide = page_number + 1 if slide > 1: create_empty_slide() pdf_path = create_pdf_for_page(pdf, page_number) insert_image(slide, pdf_path) os.remove(pdf_path) notes = get_beamer_notes_for_page(pdf, page_number) insert_note(slide, notes) process_annotations_for_page(pdf, page_number) print((slide, notes, pdf_path)) # TODO select first slide save_keynote_document(path_to_keynote)
def main(argv): inputfile = "" outputfile = "" filter = "" try: opts, args = getopt.getopt(sys.argv[1:], "fio", ["filter", "input", "output"]) except getopt.GetoptError as err: print(err) usage() sys.exit(2) if len(args) != 3: print("Not enough arguments") sys.exit(2) filter = args[0].decode('utf-8') if not filter: print 'Unable to create context filter' sys.exit(2) inputfile = args[1].decode('utf-8') if not inputfile: print 'Unable to open input file' sys.exit(2) outputfile = args[2].decode('utf-8') if not outputfile: print 'Unable to create output context' sys.exit(2) pdfURL = NSURL.fileURLWithPath_(inputfile) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) filterURL = NSURL.fileURLWithPath_(filter) value = QuartzFilter.quartzFilterWithURL_(filterURL) dict = {'QuartzFilter': value} pdfDoc.writeToFile_withOptions_(outputfile, dict)
def setPDFDocument(self, document): if document is None: document = PDFDocument.alloc().init() self.getNSView().setDocument_(document)
def setPath(self, path): url = NSURL.fileURLWithPath_(path) document = PDFDocument.alloc().initWithURL_(url) self.setPDFDocument(document)
def set(self, pdfData): pdf = PDFDocument.alloc().initWithData_(pdfData) self.setPDFDocument(pdf)
#! /usr/bin/python # -*- coding: utf-8 -*- # Gets PDF metadata for any PDF file provided as an argument # by Ben Byram-Wigfield v1.2 # import sys from Quartz import PDFDocument from Foundation import NSURL if __name__ == '__main__': for filename in sys.argv[1:]: filename = filename.decode('utf-8') pdfURL = NSURL.fileURLWithPath_(filename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: print "URL:", pdfDoc.documentURL() # Might be nice to Unicode this. metadata = pdfDoc.documentAttributes() for key in metadata: print "{}: {}".format(key, metadata[key]) print "Number of Pages:", pdfDoc.pageCount() print "Is Encrypted:", pdfDoc.isEncrypted() print "Is Locked:", pdfDoc.isLocked() print "Allows Copying:", pdfDoc.allowsCopying() print "Allows Printing:", pdfDoc.allowsPrinting() print "Version: {}.{}".format(pdfDoc.majorVersion(),pdfDoc.minorVersion()) else: print "Cannot get this file. (Not a PDF? / Bad filename?)"
def pageCount(pdfPath): # pdfPath = pdfPath.decode('utf-8') pdfURL = NSURL.fileURLWithPath_(pdfPath) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: return pdfDoc.pageCount()
#!/usr/bin/env python3 # GETINFO: Gets PDF metadata for any PDF file(s) provided as an argument # by Ben Byram-Wigfield v1.4 # import sys from Quartz import PDFDocument from Foundation import NSURL if __name__ == '__main__': for filename in sys.argv[1:]: pdfURL = NSURL.fileURLWithPath_(filename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: print("URL:", pdfDoc.documentURL()) # Might be nice to Unicode this. metadata = pdfDoc.documentAttributes() for key in metadata: print("{}: {}".format(key, metadata[key])) print("Number of Pages:", pdfDoc.pageCount()) print("Is Encrypted:", pdfDoc.isEncrypted()) print("Is Locked:", pdfDoc.isLocked()) print("Allows Copying:", pdfDoc.allowsCopying()) print("Allows Printing:", pdfDoc.allowsPrinting()) print("Version: {}.{}".format(pdfDoc.majorVersion(), pdfDoc.minorVersion())) else: print("Cannot get this file. (Not a PDF? / Bad filename?)")
def get_pdf_pages(pdf_path): pdf_nsurl = NSURL.fileURLWithPath_(pdf_path) pdf = PDFDocument.alloc().initWithURL_(pdf_nsurl) page_count = pdf.pageCount() return [pdf.pageAtIndex_(index) for index in range(0, page_count)]