1 # -*- coding: utf-8 -*-
3 # (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 warnings.simplefilter("ignore", DeprecationWarning)
23 warnings.simplefilter("ignore", SyntaxWarning)
24 from reportlab.platypus.paragraph import Paragraph
25 from reportlab.platypus.flowables import Preformatted, Image, HRFlowable
26 from reportlab.platypus.doctemplate import *
27 #from reportlab.rl_config import TTFSearchPath
28 from reportlab.platypus import SimpleDocTemplate, Spacer
29 from reportlab.platypus.tables import Table, TableStyle
30 from reportlab.lib.pagesizes import letter, legal, A4
31 from reportlab.lib.units import inch
32 from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
33 from reportlab.lib import colors
34 #from reportlab.pdfbase import pdfmetrics
35 #from reportlab.pdfbase.ttfonts import TTFont
36 from time import localtime, strftime
38 warnings.simplefilter('default', DeprecationWarning)
39 warnings.simplefilter("default", SyntaxWarning)
41 if __name__ == "__main__":
46 from base import utils
48 PAGE_SIZE_LETTER = 'letter'
49 PAGE_SIZE_LEGAL = 'legal'
54 return s.replace("&", "&").replace(">", ">").replace("<", "<")
57 def createStandardCoverPage(page_size=PAGE_SIZE_LETTER,
68 preserve_formatting=False,
71 s = getSampleStyleSheet()
76 #TTFSearchPath.append('/usr/share/fonts/truetype/arphic')
77 #pdfmetrics.registerFont(TTFont('UMing', 'uming.ttf'))
79 ps = ParagraphStyle(name="title",
81 fontName='helvetica-bold',
82 #fontName='STSong-Light',
87 story.append(Paragraph("FAX", ps))
89 story.append(Spacer(1, inch))
91 ps = ParagraphStyle(name='normal',
92 fontName='Times-Roman',
93 #fontName='STSong-Light',
97 recipient_name_label = Paragraph("To:", ps)
98 recipient_name_text = Paragraph(escape(recipient_name[:64]), ps)
100 recipient_fax_label = Paragraph("Fax:", ps)
101 recipient_fax_text = Paragraph(escape(recipient_fax[:64]), ps)
103 recipient_phone_label = Paragraph("Phone:", ps)
104 recipient_phone_text = Paragraph(escape(recipient_phone[:64]), ps)
106 sender_name_label = Paragraph("From:", ps)
107 sender_name_text = Paragraph(escape(sender_name[:64]), ps)
109 sender_phone_label = Paragraph("Phone:", ps)
110 sender_phone_text = Paragraph(escape(sender_phone[:64]), ps)
112 sender_email_label = Paragraph("Email:", ps)
113 sender_email_text = Paragraph(escape(sender_email[:64]), ps)
115 regarding_label = Paragraph("Regarding:", ps)
116 regarding_text = Paragraph(escape(regarding[:128]), ps)
118 date_time_label = Paragraph("Date:", ps)
119 date_time_text = Paragraph(strftime("%a, %d %b %Y %H:%M:%S (%Z)", localtime()), ps)
121 total_pages_label = Paragraph("Total Pages:", ps)
122 total_pages_text = Paragraph("%d" % total_pages, ps)
124 data = [[recipient_name_label, recipient_name_text, sender_name_label, sender_name_text],
125 [recipient_fax_label, recipient_fax_text, sender_phone_label, sender_phone_text],
126 [date_time_label, date_time_text, sender_email_label, sender_email_text],
127 [regarding_label, regarding_text, total_pages_label, total_pages_text]]
129 LIST_STYLE = TableStyle([#('LINEABOVE', (0,0), (-1,0), 2, colors.black),
130 #('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
131 #('LINEBELOW', (0,-1), (-1,-1), 2, colors.black),
132 ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
133 ('VALIGN', (0, 0), (-1, -1), 'TOP'),
136 story.append(HRFlowable(width='100%', color='black'))
138 story.append(Table(data, style=LIST_STYLE))
141 MSG_STYLE = TableStyle([#('LINEABOVE', (0,0), (-1,0), 2, colors.black),
142 #('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
143 #('LINEBELOW', (0,-1), (-1,-1), 2, colors.black),
144 ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
145 ('VALIGN', (0, 0), (-1, -1), 'TOP'),
146 ('SPAN', (-2, 1), (-1, -1)),
149 story.append(HRFlowable(width='100%', color='black'))
150 story.append(Spacer(1, 0.5*inch))
152 if preserve_formatting:
153 message = '\n'.join(message[:2048].splitlines()[:32])
155 data = [[Paragraph("Comments/Notes:", ps), ''],
156 [Preformatted(escape(message), ps), ''],]
158 data = [[Paragraph("Comments/Notes:", ps), ''],
159 [Paragraph(escape(message[:2048]), ps), ''],]
161 story.append(HRFlowable(width='100%', color='black'))
162 story.append(Table(data, style=MSG_STYLE))
163 story.append(HRFlowable(width='100%', color='black'))
165 if page_size == PAGE_SIZE_LETTER:
167 elif page_size == PAGE_SIZE_LEGAL:
173 f_fd, f = utils.make_temp_file()
177 doc = SimpleDocTemplate(f, pagesize=pgsz)
183 def createConfidentialCoverPage(page_size=PAGE_SIZE_LETTER,
194 preserve_formatting=False,
197 s = getSampleStyleSheet()
201 story.append(Image(os.path.join(prop.image_dir, 'other', 'confidential_title.png')))
202 story.append(Spacer(1, inch))
203 story.append(HRFlowable(width='100%', color='black'))
205 ps = ParagraphStyle(name='normal',
206 fontName='Times-Roman',
207 #fontName='STSong-Light',
211 recipient_name_label = Paragraph("To:", ps)
212 recipient_name_text = Paragraph(escape(recipient_name[:64]), ps)
214 recipient_fax_label = Paragraph("Fax:", ps)
215 recipient_fax_text = Paragraph(escape(recipient_fax[:64]), ps)
217 recipient_phone_label = Paragraph("Phone:", ps)
218 recipient_phone_text = Paragraph(escape(recipient_phone[:64]), ps)
220 sender_name_label = Paragraph("From:", ps)
221 sender_name_text = Paragraph(escape(sender_name[:64]), ps)
223 sender_phone_label = Paragraph("Phone:", ps)
224 sender_phone_text = Paragraph(escape(sender_phone[:64]), ps)
226 sender_email_label = Paragraph("Email:", ps)
227 sender_email_text = Paragraph(escape(sender_email[:64]), ps)
229 regarding_label = Paragraph("Regarding:", ps)
230 regarding_text = Paragraph(escape(regarding[:128]), ps)
232 date_time_label = Paragraph("Date:", ps)
233 date_time_text = Paragraph(strftime("%a, %d %b %Y %H:%M:%S (%Z)", localtime()), ps)
235 total_pages_label = Paragraph("Total Pages:", ps)
236 total_pages_text = Paragraph("%d" % total_pages, ps)
238 data = [[recipient_name_label, recipient_name_text],
239 [recipient_fax_label, recipient_fax_text],
241 [sender_name_label, sender_name_text],
242 [sender_phone_label, sender_phone_text],
243 [sender_email_label, sender_email_text],
245 [date_time_label, date_time_text],
246 [total_pages_label, total_pages_text],
247 [regarding_label, regarding_text],]
249 LIST_STYLE = TableStyle([#('LINEABOVE', (0,0), (-1,0), 2, colors.black),
250 #('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
251 #('LINEBELOW', (0,-1), (-1,-1), 2, colors.black),
252 ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
253 ('VALIGN', (0, 0), (-1, -1), 'TOP'),
257 story.append(Table(data, style=LIST_STYLE))
258 story.append(HRFlowable(width='100%', color='black'))
261 MSG_STYLE = TableStyle([#('LINEABOVE', (0,0), (-1,0), 2, colors.black),
262 #('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
263 #('LINEBELOW', (0,-1), (-1,-1), 2, colors.black),
264 ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
265 ('VALIGN', (0, 0), (-1, -1), 'TOP'),
266 #('SPAN', (-2, 1), (-1, -1)),
269 #story.append(HRFlowable(width='100%', color='black'))
270 story.append(Spacer(1, 0.5*inch))
272 # if preserve_formatting:
273 # message = '\n'.join(message[:2048].splitlines()[:32])
275 # data = [#[Paragraph("Comments/Notes:", ps), ''],
276 # [Preformatted(escape(message), ps)],]
278 # data = [#[Paragraph("Comments/Notes:", ps), ''],
279 # [Paragraph(escape(message[:2048]), ps), ''],]
281 # #story.append(HRFlowable(width='100%', color='black'))
282 # #story.append(Table(data, style=MSG_STYLE))
284 if preserve_formatting:
285 message = '\n'.join(message[:2048].splitlines()[:32])
286 story.append(Preformatted(escape(message), ps))
288 story.append(Paragraph(escape(message), ps))
291 if page_size == PAGE_SIZE_LETTER:
293 elif page_size == PAGE_SIZE_LEGAL:
299 f_fd, f = utils.make_temp_file()
303 doc = SimpleDocTemplate(f, pagesize=pgsz)
309 def createGenericCoverPage(page_size=PAGE_SIZE_LETTER,
320 preserve_formatting=False,
323 s = getSampleStyleSheet()
327 i = Image(os.path.join(prop.image_dir, 'other', 'generic_title.png'), width=250, height=147)
330 #story.append(Spacer(1, inch))
331 story.append(HRFlowable(width='100%', color='black'))
333 ps = ParagraphStyle(name='normal',
334 fontName='Times-Roman',
335 #fontName='STSong-Light',
339 recipient_name_label = Paragraph("To:", ps)
340 recipient_name_text = Paragraph(escape(recipient_name[:64]), ps)
342 recipient_fax_label = Paragraph("Fax:", ps)
343 recipient_fax_text = Paragraph(escape(recipient_fax[:64]), ps)
345 recipient_phone_label = Paragraph("Phone:", ps)
346 recipient_phone_text = Paragraph(escape(recipient_phone[:64]), ps)
348 sender_name_label = Paragraph("From:", ps)
349 sender_name_text = Paragraph(escape(sender_name[:64]), ps)
351 sender_phone_label = Paragraph("Phone:", ps)
352 sender_phone_text = Paragraph(escape(sender_phone[:64]), ps)
354 sender_email_label = Paragraph("Email:", ps)
355 sender_email_text = Paragraph(escape(sender_email[:64]), ps)
357 regarding_label = Paragraph("Regarding:", ps)
358 regarding_text = Paragraph(escape(regarding[:128]), ps)
360 date_time_label = Paragraph("Date:", ps)
361 date_time_text = Paragraph(strftime("%a, %d %b %Y %H:%M:%S (%Z)", localtime()), ps)
363 total_pages_label = Paragraph("Total Pages:", ps)
364 total_pages_text = Paragraph("%d" % total_pages, ps)
366 data = [[recipient_name_label, recipient_name_text],
367 [recipient_fax_label, recipient_fax_text],
369 [sender_name_label, sender_name_text],
370 [sender_phone_label, sender_phone_text],
371 [sender_email_label, sender_email_text],
373 [date_time_label, date_time_text],
374 [total_pages_label, total_pages_text],
375 [regarding_label, regarding_text],]
377 LIST_STYLE = TableStyle([#('LINEABOVE', (0,0), (-1,0), 2, colors.black),
378 #('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
379 #('LINEBELOW', (0,-1), (-1,-1), 2, colors.black),
380 ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
381 ('VALIGN', (0, 0), (-1, -1), 'TOP'),
385 story.append(Table(data, style=LIST_STYLE))
386 story.append(HRFlowable(width='100%', color='black'))
389 MSG_STYLE = TableStyle([#('LINEABOVE', (0,0), (-1,0), 2, colors.black),
390 #('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
391 #('LINEBELOW', (0,-1), (-1,-1), 2, colors.black),
392 ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
393 ('VALIGN', (0, 0), (-1, -1), 'TOP'),
394 #('SPAN', (-2, 1), (-1, -1)),
397 #story.append(HRFlowable(width='100%', color='black'))
398 story.append(Spacer(1, 0.5*inch))
400 # if preserve_formatting:
401 # message = '\n'.join(message[:2048].splitlines()[:32])
403 # data = [#[Paragraph("Comments/Notes:", ps), ''],
404 # [Preformatted(escape(message), ps)],]
406 # data = [#[Paragraph("Comments/Notes:", ps), ''],
407 # [Paragraph(escape(message[:2048]), ps), ''],]
409 # #story.append(HRFlowable(width='100%', color='black'))
410 # #story.append(Table(data, style=MSG_STYLE))
412 if preserve_formatting:
413 message = '\n'.join(message[:2048].splitlines()[:32])
414 story.append(Preformatted(escape(message), ps))
416 story.append(Paragraph(escape(message), ps))
420 if page_size == PAGE_SIZE_LETTER:
422 elif page_size == PAGE_SIZE_LEGAL:
428 f_fd, f = utils.make_temp_file()
432 doc = SimpleDocTemplate(f, pagesize=pgsz)
438 def createUrgentCoverPage(page_size=PAGE_SIZE_LETTER,
449 preserve_formatting=False,
452 s = getSampleStyleSheet()
455 i = Image(os.path.join(prop.image_dir, 'other', 'urgent_title.png'), width=424, height=92)
458 story.append(Spacer(1, inch))
459 story.append(HRFlowable(width='100%', color='black'))
461 ps = ParagraphStyle(name='normal',
462 fontName='Times-Roman',
463 #fontName='STSong-Light',
467 recipient_name_label = Paragraph("To:", ps)
468 recipient_name_text = Paragraph(escape(recipient_name[:64]), ps)
470 recipient_fax_label = Paragraph("Fax:", ps)
471 recipient_fax_text = Paragraph(escape(recipient_fax[:64]), ps)
473 recipient_phone_label = Paragraph("Phone:", ps)
474 recipient_phone_text = Paragraph(escape(recipient_phone[:64]), ps)
476 sender_name_label = Paragraph("From:", ps)
477 sender_name_text = Paragraph(escape(sender_name[:64]), ps)
479 sender_phone_label = Paragraph("Phone:", ps)
480 sender_phone_text = Paragraph(escape(sender_phone[:64]), ps)
482 sender_email_label = Paragraph("Email:", ps)
483 sender_email_text = Paragraph(escape(sender_email[:64]), ps)
485 regarding_label = Paragraph("Regarding:", ps)
486 regarding_text = Paragraph(escape(regarding[:128]), ps)
488 date_time_label = Paragraph("Date:", ps)
489 date_time_text = Paragraph(strftime("%a, %d %b %Y %H:%M:%S (%Z)", localtime()), ps)
491 total_pages_label = Paragraph("Total Pages:", ps)
492 total_pages_text = Paragraph("%d" % total_pages, ps)
494 data = [[recipient_name_label, recipient_name_text],
495 [recipient_fax_label, recipient_fax_text],
497 [sender_name_label, sender_name_text],
498 [sender_phone_label, sender_phone_text],
499 [sender_email_label, sender_email_text],
501 [date_time_label, date_time_text],
502 [total_pages_label, total_pages_text],
503 [regarding_label, regarding_text],]
505 LIST_STYLE = TableStyle([#('LINEABOVE', (0,0), (-1,0), 2, colors.black),
506 #('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
507 #('LINEBELOW', (0,-1), (-1,-1), 2, colors.black),
508 ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
509 ('VALIGN', (0, 0), (-1, -1), 'TOP'),
513 story.append(Table(data, style=LIST_STYLE))
514 story.append(HRFlowable(width='100%', color='black'))
517 MSG_STYLE = TableStyle([#('LINEABOVE', (0,0), (-1,0), 2, colors.black),
518 #('LINEABOVE', (0,1), (-1,-1), 0.25, colors.black),
519 #('LINEBELOW', (0,-1), (-1,-1), 2, colors.black),
520 ('ALIGN', (1,1), (-1,-1), 'RIGHT'),
521 ('VALIGN', (0, 0), (-1, -1), 'TOP'),
522 #('SPAN', (-2, 1), (-1, -1)),
525 #story.append(HRFlowable(width='100%', color='black'))
526 story.append(Spacer(1, 0.5*inch))
528 # if preserve_formatting:
529 # message = '\n'.join(message[:2048].splitlines()[:32])
531 # data = [#[Paragraph("Comments/Notes:", ps), ''],
532 # [Preformatted(escape(message), ps)],]
534 # data = [#[Paragraph("Comments/Notes:", ps), ''],
535 # [Paragraph(escape(message[:2048]), ps), ''],]
537 # #story.append(HRFlowable(width='100%', color='black'))
538 # #story.append(Table(data, style=MSG_STYLE))
540 if preserve_formatting:
541 message = '\n'.join(message[:2048].splitlines()[:32])
542 story.append(Preformatted(escape(message), ps))
544 story.append(Paragraph(escape(message), ps))
547 if page_size == PAGE_SIZE_LETTER:
549 elif page_size == PAGE_SIZE_LEGAL:
555 f_fd, f = utils.make_temp_file()
559 doc = SimpleDocTemplate(f, pagesize=pgsz)
565 # { "name" : (function, "thumbnail.png"), ... }
566 COVERPAGES = { "basic": (createStandardCoverPage, 'standard_coverpage.png'),
567 "confidential": (createConfidentialCoverPage, 'confidential_coverpage.png'),
568 "generic": (createGenericCoverPage, "generic_coverpage.png"),
569 "urgent": (createUrgentCoverPage, "urgent_coverpage.png"),
573 if __name__ == "__main__":
574 createUrgentCoverPage(page_size=PAGE_SIZE_LETTER,
576 recipient_name='Trex',
577 recipient_phone='+1 234-567-8912',
578 recipient_fax='+1 432 123 1234',
580 sender_phone='+1 234 432 1234',
581 sender_fax='+1 567 876 5123 ',
582 sender_email='test@hplip.sf.net',
583 regarding='Some sorta stuff',
584 message="""Some HP printers require proprietary software technologies to allow full access to printer features and performance. These technologies cannot be open sourced. Because of this, HP uses a binary plug-in for these printers that work in conjunction with our Linux Open Source Printing Software to improve the printing experience for HP’s Linux Printing Customers. This binary plug-in requires the user to read and agree to a license agreement at the time of driver installation. There is a single plug-in file (for each HPLIP release) for all plug-in enabled devices.""",
585 preserve_formatting=False,