2019-01-24 02:43:28 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
2019-03-18 01:42:32 +00:00
|
|
|
|
import random
|
|
|
|
|
import string
|
|
|
|
|
|
2019-01-18 08:01:30 +00:00
|
|
|
|
from PyPDF2.pdf import BytesIO
|
2019-01-18 05:18:14 +00:00
|
|
|
|
from xhtml2pdf import pisa
|
|
|
|
|
from django.template.loader import get_template
|
2019-03-18 01:42:32 +00:00
|
|
|
|
from django.http import HttpResponse, FileResponse
|
2019-01-18 08:01:30 +00:00
|
|
|
|
from dashboard.models import Spi, GDP, SpiJC, Population, Landuse
|
2019-01-24 02:41:21 +00:00
|
|
|
|
from reportlab.pdfgen.canvas import Canvas
|
|
|
|
|
from reportlab.pdfbase import pdfmetrics
|
|
|
|
|
from reportlab.pdfbase.cidfonts import UnicodeCIDFont
|
|
|
|
|
pdfmetrics.registerFont(UnicodeCIDFont('STSong-Light'))
|
|
|
|
|
from reportlab.pdfbase.ttfonts import TTFont
|
|
|
|
|
pdfmetrics.registerFont(TTFont('msyh', 'msyh.ttf'))
|
|
|
|
|
from reportlab.lib.styles import getSampleStyleSheet
|
|
|
|
|
from reportlab.lib import colors
|
|
|
|
|
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer,Image,Table,TableStyle
|
|
|
|
|
import time
|
2019-01-24 09:29:09 +00:00
|
|
|
|
from reportlab.graphics.shapes import Drawing
|
|
|
|
|
from reportlab.graphics.charts.barcharts import VerticalBarChart
|
|
|
|
|
from reportlab.graphics.charts.linecharts import HorizontalLineChart
|
2019-01-18 08:01:30 +00:00
|
|
|
|
|
|
|
|
|
def render(path: str, params: dict):
|
|
|
|
|
template = get_template(path)
|
|
|
|
|
html = template.render(params)
|
|
|
|
|
response = BytesIO()
|
|
|
|
|
pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), response)
|
|
|
|
|
if not pdf.err:
|
|
|
|
|
return HttpResponse(response.getvalue(), content_type='application/pdf')
|
|
|
|
|
else:
|
|
|
|
|
return HttpResponse("Error Rendering PDF", status=400)
|
|
|
|
|
|
2019-01-24 02:43:28 +00:00
|
|
|
|
|
2019-01-18 08:01:30 +00:00
|
|
|
|
def pdf_spi(request):
|
2019-01-27 05:50:27 +00:00
|
|
|
|
story = []
|
|
|
|
|
stylesheet = getSampleStyleSheet()
|
|
|
|
|
normalStyle = stylesheet['Normal']
|
|
|
|
|
curr_date = time.strftime("%Y-%m-%d", time.localtime())
|
2019-03-18 01:42:32 +00:00
|
|
|
|
name_date = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
|
|
|
|
|
salt = ''.join(random.sample(string.ascii_letters + string.digits, 8))
|
2019-01-27 05:50:27 +00:00
|
|
|
|
spis = Spi.objects.filter().order_by('-year')[:100]
|
|
|
|
|
rpt_title = '<para autoLeading="off" fontSize=15 align=center><b><font face="msyh">宝鸡市气象历史数据%s</font></b><br/><br/><br/></para>' % (
|
|
|
|
|
curr_date)
|
|
|
|
|
story.append(Paragraph(rpt_title, normalStyle))
|
|
|
|
|
text = '''<para autoLeading="off" fontSize=8><font face="msyh" >数据描述:</font><br/>
|
|
|
|
|
<font face="msyh" color=royalblue>1.名称</font><br/><font face="msyh" fontsize=7>宝鸡地区格点气象数据</font><br/>
|
|
|
|
|
<font face="msyh" color=royalblue>2.要素</font><br/>
|
|
|
|
|
<font face="msyh" fontsize=7>月平均降水(mm),月平均气温(℃)
|
|
|
|
|
</font><br/>
|
|
|
|
|
<font face="msyh" color=royalblue>3.时间</font><br/><font face="msyh" fontsize=7>1979-2015年
|
|
|
|
|
</font><br/>
|
|
|
|
|
<font face="msyh" color=royalblue>4.数据</font><br/>
|
|
|
|
|
<font face="msyh" fontsize=7>宝鸡地区179个网格的格点数据,分辨率为10公里
|
|
|
|
|
</font><br/></para>'''
|
|
|
|
|
story.append(Paragraph(text, normalStyle))
|
|
|
|
|
drawing = Drawing(400, 200)
|
|
|
|
|
data = [(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)]
|
|
|
|
|
bc = VerticalBarChart()
|
|
|
|
|
bc.x = 50
|
|
|
|
|
bc.y = 50
|
|
|
|
|
bc.height = 125
|
|
|
|
|
bc.width = 300
|
|
|
|
|
bc.data = data
|
|
|
|
|
bc.strokeColor = colors.black
|
|
|
|
|
|
|
|
|
|
bc.valueAxis._valueMin = 0
|
|
|
|
|
bc.valueAxis._valueMax = 50
|
|
|
|
|
bc.valueAxis._valueStep = 10
|
|
|
|
|
|
|
|
|
|
bc.categoryAxis.labels.boxAnchor = 'ne'
|
|
|
|
|
bc.categoryAxis.labels.dx = 8
|
|
|
|
|
bc.categoryAxis.labels.dy = -2
|
|
|
|
|
bc.categoryAxis.labels.angle = 30
|
|
|
|
|
bc.categoryAxis.categoryNames = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12']
|
|
|
|
|
|
|
|
|
|
bc.groupSpacing = 10
|
|
|
|
|
bc.barSpacing = 2.5
|
|
|
|
|
|
|
|
|
|
drawing.add(bc)
|
|
|
|
|
story.append(drawing)
|
|
|
|
|
|
|
|
|
|
drawingline = Drawing(400, 200)
|
|
|
|
|
data = [(13.2222, 5, 20, 22, 37, 45, 19, 4, 11, 11, 11, 11),
|
|
|
|
|
(5, 20, 46, 38, 23, 21, 6, 14, 22, 22, 22, 22)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
lc = HorizontalLineChart()
|
|
|
|
|
lc.x = 50
|
|
|
|
|
lc.y = 50
|
|
|
|
|
lc.height = 125
|
|
|
|
|
lc.width = 300
|
|
|
|
|
lc.data = data
|
|
|
|
|
lc.joinedLines = 1
|
|
|
|
|
catNames = '1 2 3 4 5 6 7 8 9 10 11 12'.split(' ')
|
|
|
|
|
lc.categoryAxis.categoryNames = catNames
|
|
|
|
|
lc.categoryAxis.labels.boxAnchor = 'n'
|
|
|
|
|
lc.valueAxis.valueMin = 0
|
|
|
|
|
lc.valueAxis.valueMax = 60
|
|
|
|
|
lc.valueAxis.valueStep = 15
|
|
|
|
|
lc.lines[0].strokeWidth = 2
|
|
|
|
|
lc.lines[1].strokeWidth = 1.5
|
|
|
|
|
drawingline.add(lc)
|
|
|
|
|
story.append(drawingline)
|
|
|
|
|
|
|
|
|
|
|
2019-03-15 01:18:04 +00:00
|
|
|
|
# img = Image('D:/spi.png')
|
|
|
|
|
# img.drawHeight = 200
|
|
|
|
|
# img.drawWidth = 400
|
|
|
|
|
# story.append(img)
|
2019-01-27 05:50:27 +00:00
|
|
|
|
text = '<para autoLeading="off" fontSize=9><br/><br/><br/><b><font face="msyh">数据报表:数据依次为区站号, 经度, 纬度, 年份,月份,月平均降水,月平均温度,</font></b><br/></para>'
|
|
|
|
|
story.append(Paragraph(text, normalStyle))
|
|
|
|
|
for spi in spis:
|
|
|
|
|
component_data= [
|
|
|
|
|
[spi.code, spi.longitude, spi.latitude, spi.year, spi.month, spi.mean_precipitation,
|
|
|
|
|
spi.mean_temperature]
|
|
|
|
|
]
|
|
|
|
|
#创建表格对象,并设定各列宽度
|
|
|
|
|
component_table = Table(component_data, colWidths=[50,50,50, 30,30, 90, 90])
|
|
|
|
|
#添加表格样式
|
|
|
|
|
component_table.setStyle(TableStyle([
|
|
|
|
|
('FONTNAME',(0,0),(-1,-1),'msyh'),#字体
|
|
|
|
|
('FONTSIZE',(0,0),(-1,-1),6),#字体大小
|
|
|
|
|
# ('SPAN',(0,0),(3,0)),#合并第一行前三列
|
|
|
|
|
('BACKGROUND',(0,0),(-1,0), colors.lightskyblue),#设置第一行背景颜色
|
|
|
|
|
# ('SPAN',(-1,0),(-2,0)), #合并第一行后两列
|
|
|
|
|
('ALIGN',(-1,0),(-2,0),'RIGHT'),#对齐
|
|
|
|
|
('VALIGN',(-1,0),(-2,0),'MIDDLE'), #对齐
|
|
|
|
|
('LINEBEFORE',(0,0),(0,-1),0.1,colors.grey),#设置表格左边线颜色为灰色,线宽为0.1
|
|
|
|
|
('TEXTCOLOR',(0,1),(-2,-1),colors.royalblue),#设置表格内文字颜色
|
|
|
|
|
('GRID',(0,0),(-1,-1),0.5,colors.red),#设置表格框线为红色,线宽为0.5
|
|
|
|
|
]))
|
|
|
|
|
story.append(component_table)
|
2019-03-18 01:42:32 +00:00
|
|
|
|
doc = SimpleDocTemplate('/home/g214/pdf/'+name_date+salt+'.pdf')
|
2019-01-27 05:50:27 +00:00
|
|
|
|
doc.build(story)
|
2019-03-18 01:42:32 +00:00
|
|
|
|
file = open('/home/g214/pdf/'+name_date+salt+'.pdf', 'rb')
|
|
|
|
|
response = FileResponse(file)
|
|
|
|
|
response['Content-Type'] = 'application/octet-stream'
|
2019-03-21 06:51:17 +00:00
|
|
|
|
# response['Content-Disposition'] = 'attachment'
|
|
|
|
|
response['Content-Disposition'] = 'attachment; filename=spi.pdf'
|
2019-03-18 01:42:32 +00:00
|
|
|
|
return response
|
2019-01-24 02:41:21 +00:00
|
|
|
|
|
2019-01-18 08:01:30 +00:00
|
|
|
|
|
|
|
|
|
def pdf_gdp(request):
|
|
|
|
|
gdps = GDP.objects.all().order_by('-date')[:100]
|
2019-01-24 02:43:28 +00:00
|
|
|
|
return render('export/export_to_pdf_for_gdp.html', {'gdps': gdps})
|
|
|
|
|
|
2019-01-18 08:01:30 +00:00
|
|
|
|
|
|
|
|
|
def pdf_spi_jc(request):
|
|
|
|
|
spi_jc = SpiJC.objects.all().order_by('-year')[:100]
|
2019-01-24 02:43:28 +00:00
|
|
|
|
return render('export/export_to_pdf_for_spi_jc.html', {'spi_jc': spi_jc})
|
|
|
|
|
|
2019-01-18 05:18:14 +00:00
|
|
|
|
|
2019-01-18 08:01:30 +00:00
|
|
|
|
def pdf_population(request):
|
|
|
|
|
pops = Population.objects.all().order_by('-date')[:100]
|
2019-01-24 02:43:28 +00:00
|
|
|
|
return render('export/export_to_pdf_for_population.html', {'pops': pops})
|
|
|
|
|
|
2019-01-18 05:18:14 +00:00
|
|
|
|
|
2019-01-18 08:01:30 +00:00
|
|
|
|
def pdf_landuse(request):
|
|
|
|
|
landuses = Landuse.objects.all().order_by('-date')[:100]
|
2019-01-24 02:43:28 +00:00
|
|
|
|
return render('export/export_to_pdf_for_landuse.html', {'landuses': landuses})
|