def post(self): data = self.reqparse.parse_args() # sprawdzenie czy zostały zapostowane pliki w base64 lub normalnie if data["pdf1_64"] and data["pdf2_64"]: input1 = io.BytesIO(base64.b64decode(data["pdf1_64"])) input2 = io.BytesIO(base64.b64decode(data["pdf2_64"])) input1.seek(0) input2.seek(0) elif data['pdf1'] and data['pdf2']: # inicjalizacja inputów i przypisanie do nich zawartości postowanych plików input1 = data["pdf1"] input2 = data["pdf2"] else: return { 'status': 'error', 'message': 'No files found' } # inicjalizacja "virtualnego" pliku wyjściowego i mergera PDF output = io.BytesIO() merger = PdfFileMerger(output) # Próbujemy mergować pliki # łączenie, zapisywanie i zamykanie wirtualnego pliku merger.append(input1) merger.append(input2) merger.write(output) merger.close() # przeniesienie kursora na początek wirtualnego pliku output.seek(0) print("Successfully merged") # jeśli output ma być w json i base64 to konwertujemy pdf na base64 if data['type'] == "64": output64 = base64.b64encode(output.getvalue()).decode() # zwracamy pdf jako string w base64 return { 'status': 'success', 'message': output64 } # w przeciwnym razie wywołujemy efekt kliknięcia przycisku "Download" i otworzenie przeglądarkowego okienka zapisu pliku else: return make_response(send_file(output, mimetype="application/pdf"), 200)
def merge(files): """Merge two or more pdfs into one""" # Setting the file name of output output = "PyPDF-Merging-Output.pdf" # Create the merger instance merger = PdfFileMerger(open(output, "wb")) # Opening two input files input1 = open(files[0], "rb") input2 = open(files[1], "rb") # Add the first 3 pages of input1 to output merger.append(fileobj=input1, pages=(0, 3)) # Insert the first page of input2 into the output beginning after the second page merger.merge(position=2, fileobj=input2, pages=(0, 1)) # Append entire input3 document to the end of the output document merger.append(input2) # Output and close the file merger.write(output) print("Output successfully written to", output) merger.close()
def main(what): output = f"{what}/final.pdf" files = sorted([ f"{what}/{zw}" for zw in os.listdir(what) if ".pdf" in zw and not zw in output ]) print("merging", *files) reader1 = PdfFileReader(files[0]) merger = PdfFileMerger() inp = [open(fil, "rb") for fil in files] for zw in inp: merger.append(zw, import_bookmarks=False) merger.write(open(output, "wb")) print("Output successfully written to", output) merger.close()
def merge_pdfs(input_files: list, page_range: tuple, output_file: str, bookmark: bool = True): """ Merge a list of PDF files and save the combined result into the `output_file`. `page_range` to select a range of pages (behaving like Python's range() function) from the input files e.g (0,2) -> First 2 pages e.g (0,6,2) -> pages 1,3,5 bookmark -> add bookmarks to the output file to navigate directly to the input file section within the output file. """ # strict = False -> To ignore PdfReadError - Illegal Character error merger = PdfFileMerger(strict=False) for input_file in input_files: bookmark_name = os.path.splitext( os.path.basename(input_file))[0] if bookmark else None # pages To control which pages are appended from a particular file. merger.append(fileobj=open(input_file, 'rb'), pages=page_range, bookmark=bookmark_name) # Insert the pdf at specific page merger.write(fileobj=open(output_file, 'wb')) merger.close()
def merge_pdfs(self): ''' merge files into a PDF # todo: check file type ''' merged_pdf = PdfFileMerger() for file_path in self.paths_list: file_name, page_range = self._path_decople_(file_path) if file_name.lower().endswith(('.png', '.jpg', '.jpeg')): merged_pdf.append(fileobj=self._image_to_page_(file_name)) else: if page_range: merged_pdf.append(fileobj=file_name, pages=page_range) else: merged_pdf.append(fileobj=file_name) # write to outputfile output = open(self.output_file_path, 'wb') # output file merged_pdf.write(output) # write merge content to file output.close() merged_pdf.close()
for pdf_file in glob.glob(new_pathfiles): f_path, f_name = os.path.split(pdf_file) name, *res = os.path.splitext(f_name) pdfs[int(name)] = pdf_file ranges = [] for i in range(len(file_groups) - 1): r = file_groups[i], file_groups[i + 1] ranges.append(r) groups = {} n = 1 for r in ranges: for i in range(*r): file = pdfs.get(i, 'None') if file != 'None': groups.setdefault(n, []).append(file) n += 1 for group, files in groups.items(): merger = PdfFileMerger() for file in files: merger.append(file) output_path = path + 'merged\\' + str(group) + new_ext merger.write(output_path) merger.close() print('Completed')
def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') # name = req.params.get('name') # pdf1 = req.params.get('pdf1') # pdf2 = req.params.get('pdf2') # if not pdf1 and not pdf2: try: req_body = req.get_json() except ValueError: pass # else: # pdf1 = req_body.get('pdf1') # pdf2 = req_body.get('pdf2') pdf1 = req_body.get('pdf1') pdf2 = req_body.get('pdf2') if pdf1 and pdf2: input1 = io.BytesIO(base64.b64decode(pdf1)) input2 = io.BytesIO(base64.b64decode(pdf2)) # input1 = io.BytesIO(pdf1) # input2 = io.BytesIO(pdf2) input1.seek(0) input2.seek(0) output = io.BytesIO() merger = PdfFileMerger(output) # Próbujemy mergować pliki # łączenie, zapisywanie i zamykanie wirtualnego pliku merger.append(input1) merger.append(input2) merger.write(output) merger.close() # przeniesienie kursora na początek wirtualnego pliku output.seek(0) print("Successfully merged") # output64 = base64.b64encode(output.getvalue()) # output64 = base64.b64encode(output.getvalue()).decode() # b_output = str(output.getvalue())[2:-1] # base_output = str(base64.b64encode(output.getvalue()))[2:-1] # return func.HttpResponse( # json.dumps({ # 'status': 'success', # 'content': base_output # }), # mimetype="application/json", # status_code=200 # ) return func.HttpResponse(output.getvalue(), headers={'Content-Type':'application/pdf'} ) else: return func.HttpResponse( json.dumps({ 'status': 'error', 'content': 'Unable to parse pdf1 and pdf2' }), mimetype="application/json", status_code=400 )
def mergeBrachyPlanFile(ptPdfInfos, ptJpgInfos, mergFolderPath): patient = ptPdfInfos[0].patient print('%s Start creating plan for patient %s.' % (datetime.now().strftime('%m/%d %H:%M'), patient)) mergFilePath = os.path.join(mergFolderPath, patient + '.pdf') if os.path.isfile(mergFilePath): print('%s --The merged file already exists in %s.' % (datetime.now().strftime('%m/%d %H:%M'), mergFilePath)) return findPlnFile = False findDVHFile = False find2ndCalcFile = False findImgFile = False mergPdf = PdfFileMerger(strict=False) # find Oncentra plan pdfInfo = next( (info for info in ptPdfInfos if info.type == 'plandocOncentra'), None) if pdfInfo is not None: plnTime = pdfInfo.printTime findPlnFile = True mergPdf.append(pdfInfo.fileName) if findPlnFile == False: print('%s --Cannot find the plan file.' % datetime.now().strftime('%m/%d %H:%M')) mergPdf.close() return # find Oncentra DVH plan for pdfInfo in ptPdfInfos: if pdfInfo.type == 'DVHOncentra': if abs(plnTime - pdfInfo.printTime) < timedelta(hours=5): findDVHFile = True mergPdf.append(pdfInfo.fileName) break if findDVHFile == False: print('%s --Cannot find the DVH file within 5 hours time frame.' % datetime.now().strftime('%m/%d %H:%M')) mergPdf.close() return # find plan image(s), can merge multiple images for imgInfo in ptJpgInfos: if abs(plnTime - imgInfo.modifiedTime) < timedelta(hours=5): imgFilePath = imgInfo.convertPdf() if imgFilePath: mergPdf.append(imgFilePath) findImgFile = True if findImgFile == False: print('%s --Cannot find the image file within 5 hours time frame.' % datetime.now().strftime('%m/%d %H:%M')) mergPdf.close() return # find 2nd check for pdfInfo in ptPdfInfos: if pdfInfo.type == '2ndchkbrachy': if abs(plnTime - pdfInfo.printTime) < timedelta(hours=24): find2ndCalcFile = True mergPdf.append(pdfInfo.fileName) break if find2ndCalcFile == False: print('%s --Cannot find the 2nd check file within 1 day time frame.' % datetime.now().strftime('%m/%d %H:%M')) mergPdf.close() return try: mergPdf.write(mergFilePath) print('%s --Merged plan completed.' % datetime.now().strftime('%m/%d %H:%M')) mergPdf.close() except: print('%s --Fail to save merged plan file.' % datetime.now().strftime('%m/%d %H:%M')) return