Tizen 2.1 base
[platform/upstream/hplip.git] / fax / coverpages.py
1 # -*- coding: utf-8 -*-
2 #
3 # (c) Copyright 2003-2009 Hewlett-Packard Development Company, L.P.
4 #
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.
9 #
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.
14 #
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
18 #
19 # Author: Don Welch
20 #
21 import warnings
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
37 #import warnings
38 warnings.simplefilter('default', DeprecationWarning)
39 warnings.simplefilter("default", SyntaxWarning)
40
41 if __name__ ==  "__main__":
42     import sys
43     sys.path.append("..")
44
45 from base.g import *
46 from base import utils
47
48 PAGE_SIZE_LETTER = 'letter'
49 PAGE_SIZE_LEGAL = 'legal'
50 PAGE_SIZE_A4 = 'a4'
51
52
53 def escape(s):
54     return s.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
55
56
57 def createStandardCoverPage(page_size=PAGE_SIZE_LETTER,
58                             total_pages=1,
59                             recipient_name='',
60                             recipient_phone='',
61                             recipient_fax='',
62                             sender_name='',
63                             sender_phone='',
64                             sender_fax='',
65                             sender_email='',
66                             regarding='',
67                             message='',
68                             preserve_formatting=False,
69                             output=None):
70
71     s = getSampleStyleSheet()
72
73     story = []
74
75     #print prop.locale
76     #TTFSearchPath.append('/usr/share/fonts/truetype/arphic')
77     #pdfmetrics.registerFont(TTFont('UMing', 'uming.ttf'))
78
79     ps = ParagraphStyle(name="title",
80                         parent=None,
81                         fontName='helvetica-bold',
82                         #fontName='STSong-Light',
83                         #fontName = 'UMing',
84                         fontSize=72,
85                         )
86
87     story.append(Paragraph("FAX", ps))
88
89     story.append(Spacer(1, inch))
90
91     ps = ParagraphStyle(name='normal',
92                         fontName='Times-Roman',
93                         #fontName='STSong-Light',
94                         #fontName='UMing',
95                         fontSize=12)
96
97     recipient_name_label = Paragraph("To:", ps)
98     recipient_name_text = Paragraph(escape(recipient_name[:64]), ps)
99
100     recipient_fax_label = Paragraph("Fax:", ps)
101     recipient_fax_text = Paragraph(escape(recipient_fax[:64]), ps)
102
103     recipient_phone_label = Paragraph("Phone:", ps)
104     recipient_phone_text = Paragraph(escape(recipient_phone[:64]), ps)
105
106     sender_name_label = Paragraph("From:", ps)
107     sender_name_text = Paragraph(escape(sender_name[:64]), ps)
108
109     sender_phone_label = Paragraph("Phone:", ps)
110     sender_phone_text = Paragraph(escape(sender_phone[:64]), ps)
111
112     sender_email_label = Paragraph("Email:", ps)
113     sender_email_text = Paragraph(escape(sender_email[:64]), ps)
114
115     regarding_label = Paragraph("Regarding:", ps)
116     regarding_text = Paragraph(escape(regarding[:128]), ps)
117
118     date_time_label = Paragraph("Date:", ps)
119     date_time_text = Paragraph(strftime("%a, %d %b %Y %H:%M:%S (%Z)", localtime()), ps)
120
121     total_pages_label = Paragraph("Total Pages:", ps)
122     total_pages_text = Paragraph("%d" % total_pages, ps)
123
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]]
128
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'),
134                             ])
135
136     story.append(HRFlowable(width='100%', color='black'))
137
138     story.append(Table(data, style=LIST_STYLE))
139
140     if message:
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)),
147                                 ])
148
149         story.append(HRFlowable(width='100%', color='black'))
150         story.append(Spacer(1, 0.5*inch))
151
152         if preserve_formatting:
153             message = '\n'.join(message[:2048].splitlines()[:32])
154
155             data = [[Paragraph("Comments/Notes:", ps), ''],
156                     [Preformatted(escape(message), ps), ''],]
157         else:
158             data = [[Paragraph("Comments/Notes:", ps), ''],
159                     [Paragraph(escape(message[:2048]), ps), ''],]
160
161         story.append(HRFlowable(width='100%', color='black'))
162         story.append(Table(data, style=MSG_STYLE))
163         story.append(HRFlowable(width='100%', color='black'))
164
165     if page_size == PAGE_SIZE_LETTER:
166         pgsz = letter
167     elif page_size == PAGE_SIZE_LEGAL:
168         pgsz = legal
169     else:
170         pgsz = A4
171
172     if output is None:
173         f_fd, f = utils.make_temp_file()
174     else:
175         f = output
176
177     doc = SimpleDocTemplate(f, pagesize=pgsz)
178     doc.build(story)
179
180     return f
181
182
183 def createConfidentialCoverPage(page_size=PAGE_SIZE_LETTER,
184                             total_pages=1,
185                             recipient_name='',
186                             recipient_phone='',
187                             recipient_fax='',
188                             sender_name='',
189                             sender_phone='',
190                             sender_fax='',
191                             sender_email='',
192                             regarding='',
193                             message='',
194                             preserve_formatting=False,
195                             output=None):
196
197     s = getSampleStyleSheet()
198
199     story = []
200
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'))
204
205     ps = ParagraphStyle(name='normal',
206                         fontName='Times-Roman',
207                         #fontName='STSong-Light',
208                         #fontName='UMing',
209                         fontSize=12)
210
211     recipient_name_label = Paragraph("To:", ps)
212     recipient_name_text = Paragraph(escape(recipient_name[:64]), ps)
213
214     recipient_fax_label = Paragraph("Fax:", ps)
215     recipient_fax_text = Paragraph(escape(recipient_fax[:64]), ps)
216
217     recipient_phone_label = Paragraph("Phone:", ps)
218     recipient_phone_text = Paragraph(escape(recipient_phone[:64]), ps)
219
220     sender_name_label = Paragraph("From:", ps)
221     sender_name_text = Paragraph(escape(sender_name[:64]), ps)
222
223     sender_phone_label = Paragraph("Phone:", ps)
224     sender_phone_text = Paragraph(escape(sender_phone[:64]), ps)
225
226     sender_email_label = Paragraph("Email:", ps)
227     sender_email_text = Paragraph(escape(sender_email[:64]), ps)
228
229     regarding_label = Paragraph("Regarding:", ps)
230     regarding_text = Paragraph(escape(regarding[:128]), ps)
231
232     date_time_label = Paragraph("Date:", ps)
233     date_time_text = Paragraph(strftime("%a, %d %b %Y %H:%M:%S (%Z)", localtime()), ps)
234
235     total_pages_label = Paragraph("Total Pages:", ps)
236     total_pages_text = Paragraph("%d" % total_pages, ps)
237
238     data = [[recipient_name_label, recipient_name_text],
239             [recipient_fax_label, recipient_fax_text],
240             ['', ''],
241             [sender_name_label, sender_name_text],
242             [sender_phone_label, sender_phone_text],
243             [sender_email_label, sender_email_text],
244             ['', ''],
245             [date_time_label, date_time_text],
246             [total_pages_label, total_pages_text],
247             [regarding_label, regarding_text],]
248
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'),
254                             ])
255
256
257     story.append(Table(data, style=LIST_STYLE))
258     story.append(HRFlowable(width='100%', color='black'))
259
260     if message:
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)),
267                                 ])
268
269         #story.append(HRFlowable(width='100%', color='black'))
270         story.append(Spacer(1, 0.5*inch))
271
272 #        if preserve_formatting:
273 #            message = '\n'.join(message[:2048].splitlines()[:32])
274 #
275 #            data = [#[Paragraph("Comments/Notes:", ps), ''],
276 #                    [Preformatted(escape(message), ps)],]
277 #        else:
278 #            data = [#[Paragraph("Comments/Notes:", ps), ''],
279 #                    [Paragraph(escape(message[:2048]), ps), ''],]
280 #
281 #        #story.append(HRFlowable(width='100%', color='black'))
282 #        #story.append(Table(data, style=MSG_STYLE))
283
284         if preserve_formatting:
285             message = '\n'.join(message[:2048].splitlines()[:32])
286             story.append(Preformatted(escape(message), ps))
287         else:
288             story.append(Paragraph(escape(message), ps))
289
290
291     if page_size == PAGE_SIZE_LETTER:
292         pgsz = letter
293     elif page_size == PAGE_SIZE_LEGAL:
294         pgsz = legal
295     else:
296         pgsz = A4
297
298     if output is None:
299         f_fd, f = utils.make_temp_file()
300     else:
301         f = output
302
303     doc = SimpleDocTemplate(f, pagesize=pgsz)
304     doc.build(story)
305
306     return f
307
308
309 def createGenericCoverPage(page_size=PAGE_SIZE_LETTER,
310                             total_pages=1,
311                             recipient_name='',
312                             recipient_phone='',
313                             recipient_fax='',
314                             sender_name='',
315                             sender_phone='',
316                             sender_fax='',
317                             sender_email='',
318                             regarding='',
319                             message='',
320                             preserve_formatting=False,
321                             output=None):
322
323     s = getSampleStyleSheet()
324
325     story = []
326
327     i = Image(os.path.join(prop.image_dir, 'other', 'generic_title.png'), width=250, height=147)
328     i.hAlign = 'LEFT'
329     story.append(i)
330     #story.append(Spacer(1, inch))
331     story.append(HRFlowable(width='100%', color='black'))
332
333     ps = ParagraphStyle(name='normal',
334                         fontName='Times-Roman',
335                         #fontName='STSong-Light',
336                         #fontName='UMing',
337                         fontSize=12)
338
339     recipient_name_label = Paragraph("To:", ps)
340     recipient_name_text = Paragraph(escape(recipient_name[:64]), ps)
341
342     recipient_fax_label = Paragraph("Fax:", ps)
343     recipient_fax_text = Paragraph(escape(recipient_fax[:64]), ps)
344
345     recipient_phone_label = Paragraph("Phone:", ps)
346     recipient_phone_text = Paragraph(escape(recipient_phone[:64]), ps)
347
348     sender_name_label = Paragraph("From:", ps)
349     sender_name_text = Paragraph(escape(sender_name[:64]), ps)
350
351     sender_phone_label = Paragraph("Phone:", ps)
352     sender_phone_text = Paragraph(escape(sender_phone[:64]), ps)
353
354     sender_email_label = Paragraph("Email:", ps)
355     sender_email_text = Paragraph(escape(sender_email[:64]), ps)
356
357     regarding_label = Paragraph("Regarding:", ps)
358     regarding_text = Paragraph(escape(regarding[:128]), ps)
359
360     date_time_label = Paragraph("Date:", ps)
361     date_time_text = Paragraph(strftime("%a, %d %b %Y %H:%M:%S (%Z)", localtime()), ps)
362
363     total_pages_label = Paragraph("Total Pages:", ps)
364     total_pages_text = Paragraph("%d" % total_pages, ps)
365
366     data = [[recipient_name_label, recipient_name_text],
367             [recipient_fax_label, recipient_fax_text],
368             ['', ''],
369             [sender_name_label, sender_name_text],
370             [sender_phone_label, sender_phone_text],
371             [sender_email_label, sender_email_text],
372             ['', ''],
373             [date_time_label, date_time_text],
374             [total_pages_label, total_pages_text],
375             [regarding_label, regarding_text],]
376
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'),
382                             ])
383
384
385     story.append(Table(data, style=LIST_STYLE))
386     story.append(HRFlowable(width='100%', color='black'))
387
388     if message:
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)),
395                                 ])
396
397         #story.append(HRFlowable(width='100%', color='black'))
398         story.append(Spacer(1, 0.5*inch))
399
400 #        if preserve_formatting:
401 #            message = '\n'.join(message[:2048].splitlines()[:32])
402 #
403 #            data = [#[Paragraph("Comments/Notes:", ps), ''],
404 #                    [Preformatted(escape(message), ps)],]
405 #        else:
406 #            data = [#[Paragraph("Comments/Notes:", ps), ''],
407 #                    [Paragraph(escape(message[:2048]), ps), ''],]
408 #
409 #        #story.append(HRFlowable(width='100%', color='black'))
410 #        #story.append(Table(data, style=MSG_STYLE))
411
412         if preserve_formatting:
413             message = '\n'.join(message[:2048].splitlines()[:32])
414             story.append(Preformatted(escape(message), ps))
415         else:
416             story.append(Paragraph(escape(message), ps))
417
418     #
419
420     if page_size == PAGE_SIZE_LETTER:
421         pgsz = letter
422     elif page_size == PAGE_SIZE_LEGAL:
423         pgsz = legal
424     else:
425         pgsz = A4
426
427     if output is None:
428         f_fd, f = utils.make_temp_file()
429     else:
430         f = output
431
432     doc = SimpleDocTemplate(f, pagesize=pgsz)
433     doc.build(story)
434
435     return f
436
437
438 def createUrgentCoverPage(page_size=PAGE_SIZE_LETTER,
439                             total_pages=1,
440                             recipient_name='',
441                             recipient_phone='',
442                             recipient_fax='',
443                             sender_name='',
444                             sender_phone='',
445                             sender_fax='',
446                             sender_email='',
447                             regarding='',
448                             message='',
449                             preserve_formatting=False,
450                             output=None):
451
452     s = getSampleStyleSheet()
453
454     story = []
455     i = Image(os.path.join(prop.image_dir, 'other', 'urgent_title.png'), width=424, height=92)
456     i.hAlign = 'LEFT'
457     story.append(i)
458     story.append(Spacer(1, inch))
459     story.append(HRFlowable(width='100%', color='black'))
460
461     ps = ParagraphStyle(name='normal',
462                         fontName='Times-Roman',
463                         #fontName='STSong-Light',
464                         #fontName='UMing',
465                         fontSize=12)
466
467     recipient_name_label = Paragraph("To:", ps)
468     recipient_name_text = Paragraph(escape(recipient_name[:64]), ps)
469
470     recipient_fax_label = Paragraph("Fax:", ps)
471     recipient_fax_text = Paragraph(escape(recipient_fax[:64]), ps)
472
473     recipient_phone_label = Paragraph("Phone:", ps)
474     recipient_phone_text = Paragraph(escape(recipient_phone[:64]), ps)
475
476     sender_name_label = Paragraph("From:", ps)
477     sender_name_text = Paragraph(escape(sender_name[:64]), ps)
478
479     sender_phone_label = Paragraph("Phone:", ps)
480     sender_phone_text = Paragraph(escape(sender_phone[:64]), ps)
481
482     sender_email_label = Paragraph("Email:", ps)
483     sender_email_text = Paragraph(escape(sender_email[:64]), ps)
484
485     regarding_label = Paragraph("Regarding:", ps)
486     regarding_text = Paragraph(escape(regarding[:128]), ps)
487
488     date_time_label = Paragraph("Date:", ps)
489     date_time_text = Paragraph(strftime("%a, %d %b %Y %H:%M:%S (%Z)", localtime()), ps)
490
491     total_pages_label = Paragraph("Total Pages:", ps)
492     total_pages_text = Paragraph("%d" % total_pages, ps)
493
494     data = [[recipient_name_label, recipient_name_text],
495             [recipient_fax_label, recipient_fax_text],
496             ['', ''],
497             [sender_name_label, sender_name_text],
498             [sender_phone_label, sender_phone_text],
499             [sender_email_label, sender_email_text],
500             ['', ''],
501             [date_time_label, date_time_text],
502             [total_pages_label, total_pages_text],
503             [regarding_label, regarding_text],]
504
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'),
510                             ])
511
512
513     story.append(Table(data, style=LIST_STYLE))
514     story.append(HRFlowable(width='100%', color='black'))
515
516     if message:
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)),
523                                 ])
524
525         #story.append(HRFlowable(width='100%', color='black'))
526         story.append(Spacer(1, 0.5*inch))
527
528 #        if preserve_formatting:
529 #            message = '\n'.join(message[:2048].splitlines()[:32])
530 #
531 #            data = [#[Paragraph("Comments/Notes:", ps), ''],
532 #                    [Preformatted(escape(message), ps)],]
533 #        else:
534 #            data = [#[Paragraph("Comments/Notes:", ps), ''],
535 #                    [Paragraph(escape(message[:2048]), ps), ''],]
536 #
537 #        #story.append(HRFlowable(width='100%', color='black'))
538 #        #story.append(Table(data, style=MSG_STYLE))
539
540         if preserve_formatting:
541             message = '\n'.join(message[:2048].splitlines()[:32])
542             story.append(Preformatted(escape(message), ps))
543         else:
544             story.append(Paragraph(escape(message), ps))
545
546
547     if page_size == PAGE_SIZE_LETTER:
548         pgsz = letter
549     elif page_size == PAGE_SIZE_LEGAL:
550         pgsz = legal
551     else:
552         pgsz = A4
553
554     if output is None:
555         f_fd, f = utils.make_temp_file()
556     else:
557         f = output
558
559     doc = SimpleDocTemplate(f, pagesize=pgsz)
560     doc.build(story)
561
562     return f
563
564
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"),
570              }
571
572
573 if __name__ ==  "__main__":
574     createUrgentCoverPage(page_size=PAGE_SIZE_LETTER,
575                                 total_pages=1,
576                                 recipient_name='Trex',
577                                 recipient_phone='+1 234-567-8912',
578                                 recipient_fax='+1 432 123 1234',
579                                 sender_name='Don',
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,
586                                 output="output.pdf")
587
588