from django.http import HttpResponse,FileResponse
import io
from reportlab.pdfgen import canvas
# Create your views here.
# from django.shortcuts import render, redirect
# from c4escalculation.models import EsInfo,BasisPay
# import math
from reportlab.pdfbase import pdfmetrics # 注册字体
from reportlab.pdfbase.ttfonts import TTFont # 字体类
from reportlab.platypus import Table, SimpleDocTemplate, Paragraph, Image # 报告内容相关类
from reportlab.lib.pagesizes import letter,A4 # 页面的标志尺寸(8.5*inch, 11*inch)
from reportlab.lib.styles import getSampleStyleSheet # 文本样式
from reportlab.lib import colors # 颜色模块
from reportlab.graphics.charts.barcharts import VerticalBarChart # 图表类
from reportlab.graphics.charts.legends import Legend # 图例类
from reportlab.graphics.shapes import Drawing # 绘图工具
from reportlab.lib.units import cm, mm # 单位:cm
# 注册字体(提前准备好字体文件, 如果同一个文件需要多种字体可以注册多个)
pdfmetrics.registerFont(TTFont('SimFang', 'simfang.ttf'))
pdfmetrics.registerFont(TTFont('YouYuan', 'SIMYOU.TTF'))
pdfmetrics.registerFont(TTFont('SimSun', 'STZHONGS.TTF'))
pdfmetrics.registerFont(TTFont('SimKai', 'simkai.ttf'))
class Graphs:
# style = getSampleStyleSheet()
# 绘制标题
@staticmethod
def draw_title(title: str):
# 获取所有样式表
style = getSampleStyleSheet()
# 拿到标题样式
ct =style['Heading1']
# 单独设置样式相关属性
ct.fontName = 'SimSun' # 字体名
ct.fontSize = 22 # 字体大小
ct.leading = 50 # 行间距
ct.textColor = colors.black # 字体颜色
ct.alignment = 1 # 居中
ct.bold = True
# 创建标题对应的段落,并且返回
return Paragraph(title, ct)
# 绘制小标题
@staticmethod
def draw_little_title(title: str):
# 获取所有样式表
style = getSampleStyleSheet()
# 拿到标题样式
ct = style['Normal']
# 单独设置样式相关属性
ct.fontName = 'SimSun' # 字体名
ct.fontSize = 15 # 字体大小
ct.leading = 30 # 行间距
ct.textColor = colors.red # 字体颜色
# 创建标题对应的段落,并且返回
return Paragraph(title, ct)
# 绘制普通段落内容
@staticmethod
def draw_text(text: str):
# 获取所有样式表
style = getSampleStyleSheet()
# 获取普通样式
ct = style['Normal']
ct.fontName = 'SimKai'
ct.fontSize = 16
ct.wordWrap = 'CJK' # 设置自动换行
ct.alignment = 0 # 左对齐
ct.firstLineIndent = 32 # 第一行开头空格
ct.leading = 25
return Paragraph(text, ct)
# 绘制表格
@staticmethod
def draw_table(*args):
# 列宽度
# col_width = 50
# cell=[80,70,80,100,85,60]
# style = [#表格全局设置
# ('FONTNAME', (0, 0), (-1, -1), 'SimKai'), # 字体
# ('FONTSIZE', (0, 0), (-1, -1), 10), # 字体大小
# ('TEXTCOLOR', (0, 0), (-1, -1), colors.darkslategray), # 设置表格内文字颜色
# #第一行设置
# # ('ALIGN', (0, 0), (0, 0), 'CENTER'), # 第一行水平居中
# ('ALIGN', (0, 0), (0, 0), 'RIGHT'),# 第一行第一列靠右
# ('ALIGN', (1, 0), (1, 0), 'LEFT'),# 第一行第二列靠左
# ('ALIGN', (4, 0), (4, 0), 'RIGHT'),
# ('ALIGN', (5, 0),(-1,0),'LEFT'),
# ('BACKGROUND', (0, 0), (-1, 0), '#d5dae6'), # 设置第一行背景颜色
# #第二行开始
# ('GRID', (0, 1), (-1, -1), 0.5, colors.grey), # 设置表格框线为grey色,线宽为0.5,第一行无框线
# ('ALIGN', (0, 1), (-1, -1), 'LEFT'), # 第二行到最后一行左右左对齐
# ('FONTSIZE', (0, 1), (-1, -1), 10), # 第二行到最后一行的字体大小
# ('VALIGN', (0, 1), (-1, -1), 'MIDDLE'), #第二行到最后一行上下居中对齐
# # 第一、三、五列设置相同
# ('ALIGN', (0, 1),(0,-1),'CENTER'),#第一列居中
# ('FONTNAME', (0, 1), (0, -1), 'YouYuan'), # 字体)
# ('ALIGN', (2, 1),(2,-1),'CENTER'),#第三列居中
# ('FONTNAME', (2, 1), (2, -1), 'YouYuan'), # 字体)
# ('ALIGN', (4, 1),(4,-1),'CENTER'),#第五列居中
# ('FONTNAME', (4, 1), (4, -1), 'YouYuan'), # 字体)
# #合并格
# ('SPAN', (4, 1), (5, 6)), # 合并第2行第5列到第6列第7行放置照片
# ('SPAN', (1, 2), (3, 2)), # 合并第一列三四行放置 部职别
# ]
# table = Table(args, colWidths=col_width, style=style)
table = Table(args,)
return table
# 创建图表
@staticmethod
def draw_bar(bar_data: list, ax: list, items: list):
drawing = Drawing(500, 250)
bc = VerticalBarChart()
bc.x = 45 # 整个图表的x坐标
bc.y = 45 # 整个图表的y坐标
bc.height = 200 # 图表的高度
bc.width = 350 # 图表的宽度
bc.data = bar_data
bc.strokeColor = colors.black # 顶部和右边轴线的颜色
bc.valueAxis.valueMin = 5000 # 设置y坐标的最小值
bc.valueAxis.valueMax = 26000 # 设置y坐标的最大值
bc.valueAxis.valueStep = 2000 # 设置y坐标的步长
bc.categoryAxis.labels.dx = 2
bc.categoryAxis.labels.dy = -8
bc.categoryAxis.labels.angle = 20
bc.categoryAxis.categoryNames = ax
# 图示
leg = Legend()
leg.fontName = 'SimSun'
leg.alignment = 'right'
leg.boxAnchor = 'ne'
leg.x = 475 # 图例的x坐标
leg.y = 240
leg.dxTextSpace = 10
leg.columnMaximum = 3
leg.colorNamePairs = items
drawing.add(leg)
drawing.add(bc)
return drawing
# 绘制图片
@staticmethod
def draw_img(path,width=35,heigh=53):
img = Image(path) # 读取指定路径下的图片
# img.drawWidth = 5*cm # 设置图片的宽度
# img.drawHeight = 8*cm # 设置图片的高度
img.drawWidth = width*mm # 设置图片的宽度2寸照片 35mm 413×626像素(一般打印300像素/英寸)。
img.drawHeight = heigh*mm # 设置图片的高度 53mm
return img
# def makepdf(Graph,):
# # 创建内容对应的空列表
# content = list()
# # 添加标题
# content.append(Graphs.draw_title('信息'))
# # 添加图片
# # content.append(Graphs.draw_img('/home/jxk02/图片/lwk.jpg'))
# # 添加段落文字
# content.append(Graphs.draw_text('本人已掌握逐月领取退役金安置的相关政策,知晓安置后的相关政治和生活待遇,自愿自行选择逐月领取退役金安置。'))
# # 添加小标题
# # content.append(Graphs.draw_title('县区:'))
# # content.append(Graphs.draw_little_title('基数年份'))
# # 添加表格
# stylesheet=getSampleStyleSheet()
# P=Graphs.draw_img('/home/jxk02/图片/lwk.jpg')
# text='''<para fontName="YouYuan" FONTSIZE=8 TEXTCOLOR=darkslategray>
# 工程师
# </para>'''
# B = Paragraph(text,
# stylesheet["BodyText"])
# data = [
# ('分配县区:', '41', ' ','','基数年份:','2021'),
#
# ]
# content.append(Graphs.draw_table(*data))
# 生成图表
# content.append(Graphs.draw_title(''))
# content.append(Graphs.draw_little_title('热门城市的就业情况'))
# b_data = [(25400, 12900, 20100, 20300, 20300, 17400), (15800, 9700, 12982, 9283, 13900, 7623)]
# ax_data = ['BeiJing', 'ChengDu', 'ShenZhen', 'ShangHai', 'HangZhou', 'NanJing']
# leg_items = [(colors.red, '平均薪资'), (colors.green, '招聘量')]
# content.append(Graphs.draw_bar(b_data, ax_data, leg_items))
# 生成pdf文件
# doc = SimpleDocTemplate('report.pdf', pagesize=A4)
# doc.build(content)
# return FileResponse(open('report.pdf', 'rb'), content_type='application/pdf')
def show_pdf(request,filename):
files='pdf/'+filename+'.pdf'
# print (files)
return FileResponse(open(files, 'rb'), content_type='application/pdf')
#以下为其他方法,没用过
# Style=getSampleStyleSheet()
# bt = Style['Normal'] #字体的样式
# # bt.fontName='song' #使用的字体
# bt.fontSize=14 #字号
# bt.wordWrap = 'CJK' #该属性支持自动换行,'CJK'是中文模式换行,用于英文中会截断单词造成阅读困难,可改为'Normal'
# bt.firstLineIndent = 32 #该属性支持第一行开头空格
# bt.leading = 20 #该属性是设置行距
# ct=Style['Normal']
# # ct.fontName='song'
# ct.fontSize=12
# ct.alignment=1 #居中
# ct.textColor = colors.red
# t = Paragraph('hello',bt)
# pdf=SimpleDocTemplate('ppff.pdf')
# pdf.multiBuild([t])
# from reportlab.platypus import Paragraph,SimpleDocTemplate,Table,TableStyle
# from reportlab.lib.units import inch
# from reportlab.lib import colors
# def table_model(data):
# width = 7.2 # 总宽度
# colWidths = (width / len(data[0])) * inch # 每列的宽度
# dis_list = []
# for x in data:
# # dis_list.append(map(lambda i: Paragraph('%s' % i, cn), x))
# dis_list.append(x)
# style = [
# # ('FONTNAME', (0, 0), (-1, -1), 'song'), # 字体
# ('FONTSIZE', (0, 0), (-1, 0), 15), # 字体大小
# ('BACKGROUND', (0, 0), (-1, 0), HexColor('#d5dae6')), # 设置第一行背景颜色
# ('BACKGROUND', (0, 1), (-1, 1), HexColor('#d5dae6')), # 设置第二行背景颜色
# # 合并 ('SPAN',(第一个方格的左上角坐标),(第二个方格的左上角坐标)),合并后的值为靠上一行的值,按照长方形合并
# ('SPAN',(0,0),(0,1)),
# ('SPAN',(1,0),(2,0)),
# ('SPAN',(3,0),(4,0)),
# ('SPAN',(5,0),(7,0)),
# ('ALIGN', (0, 0), (-1, -1), 'CENTER'), # 对齐
# ('VALIGN', (-1, 0), (-2, 0), 'MIDDLE'), # 对齐
# ('LINEBEFORE', (0, 0), (0, -1), 0.1, colors.grey), # 设置表格左边线颜色为灰色,线宽为0.1
# ('TEXTCOLOR', (0, 0), (-1, 0), colors.royalblue), # 设置表格内文字颜色
# ('TEXTCOLOR', (0, -1), (-1, -1), colors.red), # 设置表格内文字颜色
# ('GRID', (0, 0), (-1, -1), 0.5, colors.grey), # 设置表格框线为grey色,线宽为0.5
# ]
# component_table = Table(dis_list, colWidths=colWidths,style=style)
# return component_table
# Style=getSampleStyleSheet()
# n = Style['Normal']
# data = [[0,1,2,3,4,5,6,7],
# [00,11,22,33,44,55,66,77],
# [000,111,222,333,444,555,666,777],
# [0000,1111, 2222, 3333, 4444, 5555, 6666, 7777],]
# z = table_model(data)
# pdf = MyDocTemplate('ppff.pdf')
# pdf.multiBuild([Paragraph('Title',n),z])