Added printing

This commit is contained in:
David Taylor
2015-08-09 23:39:03 +03:00
committed by Tom Price
parent fe943e12c1
commit ca8ba10efa
10 changed files with 324 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

View File

@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8" ?>
{% load multiply from filters %}
{% load static %}
<!DOCTYPE document SYSTEM "rml.dtd">
<document filename="Form Print.pdf">
<docinit>
<registerTTFont faceName="OpenSans" fileName="{{ fonts.opensans.regular }}"/>
<registerTTFont faceName="OpenSans-Bold" fileName="{{ fonts.opensans.bold }}"/>
<registerFontFamily name="OpenSans" bold="OpenSans-Bold" boldItalic="OpenSans-Bold"/>
</docinit>
<stylesheet>
<initialize>
<color id="LightGray" RGB="#D3D3D3"/>
<color id="DarkGray" RGB="#707070"/>
</initialize>
<paraStyle name="style.para" fontName="OpenSans" />
<paraStyle name="blockPara" spaceAfter="5" spaceBefore="5"/>
<paraStyle name="style.Heading1" fontName="OpenSans" fontSize="16" leading="18" spaceAfter="0"/>
<paraStyle name="style.Heading2" fontName="OpenSans-Bold" fontSize="10" spaceAfter="2"/>
<paraStyle name="style.Heading3" fontName="OpenSans" fontSize="10" spaceAfter="0"/>
<paraStyle name="center" alignment="center"/>
<blockTableStyle id="checkboxTable">
<!-- <lineStyle cap="default" kind="grid" colorName="black" thickness="1" start="0,0" stop="-1,-1"/> -->
<blockValign start="0,0" stop="-1,-1" value="top"/>
<!-- <lineStyle kind="lineabove" start="1,0" stop="-1,-1" colorName="lightgrey"/> -->
<lineStyle kind="linebelow" start="1,0" stop="-1,-1" colorName="lightgrey"/>
<blockLeftPadding start="0,0" stop="-1,-1" length="0"/>
</blockTableStyle>
<blockTableStyle id="stringTable">
<!-- <lineStyle cap="default" kind="grid" colorName="black" thickness="1" start="0,0" stop="-1,-1"/> -->
<blockValign start="0,0" stop="-1,-1" value="top"/>
<!-- <blockLeftPadding start="0,0" stop="-1,-1" length="0"/> -->
<!-- <lineStyle kind="lineabove" start="1,0" stop="-1,-1" colorName="lightgrey"/> -->
<lineStyle kind="linebelow" start="1,0" stop="-1,-1" colorName="lightgrey"/>
</blockTableStyle>
<blockTableStyle id="objectTable">
<!-- <lineStyle cap="default" kind="grid" colorName="black" thickness="1" start="0,0" stop="-1,-1"/> -->
<blockValign start="0,0" stop="-1,-1" value="top"/>
<!-- <blockLeftPadding start="0,0" stop="-1,-1" length="0"/> -->
<!-- <lineStyle kind="lineabove" start="1,0" stop="-1,-1" colorName="lightgrey"/> -->
<lineStyle kind="linebelow" start="1,0" stop="-1,-1" colorName="lightgrey"/>
</blockTableStyle>
</stylesheet>
<template > {# Note: page is 595x842 points (1 point=1/72in) #}
<pageTemplate id="Headed" >
<pageGraphics>
<image file="RIGS/static/imgs/paperwork/corner-tr-su.jpg" x="395" y="642" height="200" width="200"/>
<image file="RIGS/static/imgs/paperwork/corner-bl.jpg" x="0" y="0" height="200" width="200"/>
{# logo positioned 42 from left, 33 from top #}
<image file="RIGS/static/imgs/paperwork/tec-logo.jpg" x="42" y="719" height="90" width="84"/>
<setFont name="OpenSans-Bold" size="22.5" leading="10"/>
<drawString x="137" y="780">TEC PA &amp; Lighting</drawString>
<setFont name="OpenSans" size="9"/>
<drawString x="137" y="760">Portland Building, University Park, Nottingham, NG7 2RD</drawString>
<drawString x="137" y="746">www.nottinghamtec.co.uk</drawString>
<drawString x="265" y="746">info@nottinghamtec.co.uk</drawString>
<drawString x="137" y="732">Phone: (0115) 846 8720</drawString>
<setFont name="OpenSans" size="10" />
{% if copy %}<drawCenteredString x="302.5" y="50">[{{ copy }} Copy]</drawCenteredString>{% endif %}
<drawCenteredString x="302.5" y="38">[Page <pageNumber/> of <getName id="lastPage" default="0" />]</drawCenteredString>
</pageGraphics>
<frame id="main" x1="50" y1="65" width="495" height="645"/>
</pageTemplate>
<pageTemplate id="Main">
<pageGraphics>
<image file="RIGS/static/imgs/paperwork/corner-tr.jpg" x="395" y="642" height="200" width="200"/>
<image file="RIGS/static/imgs/paperwork/corner-bl.jpg" x="0" y="0" height="200" width="200"/>
<setFont name="OpenSans" size="10"/>
{% if not invoice %}<drawCenteredString x="302.5" y="50">[{{ copy }} Copy]</drawCenteredString>{% endif %}
<drawCenteredString x="302.5" y="38">[Page <pageNumber/> of <getName id="lastPage" default="0" />]</drawCenteredString>
</pageGraphics>
<frame id="main" x1="50" y1="65" width="495" height="727"/>
</pageTemplate>
</template>
<story firstPageTemplate="Headed">
Hello this is some text
{% include "rigForms/form_print_page.xml" %}
</story>
</document>

View File

@@ -0,0 +1,17 @@
<setNextFrame name="main"/>
<nextFrame/>
{% autoescape off %}
{{ formData }}
{% endautoescape %}
<namedString id="lastPage"><pageNumber/></namedString>

View File

@@ -0,0 +1,16 @@
{# "field" context should have "title", "description" and "value" #}
{# "children" context should be a string of inner XML #}
<blockTable colWidths="{{currentIndent}},100%" alignment="left" style="objectTable">
<tr>
<td></td>
<td><h1>{{field.title}}</h1></td>
</tr>
</blockTable>
{% autoescape off %}
{% for child in children %}
{{ child }}
{% endfor %}
{% endautoescape %}

View File

@@ -0,0 +1,15 @@
{# "field" context should have "title", "description"}
<blockTable colWidths="{{currentIndent}},25,100%" alignment="left" style="checkboxTable">
<tr>
<td></td>
<td>
{% if value %}
<img src="rigForms/static/images/form-print/checked_checkbox.jpg" width="20" height="20"/>
{% else %}
<img src="rigForms/static/images/form-print/unchecked_checkbox.jpg" width="20" height="20"/>
{% endif %}
</td>
<td><h4>{{field.title}}</h4>
<para>{{field.description}}</para></td>
</tr>
</blockTable>

View File

@@ -0,0 +1,14 @@
{# "field" context should have "title", "description" and "value" #}
{# "children" context should be a string of inner XML #}
<blockTable colWidths="{{currentIndent}},100%" alignment="left" style="objectTable">
<tr>
<td></td>
<td><h1>{{field.title}}</h1></td>
</tr>
</blockTable>
{% autoescape off %}
{{ children }}
{% endautoescape %}

View File

@@ -0,0 +1,12 @@
{# "field" context should have "title", "description" and "value"}
<blockTable colWidths="{{currentIndent}},100,100%" alignment="left" style="stringTable">
<tr>
<td></td>
<td>
<h4>{{field.title}}</h4>
<para>{{field.description}}</para>
</td>
<td>
<para>{{value}}</para></td>
</tr>
</blockTable>

View File

@@ -11,5 +11,6 @@ urlpatterns = patterns('',
name='update_form'),
url(r'^for-event/(?P<event_pk>\d+)/$', permission_required_with_403('rigForms.view_form')(views.FormList.as_view()),
name='form_list'),
url(r'^(?P<pk>\d+)/print/$', permission_required_with_403('rigForms.view_form')(views.FormPrint.as_view()), name='form_print'),
)

View File

@@ -7,6 +7,18 @@ from django.http.response import HttpResponseRedirect
from django.core.urlresolvers import reverse_lazy, reverse, NoReverseMatch
from django.db.models import Q
import json
from collections import OrderedDict
from z3c.rml import rml2pdf
from django.template.loader import get_template
from django.template import RequestContext
import cStringIO as StringIO
import re
import copy
from django.http import HttpResponse
import RIGS
class FormCreate(generic.CreateView):
@@ -76,5 +88,146 @@ class FormList(generic.ListView):
return context
class FormPrint(generic.TemplateView):
def _render_object(self, field, value, current_indent):
# Render all the child form bits first
current_indent += 20
children = self._render_field(field["properties"], value, current_indent)
current_indent -= 20
template = get_template('rigForms/print/render-object.xml')
context = {
'field': field,
'children': children,
'currentIndent':current_indent
}
return template.render(context)
def _render_string(self, field, value, current_indent):
template = get_template('rigForms/print/render-string.xml')
context = {
'field': field,
'currentIndent':current_indent,
'value':value
}
return template.render(context)
def _render_array(self, field, value, current_indent):
# Render all the child form bits first
current_indent += 20
children=[]
try:
numberOfChildren = len(value)
for key,item in enumerate(value):
thisField = copy.deepcopy(field["items"])
try:
thisField["title"] = thisField["title"] + " " + str(key+1)
except KeyError:
pass
children.append(self._render_field_item(thisField,item,current_indent))
except TypeError:
numberOfChildren = 0
current_indent -= 20
template = get_template('rigForms/print/render-array.xml')
context = {
'field': field,
'children': children,
'currentIndent':current_indent
}
return template.render(context)
def _render_boolean(self, field, value, current_indent):
template = get_template('rigForms/print/render-boolean.xml')
context = {
'field': field,
'currentIndent':current_indent,
'value':value
}
return template.render(context)
def _render_field_item(self, field, value, current_indent):
result = ""
if type(field) is OrderedDict:
if "type" in field:
if field["type"] == "object":
rendered = self._render_object(field,value,current_indent)
elif field["type"] == "string":
rendered = self._render_string(field,value,current_indent)
elif field["type"] == "array":
rendered = self._render_array(field,value,current_indent)
elif field["type"] == "boolean":
rendered = self._render_boolean(field,value,current_indent)
else:
rendered = "<h1>an unknown field</h1>"
# self._render_field(field,value,current_indent)
else:
rendered = "<h1>No type:" + str(key) + str(field) + "<br/><br/></h1>"
result += rendered
else:
result += "<h1>Not ordereddict:" + str(key) + str(field) + "<br/><br/></h1>"
return result
def _render_field(self, parentField, parentValue, current_indent):
result = ""
for (key,field) in parentField.items():
try:
value = parentValue[str(key)]
except KeyError:
value = None
except TypeError:
value = None
result += self._render_field_item(field, value, current_indent)
return result
def get(self, request, pk):
form = get_object_or_404(models.Form, pk=pk)
jsonSchema = json.loads(form.schema.schema, object_pairs_hook=OrderedDict)
jsonData = json.loads(form.data, object_pairs_hook=OrderedDict)
formData = self._render_field(jsonSchema["properties"], jsonData, 0)
# For development return the raw string
# response = HttpResponse()
# response.write(pdfData)
# return response
template = get_template('rigForms/form_print.xml')
context = RequestContext(request, {
'fonts': {
'opensans': {
'regular': 'RIGS/static/fonts/OPENSANS-REGULAR.TTF',
'bold': 'RIGS/static/fonts/OPENSANS-BOLD.TTF',
}
},
'formData':formData
})
rml = template.render(context)
buffer = StringIO.StringIO()
buffer = rml2pdf.parseString(rml)
pdfData = buffer.read()
# escapedEventName = re.sub('[^a-zA-Z0-9 \n\.]', '', object.name)
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = "filename=Form.pdf"
response.write(pdfData)
return response