def drawWeatherInfographic(data, topMargin, printCoords=False, doc=doc, docSize=docSize): datesDate = [] datesDisplay = [] datesTemp = [] for item in data: datesDate.append(item['date']) datesTemp.append(item['temp']) if item['precipitation'] == 'rain': dateDisplay = resourcePath('rainy_cloud.png') elif item['precipitation'] == 'snow': dateDisplay = resourcePath('snowy_cloud.png') else: if item['cloudCover'] >= 50: dateDisplay = resourcePath('cloud.png') elif item['cloudCover'] >= 25: dateDisplay = resourcePath('cloudy.png') else: dateDisplay = resourcePath('sun.png') dateDisplay = Image(dateDisplay, (unit * 5), (unit * 5)) datesDisplay.append(dateDisplay) datesDate = convertDates(datesDate, '{day:02d}/{month:02d}/{year}') weatherInfographic = Table([datesTemp, datesDisplay, datesDate]) weatherInfographic.setStyle( TableStyle([('ALIGN', (0, 0), (-1, -1), 'CENTER'), ('FONTSIZE', (0, 0), (-1, 0), (unit * 1.25)), ('FONTSIZE', (0, 2), (-1, 2), unit), ('TOPPADDING', (0, 1), (-1, 1), unit), ('BOTTOMPADDING', (0, 1), (-1, 1), unit)])) weatherInfographic.canv = doc infographicWidth, infographicHeight = weatherInfographic.wrap(0, 0) xAxis = (docSize['width'] - infographicWidth) / 2 yAxis = docSize['height'] - infographicHeight - topMargin weatherInfographic.drawOn(doc, xAxis, yAxis) coords = [{'x': xAxis, 'y': docSize['height'] - yAxis}] if printCoords: print(coords) return coords
def receive_journal(request): """Печать истории принятия материала за день""" user = request.user.doctorprofile # Профиль текущего пользователя from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from reportlab.platypus import Table, TableStyle from reportlab.lib import colors from reportlab.platypus import Paragraph from reportlab.lib.styles import getSampleStyleSheet styleSheet = getSampleStyleSheet() import os.path import collections import pytz import copy start = str(request.GET.get("start", "1")) group = str(request.GET.get("group", "-2")) return_type = str(request.GET.get("return", "pdf")) otd = str(request.GET.get("otd", "[]")) start = 1 if not start.isdigit() else int(start) group = -2 if group not in ["-2", "-1"] and ( not group.isdigit() or not directory.ResearchGroup.objects.filter( pk=int(group)).exists()) else int(group) otd = [int(x) for x in json.loads(otd)] PROJECT_ROOT = os.path.abspath( os.path.dirname(__file__)) # Директория Django pdfmetrics.registerFont( TTFont('OpenSans', PROJECT_ROOT + '/../static/fonts/OpenSans.ttf')) # Загрузка шрифта response = HttpResponse( content_type='application/pdf') # Формирование ответа response[ 'Content-Disposition'] = 'inline; filename="zhurnal_priema_materiala.pdf"' # Content-Disposition inline для показа PDF в браузере from io import BytesIO buffer = BytesIO() # Буфер from reportlab.pdfgen import canvas c = canvas.Canvas(buffer, pagesize=A4) # Холст tubes = TubesRegistration.objects.filter( issledovaniya__research__subgroup__podrazdeleniye=request.user. doctorprofile.podrazileniye, time_recive__gte=datetime.now().date(), doc_get__podrazileniye__pk__in=otd, doc_recive__isnull=False).order_by( 'issledovaniya__napravleniye__client__pk') local_tz = pytz.timezone(settings.TIME_ZONE) # Локальная временная зона labs = {} # Словарь с пробирками, сгруппироваными по лаборатории directions = set() vids = set() perpage = 47 n_dict = {} n = 1 for v in tubes: # Перебор пробирок idv = v.id if idv in vids: continue vids.add(idv) iss = Issledovaniya.objects.filter( tubes__id=v.id) # Получение исследований для пробирки if group == -1: iss = iss.filter(research__groups__isnull=True) elif group >= 0: iss = iss.filter(research__groups__pk=group) iss_list = collections.OrderedDict() # Список исследований k = "_" if v.doc_get: k = str(v.doc_get.podrazileniye.pk) + "@" + str( v.doc_get.podrazileniye) else: k = str(iss.first().napravleniye.doc.podrazileniye.pk) + "@" + str( iss.first().napravleniye.doc.podrazileniye) if k not in n_dict.keys(): n_dict[k] = 0 for val in iss.order_by( "research__sort_weight" ): # Цикл перевода полученных исследований в список iss_list[val.research.sort_weight] = val.research.title if len(iss_list) == 0: continue ''' if n < start: n += 1 continue''' directions.add(iss[0].napravleniye.pk) if return_type == "pdf": n_dict[k] += 1 if n_dict[k] >= start: if k not in labs.keys( ): # Добавление списка в словарь если по ключу k нету ничего в словаре labs labs[k] = [] if perpage - len(labs[k]) % perpage < len(iss_list): pre = copy.deepcopy(labs[k][len(labs[k]) - 1]) pre["researches"] = "" for x in range(0, perpage - len(labs[k]) % perpage): labs[k].append(pre) for value in iss_list: # Перебор списка исследований labs[k].append( { "type": v.type.tube.title, "researches": iss_list[value], "client-type": iss[0].napravleniye.client.type, "lab_title": iss[0].research.subgroup.title, "time": "" if not v.time_recive else v.time_recive. astimezone(local_tz).strftime("%H:%M:%S"), "dir_id": iss[0].napravleniye.pk, "podr": iss[0].napravleniye.doc.podrazileniye.title, "receive_n": str(n), "tube_id": str(v.id), "direction": str(iss[0].napravleniye.pk), "history_num": iss[0].napravleniye.history_num, "n": n_dict[k], "fio": iss[0].napravleniye.client.shortfio() } ) # Добавление в список исследований и пробирок по ключу k в словарь labs n += 1 directions = list(directions) if return_type == "directions": return HttpResponse(json.dumps(directions), content_type="application/json") labs = collections.OrderedDict(sorted(labs.items())) # Сортировка словаря c.setFont('OpenSans', 20) paddingx = 17 data_header = [ "№", "ФИО, № истории", "№ Напр", "№ емкости", "Тип емкости", "Наименования исследований" ] tw = w - paddingx * 3.5 tx = paddingx * 2 ty = 90 c.setFont('OpenSans', 9) styleSheet["BodyText"].fontName = "OpenSans" styleSheet["BodyText"].fontSize = 7 doc_num = 0 for key in labs: doc_num += 1 p = Paginator(labs[key], perpage) i = 0 if doc_num >= 2: c.showPage() nn = 0 gid = "-1" for pg_num in p.page_range: pg = p.page(pg_num) if len(pg) == 0: continue if pg_num >= 0: drawTituls(c, user, p.num_pages, pg_num, paddingx, pg[0], group=group, otd=key.split("@")[1], start=start) data = [] tmp = [] for v in data_header: tmp.append(Paragraph(str(v), styleSheet["BodyText"])) data.append(tmp) merge_list = {} num = 0 lastid = "-1" for obj in pg.object_list: tmp = [] if lastid != obj["tube_id"]: if gid != obj["tube_id"]: i += 1 lastid = gid = obj["tube_id"] shownum = True else: shownum = False if lastid not in merge_list.keys(): merge_list[lastid] = [] merge_list[lastid].append(num) if shownum: nn += 1 tmp.append( Paragraph(str(obj["n"]), styleSheet["BodyText"]) ) # "--" if obj["receive_n"] == "0" else obj["receive_n"], styleSheet["BodyText"])) fio = obj["fio"] if obj["history_num"] and len(obj["history_num"]) > 0: fio += ", " + obj["history_num"] tmp.append(Paragraph(fio, styleSheet["BodyText"])) tmp.append( Paragraph(obj["direction"], styleSheet["BodyText"])) tmp.append( Paragraph(obj["tube_id"], styleSheet["BodyText"])) tmp.append(Paragraph(obj["type"], styleSheet["BodyText"])) else: tmp.append("") tmp.append("") tmp.append("") tmp.append("") tmp.append("") research_tmp = obj["researches"] if len(research_tmp) > 44: research_tmp = research_tmp[0:-(len(research_tmp) - 44)] + "..." tmp.append(Paragraph(research_tmp, styleSheet["BodyText"])) data.append(tmp) num += 1 style = TableStyle([('TEXTCOLOR', (0, -1), (-1, -1), colors.black), ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black), ('BOX', (0, 0), (-1, -1), 0.25, colors.black), ('VALIGN', (0, 0), (-1, -1), "MIDDLE"), ('LEFTPADDING', (0, 0), (-1, -1), 1), ('RIGHTPADDING', (0, 0), (-1, -1), 1), ('TOPPADDING', (0, 0), (-1, -1), 1), ('BOTTOMPADDING', (0, 0), (-1, -1), 1)]) for span in merge_list: # Цикл объединения ячеек for pos in range(0, 6): style.add( 'INNERGRID', (pos, merge_list[span][0]), (pos, merge_list[span][0] + len(merge_list[span])), 0.28, colors.white) style.add( 'BOX', (pos, merge_list[span][0]), (pos, merge_list[span][0] + len(merge_list[span])), 0.2, colors.black) t = Table(data, colWidths=[ int(tw * 0.03), int(tw * 0.23), int(tw * 0.09), int(tw * 0.09), int(tw * 0.23), int(tw * 0.35) ], style=style) t.canv = c wt, ht = t.wrap(0, 0) t.drawOn(c, tx, h - ht - ty) if pg.has_next(): c.showPage() c.setTitle("Журнал приема материала") c.save() pdf = buffer.getvalue() buffer.close() response.write(pdf) group_str = "Все исследования" if group >= 0: group_str = directory.ResearchGroup.objects.get(pk=group).title elif group == -2: group_str = "Все исследования" else: group_str = "Без группы" slog.Log(key="", type=25, body=json.dumps({ "group": group_str, "start": start, "otds": [ "%s, %s" % (users.Podrazdeleniya.objects.get(pk=int(x)).title, x) for x in otd ] }), user=request.user.doctorprofile).save() return response
def receive_execlist(request): import datetime import directory.models as directory from reportlab.graphics import renderPDF from reportlab.lib.pagesizes import A4 from reportlab.pdfbase import pdfdoc from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import landscape from reportlab.pdfbase import pdfdoc from django.core.paginator import Paginator from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from reportlab.lib.units import mm import os.path from io import BytesIO from django.utils import timezone, datetime_safe from reportlab.lib.styles import getSampleStyleSheet from reportlab.platypus import Table, TableStyle from reportlab.lib import colors from reportlab.platypus import Paragraph PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__)) pdfmetrics.registerFont( TTFont('OpenSans', PROJECT_ROOT + '/../static/fonts/OpenSans.ttf')) pdfmetrics.registerFont( TTFont('OpenSansB', PROJECT_ROOT + '/../static/fonts/OpenSans-Bold.ttf')) w, h = landscape(A4) response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'inline; filename="execlist.pdf"' pdfdoc.PDFCatalog.OpenAction = '<</S/JavaScript/JS(this.print\({bUI:true,bSilent:false,bShrinkToFit:true}\);)>>' date = request.GET["date"] date = datetime.date(int(date.split(".")[2]), int(date.split(".")[1]), int(date.split(".")[0])) researches = json.loads(request.GET["researches"]) buffer = BytesIO() c = canvas.Canvas(buffer, pagesize=landscape(A4)) marginx = 15 * mm marginy = 10 * mm pw = w - marginx * 2 ph = h - marginy * 2 from datetime import timedelta date1 = date date2 = date1 + timedelta(days=1) def py(y=0): y *= mm return h - y - marginy def px(x=0): x *= mm return x + marginx def pxc(x=0): x *= mm return w / 2 + x def pxr(x=0): x *= mm return pw - x + marginx for pk in researches: if directory.Researches.objects.filter( pk=pk).exists() and Issledovaniya.objects.filter( research__pk=pk, tubes__time_recive__range=(date1, date2), research__subgroup__podrazdeleniye=request.user. doctorprofile.podrazileniye).exists(): research = directory.Researches.objects.get(pk=pk) fractions = [ x.title for x in directory.Fractions.objects.filter( research=research).order_by("sort_weight") ] tubes = [ x.pk for x in TubesRegistration.objects.filter( time_recive__range=(date1, date2), doc_recive=request.user.doctorprofile, issledovaniya__research=research).order_by("daynum") ] pages = Paginator(tubes, 16) for pg_num in pages.page_range: c.setFont('OpenSans', 12) c.drawString( px(), py(), "Лист исполнения - %s за %s" % (research.title, date1.strftime("%d.%m.%Y"))) c.drawRightString(pxr(), py(), research.subgroup.podrazdeleniye.title) c.drawString(px(), 6 * mm, "Страница %d из %d" % (pg_num, pages.num_pages)) styleSheet = getSampleStyleSheet() tw = pw data = [] tmp = [] tmp.append( Paragraph('<font face="OpenSansB" size="8">№</font>', styleSheet["BodyText"])) tmp.append( Paragraph( '<font face="OpenSansB" size="8">ФИО, № истории<br/>отделение</font>', styleSheet["BodyText"])) tmp.append( Paragraph('<font face="OpenSansB" size="8">№ мат.</font>', styleSheet["BodyText"])) for fraction in fractions: fraction = fraction[:int(100 / len(fractions))] tmp.append( Paragraph( '<font face="OpenSansB" size="6">%s</font>' % fraction, styleSheet["BodyText"])) data.append(tmp) pg = pages.page(pg_num) for tube_pk in pg.object_list: tube = TubesRegistration.objects.get(pk=tube_pk) napravleniye = Issledovaniya.objects.filter( tubes__pk=tube_pk).first().napravleniye tmp = [] tmp.append( Paragraph( '<font face="OpenSans" size="8">%d</font>' % (tube.daynum), styleSheet["BodyText"])) tmp.append( Paragraph( '<font face="OpenSans" size="8">%s</font>' % (napravleniye.client.fio() + ("" if not napravleniye.history_num or napravleniye.history_num == "" else ", " + napravleniye.history_num) + "<br/>" + napravleniye.doc.podrazileniye.title), styleSheet["BodyText"])) tmp.append( Paragraph( '<font face="OpenSans" size="8">%d</font>' % (tube_pk), styleSheet["BodyText"])) for _ in fractions: tmp.append( Paragraph('<font face="OpenSans" size="8"></font>', styleSheet["BodyText"])) data.append(tmp) cw = [int(tw * 0.07), int(tw * 0.245), int(tw * 0.045)] lw = tw * 0.67 for _ in range(0, len(fractions) + 1): cw.append(lw / len(fractions)) t = Table(data, colWidths=cw) t.setStyle( TableStyle([ ('ALIGN', (0, 0), (-1, -1), 'CENTER'), ('VALIGN', (0, 0), (-1, -1), 'TOP'), ('TEXTCOLOR', (0, -1), (-1, -1), colors.black), ('INNERGRID', (0, 0), (-1, -1), 0.8, colors.black), ('BOX', (0, 0), (-1, -1), 0.8, colors.black), ('LEFTPADDING', (0, 0), (-1, -1), 2), ('TOPPADDING', (0, 0), (-1, -1), 9), ('TOPPADDING', (1, 0), (1, -1), 3), ('RIGHTPADDING', (0, 0), (-1, -1), 1), ('BOTTOMPADDING', (0, 0), (-1, -1), 9), ('BOTTOMPADDING', (1, 0), (1, -1), 3), ])) t.canv = c wt, ht = t.wrap(0, 0) t.drawOn(c, px(), py(5) - ht) c.showPage() c.save() pdf = buffer.getvalue() buffer.close() response.write(pdf) return response
def receive_execlist(request): import datetime import directory.models as directory from reportlab.lib.pagesizes import A4 from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import landscape from reportlab.pdfbase import pdfdoc from django.core.paginator import Paginator from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from reportlab.lib.units import mm import os.path from io import BytesIO from reportlab.lib.styles import getSampleStyleSheet from reportlab.platypus import Table, TableStyle from reportlab.lib import colors from reportlab.platypus import Paragraph pdfmetrics.registerFont( TTFont('OpenSans', os.path.join(FONTS_FOLDER, 'OpenSans.ttf'))) pdfmetrics.registerFont( TTFont('OpenSansB', os.path.join(FONTS_FOLDER, 'OpenSans-Bold.ttf'))) w, h = landscape(A4) response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'inline; filename="execlist.pdf"' pdfdoc.PDFCatalog.OpenAction = '<</S/JavaScript/JS(this.print\({bUI:true,bSilent:false,bShrinkToFit:true}\);)>>' t = request.GET.get("type", "received") researches = json.loads(request.GET["researches"]) buffer = BytesIO() c = canvas.Canvas(buffer, pagesize=landscape(A4)) marginx = 15 * mm marginy = 10 * mm pw = w - marginx * 2 from datetime import timedelta if t == "received": date = request.GET["date"].split(".") date = datetime.date(int(date[2]), int(date[1]), int(date[0])) date1 = date date2 = date1 + timedelta(days=1) dates = request.GET["date"] else: date1 = request.GET["datestart"].split(".") date1 = datetime.date(int(date1[2]), int(date1[1]), int(date1[0])) date2 = request.GET["dateend"].split(".") date2 = datetime.date(int(date2[2]), int(date2[1]), int( date2[0])) + timedelta(days=1) if request.GET["datestart"] != request.GET["dateend"]: dates = "{} - {}".format(request.GET["datestart"], request.GET["dateend"]) else: dates = request.GET["datestart"] def py(y=0): y *= mm return h - y - marginy def px(x=0): x *= mm return x + marginx def pxr(x=0): x *= mm return pw - x + marginx for pk in researches: if directory.Researches.objects.filter( pk=pk).exists() and (Issledovaniya.objects.filter( research__pk=pk, tubes__time_recive__range=(date1, date2), time_confirmation__isnull=True).filter( Q(napravleniye__hospital=request.user.doctorprofile. hospital) | Q(napravleniye__hospital__isnull=True)).exists()): research = directory.Researches.objects.get(pk=pk) fractions_o = directory.Fractions.objects.filter( research=research, hide=False).order_by("sort_weight") fractions = [x.title for x in fractions_o] if t == "received": tubes = [ x.pk for x in TubesRegistration.objects.filter( time_recive__range=(date1, date2), doc_recive=request.user.doctorprofile, issledovaniya__research=research).order_by( "daynum").distinct() ] else: tubes = [ x.pk for x in (TubesRegistration.objects.filter( time_recive__range=(date1, date2), issledovaniya__time_confirmation__isnull=True, issledovaniya__research=research ).filter( Q(issledovaniya__napravleniye__hospital=request.user. doctorprofile.hospital) | Q(issledovaniya__napravleniye__hospital__isnull=True) ).order_by( "issledovaniya__napravleniye__client__individual__family", "issledovaniya__napravleniye__client__individual__name" ).distinct()) ] pages = Paginator(tubes, 16) nn = 0 for pg_num in pages.page_range: c.setFont('OpenSans', 12) c.drawString( px(), py(), "Лист исполнения - %s за %s" % (research.title, dates)) c.drawRightString(pxr(), py(), research.get_podrazdeleniye().title) c.drawString(px(), 6 * mm, "Страница %d из %d" % (pg_num, pages.num_pages)) styleSheet = getSampleStyleSheet() tw = pw data = [] tmp = [ Paragraph('<font face="OpenSansB" size="8">№</font>', styleSheet["BodyText"]), Paragraph( '<font face="OpenSansB" size="8">ФИО, № истории<br/>отделение</font>', styleSheet["BodyText"]), Paragraph('<font face="OpenSansB" size="8">№ напр.</font>', styleSheet["BodyText"]), Paragraph('<font face="OpenSansB" size="8">№ мат.</font>', styleSheet["BodyText"]), ] for fraction in fractions: fraction = fraction[:int(100 / len(fractions))] tmp.append( Paragraph( '<font face="OpenSansB" size="6">%s</font>' % fraction, styleSheet["BodyText"])) data.append(tmp) pg = pages.page(pg_num) for tube_pk in pg.object_list: nn += 1 tube = TubesRegistration.objects.get(pk=tube_pk) napravleniye = Issledovaniya.objects.filter( tubes__pk=tube_pk)[0].napravleniye tmp = [ Paragraph( '<font face="OpenSans" size="8">%d</font>' % (tube.daynum if t == "received" else nn), styleSheet["BodyText"]), Paragraph( '<font face="OpenSans" size="8">%s</font>' % (napravleniye.client.individual.fio() + ("" if not napravleniye.history_num or napravleniye.history_num == "" else ", " + napravleniye.history_num) + "<br/>" + napravleniye.doc.podrazdeleniye.title), styleSheet["BodyText"], ), Paragraph( '<font face="OpenSans" size="8">%d</font>' % napravleniye.pk, styleSheet["BodyText"]), Paragraph( '<font face="OpenSans" size="8">%d</font>' % tube_pk, styleSheet["BodyText"]), ] for f in fractions_o: res = Result.objects.filter( fraction=f, issledovaniye__napravleniye=napravleniye) tmp.append( Paragraph( '<font face="OpenSans" size="8">{}</font>'. format("" if t == "received" or not res.exists() else res[0].value), styleSheet["BodyText"])) data.append(tmp) cw = [ int(tw * 0.07), int(tw * 0.245), int(tw * 0.055), int(tw * 0.045) ] lw = tw * 0.615 for _ in range(0, len(fractions) + 1): cw.append(lw / len(fractions)) t = Table(data, colWidths=cw) t.setStyle( TableStyle([ ('ALIGN', (0, 0), (-1, -1), 'CENTER'), ('VALIGN', (0, 0), (-1, -1), 'TOP'), ('TEXTCOLOR', (0, -1), (-1, -1), colors.black), ('INNERGRID', (0, 0), (-1, -1), 0.8, colors.black), ('BOX', (0, 0), (-1, -1), 0.8, colors.black), ('LEFTPADDING', (0, 0), (-1, -1), 2), ('TOPPADDING', (0, 0), (-1, -1), 9), ('TOPPADDING', (1, 0), (1, -1), 3), ('RIGHTPADDING', (0, 0), (-1, -1), 1), ('BOTTOMPADDING', (0, 0), (-1, -1), 9), ('BOTTOMPADDING', (1, 0), (1, -1), 3), ])) t.canv = c wt, ht = t.wrap(0, 0) t.drawOn(c, px(), py(5) - ht) c.showPage() c.save() pdf = buffer.getvalue() buffer.close() response.write(pdf) return response
def print_history(request): """Печать истории забора материала за день""" from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont from reportlab.platypus import Table, TableStyle from reportlab.lib import colors from reportlab.platypus import Paragraph from reportlab.lib.styles import getSampleStyleSheet styleSheet = getSampleStyleSheet() import os.path import collections filter = False filterArray = [] if "filter" in request.GET.keys(): filter = True filterArray = json.loads(request.GET["filter"]) pdfmetrics.registerFont( TTFont('OpenSans', os.path.join(FONTS_FOLDER, 'OpenSans.ttf'))) # Загрузка шрифта response = HttpResponse( content_type='application/pdf') # Формирование ответа response[ 'Content-Disposition'] = 'inline; filename="napr.pdf"' # Content-Disposition inline для показа PDF в браузере buffer = BytesIO() # Буфер c = canvas.Canvas(buffer, pagesize=A4) # Холст tubes = [] if not filter: tubes = TubesRegistration.objects.filter( doc_get=request.user.doctorprofile).order_by('time_get').exclude( time_get__lt=datetime.now().date()) else: for v in filterArray: tubes.append(TubesRegistration.objects.get(pk=v)) labs = {} # Словарь с пробирками, сгруппироваными по лаборатории for v in tubes: # Перебор пробирок iss = Issledovaniya.objects.filter( tubes__id=v.id) # Получение исследований для пробирки iss_list = [] # Список исследований k = v.doc_get.podrazdeleniye.title + "@" + str( iss[0].research.get_podrazdeleniye().title) for val in iss: # Цикл перевода полученных исследований в список iss_list.append(val.research.title) if k not in labs.keys( ): # Добавление списка в словарь если по ключу k нету ничего в словаре labs labs[k] = [] for value in iss_list: # Перебор списка исследований labs[k].append( { "type": v.type.tube.title, "researches": value, "client-type": iss[0].napravleniye.client.base.short_title, "lab_title": iss[0].research.get_podrazdeleniye().title, "time": strtime(v.time_get), "dir_id": iss[0].napravleniye_id, "podr": v.doc_get.podrazdeleniye.title, "reciver": None, "tube_id": str(v.id), "history_num": iss[0].napravleniye.history_num, "fio": iss[0].napravleniye.client.individual.fio(short=True, dots=True), } ) # Добавление в список исследований и пробирок по ключу k в словарь labs labs = collections.OrderedDict(sorted(labs.items())) # Сортировка словаря c.setFont('OpenSans', 20) paddingx = 17 data_header = [ "№", "ФИО, № истории", "№ емкости", "Тип емкости", "Наименования исследований", "Емкость не принята (замечания)" ] tw = w - paddingx * 4.5 tx = paddingx * 3 ty = 90 c.setFont('OpenSans', 9) styleSheet["BodyText"].fontName = "OpenSans" styleSheet["BodyText"].fontSize = 7 doc_num = 0 for key in labs: doc_num += 1 p = Paginator(labs[key], 47) i = 0 if doc_num >= 2: c.showPage() for pg_num in p.page_range: pg = p.page(pg_num) if pg_num >= 0: draw_tituls(c, p.num_pages, pg_num, paddingx, pg[0], request.user.doctorprofile.hospital_safe_title) data = [] tmp = [] for v in data_header: tmp.append(Paragraph(str(v), styleSheet["BodyText"])) data.append(tmp) merge_list = {} num = 0 lastid = "-1" for obj in pg.object_list: tmp = [] if lastid != obj["tube_id"]: i += 1 lastid = obj["tube_id"] shownum = True else: shownum = False if lastid not in merge_list.keys(): merge_list[lastid] = [] merge_list[lastid].append(num) if shownum: tmp.append(Paragraph(str(i), styleSheet["BodyText"])) fio = obj["fio"] if obj["history_num"] and len(obj["history_num"]) > 0: fio += ", " + obj["history_num"] tmp.append(Paragraph(fio, styleSheet["BodyText"])) tmp.append( Paragraph(obj["tube_id"], styleSheet["BodyText"])) tmp.append(Paragraph(obj["type"], styleSheet["BodyText"])) else: tmp.append("") tmp.append("") tmp.append("") tmp.append("") research_tmp = obj["researches"] if len(research_tmp) > 38: research_tmp = research_tmp[0:-(len(research_tmp) - 38)] + "..." tmp.append(Paragraph(research_tmp, styleSheet["BodyText"])) tmp.append(Paragraph("", styleSheet["BodyText"])) data.append(tmp) num += 1 style = TableStyle([ ('TEXTCOLOR', (0, -1), (-1, -1), colors.black), ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black), ('BOX', (0, 0), (-1, -1), 0.25, colors.black), ('VALIGN', (0, 0), (-1, -1), "MIDDLE"), ('LEFTPADDING', (0, 0), (-1, -1), 1), ('RIGHTPADDING', (0, 0), (-1, -1), 1), ('TOPPADDING', (0, 0), (-1, -1), 1), ('BOTTOMPADDING', (0, 0), (-1, -1), 1), ]) for span in merge_list: # Цикл объединения ячеек for pos in range(0, 6): style.add( 'INNERGRID', (pos, merge_list[span][0]), (pos, merge_list[span][0] + len(merge_list[span])), 0.28, colors.white) style.add( 'BOX', (pos, merge_list[span][0]), (pos, merge_list[span][0] + len(merge_list[span])), 0.2, colors.black) t = Table(data, colWidths=[ int(tw * 0.03), int(tw * 0.23), int(tw * 0.08), int(tw * 0.23), int(tw * 0.31), int(tw * 0.14) ], style=style) t.canv = c wt, ht = t.wrap(0, 0) t.drawOn(c, tx, h - ht - ty) if pg.has_next(): c.showPage() c.save() pdf = buffer.getvalue() buffer.close() response.write(pdf) slog.Log(key="", type=10, body="", user=request.user.doctorprofile).save() return response
def print_direction(c: Canvas, n, dir: Napravleniya, format_a6: bool = False): xn, yn = 0, 0 if not format_a6: if n % 2 != 0: xn = 1 if n > 2: yn = 1 barcode = eanbc.Ean13BarcodeWidget(dir.pk + 460000000000, humanReadable=0, barHeight=17) bounds = barcode.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(width, height) d.add(barcode) paddingx = 15 ac = dir.is_all_confirm() canc = dir.cancel visit = dir.visit_date is not None if ac or canc or visit: c.saveState() c.setFillColorRGB(0, 0, 0, 0.2) c.rotate(45) if xn == 0 and yn == 1: ox = w / 2 + 40 * mm oy = h / 2 - 30 * mm elif xn == 0 and yn == 0: ox = w / 2 - 65 * mm oy = 13.5 * mm elif xn == 1 and yn == 0: ox = w - 95.75 * mm oy = 13.5 * mm - h / 4 else: ox = w + 9.25 * mm oy = h / 2 - 30 * mm - h / 4 c.setFont('OpenSansBold', 50) s = 'ОТМЕНЕНО' if ac: s = 'ИСПОЛНЕНО' elif visit: s = 'ПОСЕЩЕНО' c.drawString(ox, oy, s) c.restoreState() c.setFont('OpenSans', 10) c.drawCentredString(w / 2 - w / 4 + (w / 2 * xn), (h / 2 - height - 5) + (h / 2) * yn, dir.hospital_short_title) c.setFont('OpenSans', 8) c.drawCentredString( w / 2 - w / 4 + (w / 2 * xn), (h / 2 - height - 15) + (h / 2) * yn, "(%s. %s)" % (dir.hospital_address, dir.hospital_phones)) c.setFont('OpenSans', 14) c.drawCentredString( w / 2 - w / 4 + (w / 2 * xn), (h / 2 - height - 30) + (h / 2) * yn, "Направление" + ("" if not dir.imported_from_rmis else " из РМИС")) renderPDF.draw(d, c, w / 2 - width + (w / 2 * xn) - paddingx / 3 - 5 * mm, (h / 2 - height - 57) + (h / 2) * yn) c.setFont('OpenSans', 20) c.drawString(paddingx + (w / 2 * xn), (h / 2 - height) + (h / 2) * yn - 57, "№ " + str(dir.pk)) # Номер направления c.setFont('OpenSans', 9) c.drawString( paddingx + (w / 2 * xn), (h / 2 - height - 70) + (h / 2) * yn, "Создано: " + strdate(dir.data_sozdaniya) + " " + strtime(dir.data_sozdaniya)[:5]) history_num = dir.history_num additional_num = dir.additional_num if history_num and len(history_num) > 0: c.drawRightString(w / 2 * (xn + 1) - paddingx, (h / 2 - height - 70) + (h / 2) * yn, "№ истории: " + history_num) elif additional_num and len(additional_num) > 0: c.drawRightString(w / 2 * (xn + 1) - paddingx, (h / 2 - height - 70) + (h / 2) * yn, f"({str(additional_num).strip()})") elif dir.client.number_poliklinika and len( dir.client.number_poliklinika) > 0: c.drawRightString(w / 2 * (xn + 1) - paddingx, (h / 2 - height - 70) + (h / 2) * yn, f"({str(dir.client.number_poliklinika).strip()})") if dir.history_num and len(dir.history_num) > 0: c.drawRightString(w / 2 * (xn + 1) - paddingx, (h / 2 - height - 70) + (h / 2) * yn, "№ истории: " + dir.history_num) c.drawString(paddingx + (w / 2 * xn), (h / 2 - height - 80) + (h / 2) * yn, "ФИО: " + dir.client.individual.fio()) c.drawRightString(w / 2 * (xn + 1) - paddingx, (h / 2 - height - 80) + (h / 2) * yn, "Пол: " + dir.client.individual.sex) c.drawRightString( w / 2 * (xn + 1) - paddingx, (h / 2 - height - 90) + (h / 2) * yn, "Д/р: {} ({})".format(dir.client.individual.bd(), dir.client.individual.age_s(direction=dir))) c.drawString( paddingx + (w / 2 * xn), (h / 2 - height - 90) + (h / 2) * yn, "{}: {}".format("ID" if dir.client.base.is_rmis else "Номер карты", dir.client.number_with_type())) diagnosis = dir.diagnos.strip()[:35] if not dir.imported_from_rmis: if diagnosis != "": c.drawString( paddingx + (w / 2 * xn), (h / 2 - height - 100) + (h / 2) * yn, ("" if dir.vich_code == "" else ("Код: " + dir.vich_code + " ")) + "Диагноз (МКБ 10): " + ("не указан" if diagnosis == "-" else diagnosis), ) elif dir.vich_code != "": c.drawString(paddingx + (w / 2 * xn), (h / 2 - height - 100) + (h / 2) * yn, "Код: " + dir.vich_code) if dir.istochnik_f: c.drawString( paddingx + (w / 2 * xn), (h / 2 - height - 110) + (h / 2) * yn, "Источник финансирования: " + dir.client.base.title + " - " + dir.istochnik_f.title) else: c.drawString(paddingx + (w / 2 * xn), (h / 2 - height - 110) + (h / 2) * yn, "Источник финансирования: ") else: nds = 0 if diagnosis != "": c.drawString(paddingx + (w / 2 * xn), (h / 2 - height - 100) + (h / 2) * yn, "Диагноз (МКБ 10): " + diagnosis) nds = 5 if dir.imported_org: c.drawString(paddingx + (w / 2 * xn), (h / 2 - height - 105 - nds) + (h / 2) * yn, "Организация: " + dir.imported_org.title) issledovaniya = dir.issledovaniya_set.all() vid = [] has_descriptive = False has_doc_refferal = False need_qr_code = False for i in issledovaniya: rtp = i.research.reversed_type if rtp < -1: has_doc_refferal = True rt = { -2: 'Консультации', -3: 'Лечение', -4: 'Стоматология', -5: 'Стационар', -6: 'Микробиология', -9998: 'Морфология', -9: 'Формы', -11: 'Заявления', -12: 'Мониторинги', }[rtp] # if rtp == -6: # has_micro = True else: rt = i.research.podrazdeleniye.get_title() if rt not in vid: vid.append(rt) if i.research.podrazdeleniye and i.research.podrazdeleniye.p_type == Podrazdeleniya.PARACLINIC: has_descriptive = True if i.research.podrazdeleniye.can_has_pacs: need_qr_code = True c.drawString(paddingx + (w / 2 * xn), (h / 2 - height - 120) + (h / 2) * yn, "Вид: " + ", ".join(vid)) if dir.purpose: c.drawRightString(w / 2 * (xn + 1) - paddingx, (h / 2 - height - 120) + (h / 2) * yn, "Цель: " + dir.get_purpose_display()) if dir.external_organization: c.drawRightString(w / 2 * (xn + 1) - paddingx, (h / 2 - height - 134) + (h / 2) * yn, dir.external_organization.title) if dir.parent and dir.parent.research.is_hospital: c.setFont('OpenSansBold', 8) c.drawRightString(w / 2 * (xn + 1) - paddingx, (h / 2 - height - 129) + (h / 2) * yn, ("Стационар-" + str(dir.parent.napravleniye_id))) c.setFont('OpenSans', 9) styleSheet = getSampleStyleSheet() all_iss = issledovaniya.count() max_f = 9 min_f = 7 max_res = 36 max_off = max_f - min_f font_size = max_f - (max_off * (all_iss / max_res)) styleSheet["BodyText"].leading = font_size + 0.5 data = [] values = [] service_locations = {} n = 0 for v in issledovaniya: n += 1 service_location_title = "" if not v.service_location else v.service_location.title if service_location_title: if service_location_title not in service_locations: service_locations[service_location_title] = [] service_locations[service_location_title].append(n) values.append({ "title": v.research.get_title(), "full_title": v.research.title, "sw": v.research.sort_weight, "count": v.how_many, "comment": v.localization.title if v.localization else v.comment, "n": n, "g": -1 if not v.research.fractions_set.exists() else v.research.fractions_set.first().relation_id, "info": v.research.paraclinic_info, "hospital_department_replaced_title": v.hospital_department_replaced_title, }) one_sl = len(service_locations) <= 1 tw = w / 2 - paddingx * 2 m = 0 ns = {} if has_descriptive or has_doc_refferal: tmp = [ Paragraph( '<font face="OpenSansBold" size="8">%s</font>' % ("Исследование" if not has_doc_refferal else "Назначение"), styleSheet["BodyText"]), Paragraph('<font face="OpenSansBold" size="8">Информация</font>', styleSheet["BodyText"]), ] data.append(tmp) colWidths = [int(tw * 0.5), int(tw * 0.5)] values.sort(key=lambda l: l["full_title"]) for v in values: ns[v["n"]] = v["n"] tmp = [ Paragraph( '<font face="OpenSans" size="8">' + ("" if one_sl else "№{}: ".format(v["n"])) + xh.fix(v["full_title"]) + ("" if not v["comment"] else " <font face=\"OpenSans\" size=\"" + str(font_size * 0.8) + "\">[{}]</font>".format(v["comment"])) + ("" if not v["hospital_department_replaced_title"] else f"<br/>Направлен в: {v['hospital_department_replaced_title']}" ) + "</font>", styleSheet["BodyText"], ), Paragraph( '<font face="OpenSans" size="8">' + xh.fix(v["info"]) + "</font>", styleSheet["BodyText"]), ] data.append(tmp) m = 8 else: colWidths = [int(tw / 2), int(tw / 2)] c.drawString(paddingx + (w / 2 * xn), (h / 2 - height - 134) + (h / 2) * yn, "Назначения: ") c.setStrokeColorRGB(0, 0, 0) c.setLineWidth(1) values.sort(key=lambda l: (l["g"], l["sw"])) n_rows = int(len(values) / 2) normvars = [] c_cnt = nc_cnt = 0 for i in range(0, len(values) + 1): if (i + 1) % 2 == 0: nc_cnt += 1 if nc_cnt + n_rows < len(values): normvars.append(values[nc_cnt + n_rows]) else: normvars.append(values[c_cnt]) c_cnt += 1 p = Paginator(normvars, 2) n = 1 for pg_num in p.page_range: pg = p.page(pg_num) tmp = [] for obj in pg.object_list: ns[obj["n"]] = n tmp.append( Paragraph( '<font face="OpenSans" size="' + str(font_size) + '">' + ("" if one_sl else "№{}: ".format(n)) + obj["title"] + ("" if not obj["count"] or obj["count"] == 1 else " ({}шт.)".format(str(obj["count"]))) + ("" if not obj["comment"] else " <font face=\"OpenSans\" size=\"" + str(font_size * 0.8) + "\">[{}]</font>".format(obj["comment"])) + "</font>", styleSheet["BodyText"], )) n += 1 if len(pg.object_list) < 2: tmp.append( Paragraph( '<font face="OpenSans" size="' + str(font_size) + '"></font>', styleSheet["BodyText"])) data.append(tmp) t = Table(data, colWidths=colWidths) t.setStyle( TableStyle([ ('ALIGN', (0, 0), (-1, -1), 'CENTER'), ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), ('TEXTCOLOR', (0, -1), (-1, -1), colors.black), ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black), ('BOX', (0, 0), (-1, -1), 0.25, colors.black), ('LEFTPADDING', (0, 0), (-1, -1), 4), ('TOPPADDING', (0, 0), (-1, -1), 0.5), ('RIGHTPADDING', (0, 0), (-1, -1), 2), ('BOTTOMPADDING', (0, 0), (-1, -1), 2), ])) t.canv = c wt, ht = t.wrap(0, 0) t.drawOn(c, paddingx + (w / 2 * xn), ((h / 2 - height - 138 + m) + (h / 2) * yn - ht)) c.setFont('OpenSans', 8) if not has_descriptive and not has_doc_refferal: c.drawString(paddingx + (w / 2 * xn), (h / 2 - height - 138 + m) + (h / 2) * yn - ht - 10, "Всего назначено: " + str(len(issledovaniya))) if service_locations: n = 0 if has_descriptive or has_doc_refferal else 1 if one_sl: c.drawString(paddingx + (w / 2 * xn), (h / 2 - height - 138 + m) + (h / 2) * yn - ht - 14 - n * 10, "Место: " + list(service_locations)[0]) else: c.drawString(paddingx + (w / 2 * xn), (h / 2 - height - 138 + m) + (h / 2) * yn - ht - 14 - n * 10, "Места оказания услуг:") for title in service_locations: n += 1 c.drawString( paddingx + (w / 2 * xn), (h / 2 - height - 138 + m) + (h / 2) * yn - ht - 14 - n * 10, title + " – услуги " + ', '.join( map(lambda x: "№{}".format(ns[x]), service_locations[title])), ) if need_qr_code: qr_value = translit(dir.client.individual.fio(), 'ru', reversed=True) qr_code = qr.QrCodeWidget(qr_value) qr_code.barWidth = 70 qr_code.barHeight = 70 qr_code.qrVersion = 1 d = Drawing() d.add(qr_code) renderPDF.draw(d, c, paddingx + (w / 2 * xn) + 200, 32 + (h / 2) * yn) nn = 0 if not dir.imported_from_rmis: if dir.doc_who_create and dir.doc_who_create != dir.doc: nn = 9 c.drawString( paddingx + (w / 2 * xn), 13 + (h / 2) * yn, Truncator("Выписал: %s, %s" % (dir.doc_who_create.get_fio(), dir.doc_who_create.podrazdeleniye.title)).chars(63)) if dir.doc: c.drawString( paddingx + (w / 2 * xn), 22 + (h / 2) * yn + nn, "Отделение: " + Truncator(dir.get_doc_podrazdeleniye_title()).chars(50)) c.drawString(paddingx + (w / 2 * xn), 13 + (h / 2) * yn + nn, "Л/врач: " + dir.doc.get_fio()) else: c.drawString(paddingx + (w / 2 * xn), 31 + (h / 2) * yn + nn, "РМИС#" + dir.rmis_number) c.drawString(paddingx + (w / 2 * xn), 22 + (h / 2) * yn + nn, "Создал направление: " + dir.doc_who_create.get_fio()) c.drawString(paddingx + (w / 2 * xn), 13 + (h / 2) * yn + nn, dir.doc_who_create.podrazdeleniye.title) c.setFont('OpenSans', 7) c.setLineWidth(0.25)
def gen_pdf_dir(request): """Генерация PDF направлений""" direction_id = json.loads(request.GET.get("napr_id", '[]')) if direction_id == []: request_direction_id = json.loads(request.body) direction_id = request_direction_id.get("napr_id", "[]") if SettingManager.get( "pdf_auto_print", "true", "b" ) and not request.GET.get('normis') and not request.GET.get('embedded'): pdfdoc.PDFCatalog.OpenAction = '<</S/JavaScript/JS(this.print\({bUI:true,bSilent:false,bShrinkToFit:true}\);)>>' response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'inline; filename="directions.pdf"' pdfmetrics.registerFont( TTFont('OpenSans', os.path.join(FONTS_FOLDER, 'OpenSans.ttf'))) pdfmetrics.registerFont( TTFont('OpenSansBold', os.path.join(FONTS_FOLDER, 'OpenSans-Bold.ttf'))) pdfmetrics.registerFont( TTFont('TimesNewRoman', os.path.join(FONTS_FOLDER, 'TimesNewRoman.ttf'))) dn = (Napravleniya.objects.filter(pk__in=direction_id).prefetch_related( Prefetch( 'issledovaniya_set', queryset=Issledovaniya.objects.all().select_related( 'research', 'research__podrazdeleniye', 'localization', 'service_location').prefetch_related( 'research__fractions_set'), )).select_related( 'client', 'client__base', 'client__individual', 'parent', 'parent__research', 'doc_who_create', 'doc_who_create__podrazdeleniye', 'doc', 'doc__podrazdeleniye', 'imported_org', 'istochnik_f', ).order_by('pk')) donepage = dn.exclude(issledovaniya__research__direction_form=0) donepage = donepage.exclude(external_organization__isnull=False) external_org_form = dn.filter(external_organization__isnull=False) buffer = BytesIO() count_direction = len(direction_id) format_A6 = SettingManager.get( "format_A6", default='False', default_type='b') and count_direction == 1 and donepage.count() == 0 page_size = A6 if format_A6 else A4 c = canvas.Canvas(buffer, pagesize=page_size) c.setTitle('Направления {}'.format(', '.join( [str(x) for x in direction_id]))) # для внешних организацй external_print_form = False if external_org_form.count() > 0: external_print_form = True f = import_string('directions.forms.forms' + '380' + '.form_' + '05') c = canvas.Canvas(buffer, pagesize=A4) f(c, external_org_form) ddef = dn.filter(issledovaniya__research__direction_form=0, external_organization=None).distinct() p = Paginator(ddef, 4) # Деление списка направлений по 4 instructions = [] has_def = ddef.count() > 0 def_form_print = False if has_def and not format_A6: if external_print_form: def_form_print = True c.showPage() framePage(c) for pg_num in p.page_range: pg = p.page(pg_num) i = 4 # Номер позиции направления на странице (4..1) for n_ob in pg.object_list: # Перебор номеров направлений на странице def_form_print = True print_direction( c, i, n_ob, format_A6 ) # Вызов функции печати направления на указанную позицию instructions += n_ob.get_instructions() i -= 1 if pg.has_next(): # Если есть следующая страница c.showPage() # Создание новой страницы framePage(c) # Рисование разделительных линий для страницы if donepage.count() > 0 and has_def: if external_print_form or def_form_print: c.showPage() n = 0 cntn = donepage.count() for d in donepage: n += 1 iss = d.issledovaniya_set.all() if not iss.exists(): continue form = iss[0].research.direction_form if form != 0 and not d.external_organization: current_type_form = str(form) f = import_string('directions.forms.forms' + current_type_form[0:3] + '.form_' + current_type_form[3:5]) f(c, d) if n != cntn: c.showPage() instructions_pks = [] instructions_filtered = [] for i in instructions: if i["pk"] in instructions_pks: continue instructions_pks.append(i["pk"]) instructions_filtered.append(i) if len(instructions_filtered) > 0: s = getSampleStyleSheet() s = s["BodyText"] s.wordWrap = 'LTR' c.showPage() tx = '<font face="OpenSansBold" size="10">Памятка пациенту по проведению исследований</font>\n' for i in instructions_filtered: tx += '--------------------------------------------------------------------------------------\n<font face="OpenSansBold" size="10">{}</font>\n<font face="OpenSans" size="10"> {}\n</font>'.format( # noqa: E501 i["title"], i["text"]) data = [[Paragraph(tx.replace("\n", "<br/>"), s)]] t = Table(data, colWidths=[w - 30 * mm]) t.setStyle( TableStyle([ ('ALIGN', (0, 0), (-1, -1), 'CENTER'), ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), ('TEXTCOLOR', (0, -1), (-1, -1), colors.black), ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.white), ('BOX', (0, 0), (-1, -1), 0.25, colors.white), ('LEFTPADDING', (0, 0), (-1, -1), 4), ('TOPPADDING', (0, 0), (-1, -1), 0.5), ('RIGHTPADDING', (0, 0), (-1, -1), 2), ('BOTTOMPADDING', (0, 0), (-1, -1), 2), ])) t.canv = c wt, ht = t.wrap(0, 0) t.drawOn(c, 15 * mm, h - 15 * mm - ht) c.showPage() c.save() # Сохранение отрисованного на PDF pdf = buffer.getvalue() # Получение данных из буфера # Проверить, если единый источник финансирвоания у направлений и title==платно, тогода печатать контракт fin_ist_set = set() card_pk_set = set() for n in dn: if n.istochnik_f: fin_ist_set.add(n.istochnik_f) card_pk_set.add(n.client_id) internal_type = n.client.base.internal_type fin_status = None if fin_ist_set and fin_ist_set.pop().title.lower() == 'платно': fin_status = True if request.GET.get("contract") and internal_type: if request.GET["contract"] == '1' and SettingManager.get( "direction_contract", default='False', default_type='b'): if len(card_pk_set) == 1 and fin_status: new_form_contract = SettingManager.get("new_form_contract", default='True', default_type='b') if new_form_contract: from forms.forms102 import form_02 as f_contract else: from forms.forms102 import form_01 as f_contract fc = f_contract( request_data={ **dict(request.GET.items()), "user": request.user, "card_pk": card_pk_set.pop(), "hospital": request.user.doctorprofile.get_hospital(), }) if fc: fc_buf = BytesIO() fc_buf.write(fc) fc_buf.seek(0) buffer.seek(0) from pdfrw import PdfReader, PdfWriter today = datetime.now() date_now1 = datetime.strftime(today, "%y%m%d%H%M%S%f")[:-3] date_now_str = str(n.client_id) + str(date_now1) dir_param = SettingManager.get("dir_param", default='/tmp', default_type='s') file_dir = os.path.join(dir_param, date_now_str + '_dir.pdf') file_contract = os.path.join( dir_param, date_now_str + '_contract.pdf') save_tmp_file(buffer, filename=file_dir) save_tmp_file(fc_buf, filename=file_contract) pdf_all = BytesIO() inputs = [file_contract] if SettingManager.get( "only_contract", default='False', default_type='b') else [file_dir, file_contract] writer = PdfWriter() for inpfn in inputs: writer.addpages(PdfReader(inpfn).pages) writer.write(pdf_all) pdf_out = pdf_all.getvalue() pdf_all.close() response.write(pdf_out) buffer.close() os.remove(file_dir) os.remove(file_contract) fc_buf.close() return response buffer.close() # Закрытие буфера response.write(pdf) # Запись PDF в ответ return response