import io
from datetime import datetime
from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
packet = io.BytesIO()
pdfmetrics.registerFont(TTFont('times-new-roman-cyr-kursywa', 'times-new-roman-cyr-kursywa.ttf'))
can = canvas.Canvas(packet, pagesize=letter)
def make_pdf(place, recipient, address, address2, address3):
can.setFont("times-new-roman-cyr-kursywa", 10)
can.drawString(295, 790, place)
can.drawString(290, 779, datetime.today().strftime("%d.%m.%Y")+" р.")
can.drawString(123, 759, recipient)
can.drawString(98, 747, address)
can.drawString(38, 735, address2)
can.drawString(225, 726, address3)
can.setFont("Helvetica", 11)
can.drawString(48, 715, "✓")
can.drawString(78, 697, "✓")
can.save()
#move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(open("blank-f-119.pdf", "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# finally, write "output" to a real file
outputStream = open("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()
Это код записи
import PySimpleGUI as sg
import slate_text_extraction
sg.theme('Dark Amber') # Let's set our own color theme
# STEP 1 define the layout
layout = [
[sg.Text('Місце подання')], [sg.Input( key='-PLACE-')],
[sg.Text('Найменування адресату')], [sg.Input(key='-RECIPIENT-')],
[sg.Text('Поштова адреса')], [sg.Input( key='-ADDRESS-')],
[sg.Text()], [sg.Input(key='-ADDRESS2-')],
[sg.Text()], [sg.Input(key='-ADDRESS3-')],
[sg.Button('Сформувати'), sg.Button('Вихід')]
]
#STEP 2 - create the window
window = sg.Window('Увідомлення PDF', layout)
def validate(values):
is_valid = True
values_invalid = []
if len(values['-PLACE-']) == 0:
values_invalid.append('place')
is_valid = False
if len(values['-RECIPIENT-']) == 0:
values_invalid.append('recipient')
is_valid = False
if len(values['-ADDRESS-']) == 0:
values_invalid.append('address')
values_invalid.append('address2')
values_invalid.append('address3')
is_valid = False
result = [is_valid, values_invalid]
return result
# STEP3 - the event loop
while True:
event, values = window.read() # Read the event that happened and the values dictionary
print(event, values)
if event == sg.WIN_CLOSED or event == 'Вихід': # If user closed window with X or if user clicked "Exit" button then exit
break
if event == 'Сформувати':
validation_result = validate(values)
if validation_result[0]:
slate_text_extraction.make_pdf(values['-PLACE-'], values['-RECIPIENT-'],
values['-ADDRESS-'], values['-ADDRESS2-'], values['-ADDRESS3-'])
sg.popup('СФОРМОВАНО')
else:
sg.popup('error_message')
window.close()
Код приема информации
При запуске и первой записи всё правильно функционирует но при повторной записи выдает ошибку.
По отдельности все работает.
Возможно, я что-то не так сделал при подключении? Посоветуйте.