from dattai2021 import worksheet
from dattai2021 import num
from dattai2021 import font_type

################# データ格納 ######################

#b_value = int(worksheet.acell('D2').value)整数の場合
#日付
import datetime
dt_now = datetime.datetime.now()
b_value = dt_now.strftime('%Y年%m月%d日')

#名前EF
myo = str(worksheet.acell('E'+ str(num)).value)
namae =str(worksheet.acell('F'+ str(num)).value)
#基礎年金番号
kisow = str(worksheet.acell('W'+ str(num)).value)
kisox =str(worksheet.acell('X'+ str(num)).value)

#誕生日K
tanzyo = str(worksheet.acell('K'+ str(num)).value)
fm = str(worksheet.acell('H'+ str(num)).value)
country = str(worksheet.acell('L'+ str(num)).value)
area = str(worksheet.acell('M'+ str(num)).value)
postn = str(worksheet.acell('O'+ str(num)).value)
baname = str(worksheet.acell('Q'+ str(num)).value)
bashiten = str(worksheet.acell('R'+ str(num)).value)
baarea = str(worksheet.acell('S'+ str(num)).value)
bacountry = str(worksheet.acell('T'+ str(num)).value)
banumber = str(worksheet.acell('U'+ str(num)).value)
baacn = str(worksheet.acell('V'+ str(num)).value)
phone = str(worksheet.acell('J'+ str(num)).value)



############################PDF書き込み#############################
from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4, portrait
from reportlab.lib.units import inch, mm, cm
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

# 源真ゴシック（ http://jikasei.me/font/genshin/）
GEN_SHIN_GOTHIC_MEDIUM_TTF = "./fonts/GenShinGothic-Monospace-Medium.ttf"

# ファイルの指定
template_file = './pdf/02-1.pdf' # 既存のテンプレートPDF
output_file = './k02-1.pdf' # 完成したPDFの保存先
tmp_file = './__tmp.pdf' # 一時ファイル

# A4縦のCanvasを作成 -- (*1)bottomupにて原点左上
w, h = portrait(A4)
cv = canvas.Canvas(tmp_file, pagesize=(w, h), bottomup=False)
# フォント登録
pdfmetrics.registerFont(TTFont(font_type, GEN_SHIN_GOTHIC_MEDIUM_TTF))

font_size = 20
cv.setFont(font_type, font_size)

#####################書き込みエリア######################33

# 文字列を描画する --- (*3)
cv.setFillColorRGB(0, 0, 99)

cv.drawString(27*mm,87*mm, b_value)
font_size = 13
cv.setFont(font_type, font_size)
#singarea
cv.drawString(89*mm,89*mm, namae+" "+myo)

font_size = 29
cv.setFont(font_type, font_size)
#namearea
cv.drawString(52*mm,108*mm, namae+" "+myo)

font_size = 18
cv.setFont(font_type, font_size)
cv.drawString(52*mm,117*mm, tanzyo)
cv.drawString(167*mm,117*mm, country)

#############　現在住所　段あり　################
font_size = 10
cv.setFont(font_type, font_size)
cn = len(area)
if cn < 50:
	font_size = 11
	cv.setFont(font_type, font_size)
	cv.drawString(52*mm,126*mm,area)
else:
	ar1 = str(area[0:99])
	ar2 = str(area[100:199])
	cv.drawString(52*mm,126*mm, ar1)
	cv.drawString(52*mm,134*mm, ar2)

###########ここまで↑住所　段あり↑###########




#############　銀行住所　################
font_size = 10
cv.setFont(font_type, font_size)
cn = len(baarea)
if cn < 50:
	font_size = 11
	cv.setFont(font_type, font_size)
	cv.drawString(52*mm,166*mm,baname)
	cv.drawString(52*mm,175*mm,bashiten)
	cv.drawString(52*mm,182*mm,baarea)
	cv.drawString(160*mm,190*mm,bacountry)
	cv.drawString(52*mm,199*mm,banumber)
	font_size = 17
	cv.setFont(font_type, font_size)
	cv.drawString(77*mm,209*mm,baacn)
else:
	font_size = 11
	cv.setFont(font_type, font_size)
	cv.drawString(52*mm,166*mm,baname)
	cv.drawString(52*mm,175*mm,bashiten)
	cv.drawString(160*mm,190*mm,bacountry)
	cv.drawString(52*mm,199*mm,banumber)
	bar1 = str(baarea[0:99])
	bar2 = str(baarea[100:199])
	cv.drawString(52*mm,182*mm, bar1)
	cv.drawString(52*mm,190*mm, bar2)
	font_size = 17
	cv.setFont(font_type, font_size)
	cv.drawString(77*mm,209*mm,baacn)

###########ここまで↑住所　段あり↑###########


# 基礎年金番号
font_size = 23
cv.setFont(font_type, font_size)
cv.drawString(95*mm,236*mm,kisow)
cv.drawString(144*mm,236*mm,kisox)

# 一時ファイルに保存 --- (*4)
cv.showPage()
cv.save()

# テンプレートとなるPDFを読む --- (*5)
template_pdf = PdfFileReader(template_file)
template_page = template_pdf.getPage(0)

# 一時ファイルを読んで合成する --- (*6)
tmp_pdf = PdfFileReader(tmp_file)
template_page.mergePage(tmp_pdf.getPage(0))

# 書き込み先PDFを用意 --- (*7)
output = PdfFileWriter()
output.addPage(template_page)
with open(output_file, "wb") as fp:
  output.write(fp)
