PDF generation: Tweak to measuring line width for font size adjustment

This commit is contained in:
Les Orchard 2012-04-22 21:31:46 -04:00
Родитель 5422ae73eb
Коммит 18400b65a7
1 изменённых файлов: 23 добавлений и 17 удалений

Просмотреть файл

@ -13,8 +13,8 @@ from reportlab.pdfgen import canvas
from reportlab.lib import pagesizes from reportlab.lib import pagesizes
from reportlab.lib.units import inch from reportlab.lib.units import inch
from reportlab.platypus import ( from reportlab.platypus import (
SimpleDocTemplate, BaseDocTemplate, Paragraph, Spacer, PageBreak, SimpleDocTemplate, BaseDocTemplate, Paragraph, Preformatted, Spacer,
Frame, FrameBreak, PageTemplate, Image, Table) PageBreak, Frame, FrameBreak, PageTemplate, Image, Table)
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.rl_config import defaultPageSize from reportlab.rl_config import defaultPageSize
from reportlab.lib.units import inch from reportlab.lib.units import inch
@ -40,19 +40,20 @@ rows = 10
def render_claims_to_pdf(request, slug, claim_group, deferred_awards): def render_claims_to_pdf(request, slug, claim_group, deferred_awards):
response = HttpResponse(content_type='application/pdf; charset=utf-8')
response['Content-Disposition'] = ('attachment; filename="%s-%s.pdf"' %
(slug.encode('utf-8', 'replace'), claim_group))
doc = BaseDocTemplate(response, pageSize=pagesizes.letter,
topMargin=top_margin, leftMargin=left_margin)
debug = (request.GET.get('debug', False) is not False) debug = (request.GET.get('debug', False) is not False)
# TODO: Turn off the boundaries, once we're done tweaking the layout to response = HttpResponse(content_type='application/pdf; charset=utf-8')
# match label sheets if not debug:
# if debug: show_boundary = 1 # If debugging, don't force download.
# else: show_boundary = 0 response['Content-Disposition'] = ('attachment; filename="%s-%s.pdf"' %
show_boundary = 1 (slug.encode('utf-8', 'replace'), claim_group))
doc = BaseDocTemplate(response, pageSize=pagesizes.letter,
topMargin=top_margin, leftMargin=left_margin,
allowSplitting=1)
if debug: show_boundary = 1
else: show_boundary = 0
# Build frames for labels in the template # Build frames for labels in the template
frames = [] frames = []
@ -130,14 +131,19 @@ def render_claims_to_pdf(request, slug, claim_group, deferred_awards):
return response return response
def resize_para(str, max_size=11.0, min_size=2.0, max_width=(0.3125*inch), def resize_para(str, max_size=10.0, min_size=2.0, max_width=(1.25*inch),
font_name='Helvetica', alignment=TA_CENTER): font_name='Helvetica', alignment=TA_CENTER):
"""Produce a paragraph, reducing font size until it fits in the max width"""
size = max_size size = max_size
while size > min_size: while size > min_size:
para = Paragraph(str, ParagraphStyle(name='Size %s' % size, # HACK: Use a preformatted object so that minWidth() fomes up with
alignment=alignment, fontName=font_name, fontSize=size, # non-wrapped width. This just feels so dirty, dirty, but it works
leading=size+0.25)) style = ParagraphStyle(name='Size %s' % size,
alignment=alignment, fontName=font_name,
fontSize=size, leading=size+0.25)
para = Preformatted(str, style)
if para.minWidth() <= max_width: if para.minWidth() <= max_width:
para = Paragraph(str, style)
break break
size -= 0.125 size -= 0.125
return para return para