Пример #1
0
def generatePDF(time, audio, maxAmp, maxAmpTime, minAmp, minAmpTime, totalTime,
                datas, speech, audioName, audioDate, audioGraf, username,
                audioId):
    data = []
    #datas = datas['words']
    date = datetime.now()
    calenderDate = str(date).split(" ")
    separetedDate = calenderDate[0].split("-")
    formattedDate = separetedDate[2] + "/" + separetedDate[
        1] + "/" + separetedDate[0]

    try:
        relatorio = BytesIO()
        pdf = Canvas(relatorio, pagesize=LETTER)
        pdf.setFont("Helvetica-Bold", 20)
        pdf.drawString(190, 740, 'Relatório da Análise de Audio')
        pdf.drawInlineImage("apnealogo.png", -20, 645, 200, 180)
        pdf.setFont("Helvetica", 10)
        pdf.drawString(520, 750, 'Gerado em:')
        pdf.drawString(520, 738, formattedDate)
        pdf.line(20, 690, 585, 690)

        pdf.setFont("Helvetica-Bold", 16)
        pdf.drawString(230, 670, 'Dados do Audio')
        pdf.setFont("Helvetica", 14)
        pdf.drawString(22, 650, f'Nome do Audio: {audioName}')
        pdf.drawString(22, 630, f'Data de envio: {audioDate}')
        pdf.drawString(
            22, 610,
            f'Tempo Total do audio: {format(totalTime,".2f")} segundos')
        pdf.drawString(22, 590,
                       f'Amplitude máxima: {str(format(maxAmp,".2f"))}')
        pdf.drawString(
            22, 570,
            f'Tempo da amplitude máxima: {str(format(maxAmpTime, ".2f"))} segundos'
        )
        pdf.drawString(22, 550,
                       f'Amplitude mínima: {str(format(minAmp,".2f"))}')
        pdf.drawString(
            22, 530,
            f'Tempo da amplitude mínima: {str(format(minAmpTime,".2f"))} segundos'
        )
        pdf.line(20, 510, 585, 510)

        pdf.setFont("Helvetica-Bold", 16)
        pdf.drawString(230, 485, 'Fala Detectada')

        pdf.setFont("Helvetica", 14)
        speechLines = wrap(speech, 88)
        speechy = 480

        for line in speechLines:
            speechy -= 20
            pdf.drawString(22, speechy, line)

        for json in datas:
            data.append((json['word'], json['startTime'], json['endTime']))
            print(json['word'], json['startTime'], json['endTime'])

        data.insert(0, ("Palavra", "Tempo de Início\n(segundos)",
                        "Tempo de fim\n(segundos)"))

        table = generateTable(data)

        newPdfs = []

        generateNewPDF(table, 0, [], (speechy - 50), (speechy - 20), pdf,
                       newPdfs, relatorio)
        pdf_writer = PdfFileWriter()

        newPdfs.append(audioGraf)

        for path in newPdfs:
            pdf_reader = PdfFileReader(path)
            for page in range(pdf_reader.getNumPages()):
                pdf_writer.addPage(pdf_reader.getPage(page))

        relatorioFinal = BytesIO()
        with relatorioFinal as pdf:
            pdf_writer.write(pdf)
            print("PDF Gerado com sucesso")
            #Generate file key for pdf
            fileKey = generateFileKey(username, audioId, ".pdf", audioName)
            #Call method to save audio file to S3
            saveFileS3("apneasleepbucket", fileKey, pdf.getvalue())

        return fileKey

    except Exception as e:
        print(e)
        return 'Erro ao gerar PDF'