def loadPdfFile(self, filename): pdfFile = PdfFileReader(file(filename, "rb")) numpages = pdfFile.getNumPages() self.treestore.append([filename, numpages])
def export_to_file(self, file_out, only_selected=False): """Export to file""" selection = self.iconview.get_selected_items() pdf_output = PdfFileWriter() pdf_input = [] for pdfdoc in self.pdfqueue: pdfdoc_inp = PdfFileReader(file(pdfdoc.copyname, 'rb')) if pdfdoc_inp.getIsEncrypted(): try: # Workaround for lp:#355479 stat = pdfdoc_inp.decrypt('') except: stat = 0 if (stat!=1): errmsg = _('File %s is encrypted.\n' 'Support for encrypted files has not been implemented yet.\n' 'File export failed.') % pdfdoc.filename raise Exception, errmsg #FIXME #else # ask for password and decrypt file pdf_input.append(pdfdoc_inp) for row in self.model: if only_selected and row.path not in selection: continue # add pages from input to output document nfile = row[2] npage = row[3] if npage == -1 : pdf_output.addBlankPage() continue current_page = copy(pdf_input[nfile-1].getPage(npage-1)) angle = row[6] angle0 = current_page.get("/Rotate",0) crop = [row[7],row[8],row[9],row[10]] if angle != 0: current_page.rotateClockwise(angle) if crop != [0.,0.,0.,0.]: rotate_times = (((angle + angle0) % 360 + 45) / 90) % 4 crop_init = crop if rotate_times != 0: perm = [0,2,1,3] for it in range(rotate_times): perm.append(perm.pop(0)) perm.insert(1,perm.pop(2)) crop = [crop_init[perm[side]] for side in range(4)] #(x1, y1) = current_page.cropBox.lowerLeft #(x2, y2) = current_page.cropBox.upperRight (x1, y1) = [float(xy) for xy in current_page.mediaBox.lowerLeft] (x2, y2) = [float(xy) for xy in current_page.mediaBox.upperRight] x1_new = int(x1 + (x2-x1) * crop[0]) x2_new = int(x2 - (x2-x1) * crop[1]) y1_new = int(y1 + (y2-y1) * crop[3]) y2_new = int(y2 - (y2-y1) * crop[2]) #current_page.cropBox.lowerLeft = (x1_new, y1_new) #current_page.cropBox.upperRight = (x2_new, y2_new) current_page.mediaBox.lowerLeft = (x1_new, y1_new) current_page.mediaBox.upperRight = (x2_new, y2_new) pdf_output.addPage(current_page) # finally, write "output" to document-output.pdf pdf_output.write(file(file_out, 'wb'))