-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmbox2pdf.pl
executable file
·1418 lines (1033 loc) · 35.5 KB
/
mbox2pdf.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
# Mail and MIME Handling
use Mail::IMAPClient;
use Mail::Mbox::MessageParser;
use MIME::Parser;
use MIME::Words qw(:all);
use MIME::Body;
use MIME::Base64;
# Other stuff
use Date::Parse;
use Getopt::Long;
use File::Path;
use Digest::MD5 qw(md5_hex);
# PDF stuff
use PDF::API2;
use PDF::TextBlock;
# Encoding
use URI::Escape;
use Encode;
use utf8;
# Image Manipulatin
use Image::Magick;
binmode STDOUT, ":encoding(UTF-8)";
# --------------------------------------------------
# Global Variables
# --------------------------------------------------
my $mboxfile;
my $verbose;
my $debug;
my $type;
my $hash;
my $help;
my $testlimit = 0;
my $year = -1;
my $start = 0;
my $end = 0;
my $found_video = 0;
# where to save tmp files
our $tmp_dir_hash;
# Constant for Page size
#These make measurements easier when using PDF::API2. PDF primarily uses points to measure sizes and distances, so if we define these we can use them later to use other units. For example 5/mm returns 5 millimeters in points. The points (pt) is given just so we can clearly state when we're talking in points.
#Note that these are not necessary, but make it easier to create PDF files in perl. For the technically minded: There are 72 postscript points in an inch and there are 25.4 millimeters in an inch.
use constant DPI => 300; # how many postscript points in an inch
use constant mm => 25.4 / DPI; # 1 mm in points
use constant in => 1 / DPI; # how many points in an inch
use constant pt => 1; # 1 point
use constant DENSITY => "300"; # DPI
# Page Size in mm
use constant A4_x => 210; # x points in an A4 page ( 595.2755 )
use constant A4_y => 297; # y points in an A4 page ( 841.8897 )
use constant A5_x => 148; # x points in an A5 page ( 420 )
use constant A5_y => 210; # y points in an A5 page ( 595 )
use constant A6_x => 105; # x points in an A6 page ( 298 )
use constant A6_y => 148; # y points in an A6 page ( 420 )
# mediabox - the size of our paper in points
my $size_x = A4_x/mm;
my $size_y = A4_y/mm;
# cropbox - the size we'll cut the paper down to at the end
my $crop_size = 3/mm;
my $crop_left = $crop_size;
my $crop_bottom = $crop_size;
my $crop_right = $size_x - $crop_size;
my $crop_top = $size_y - $crop_size;
# Draw a Infobox with Background, or just a line
my $ADD_INFOBOX = "true";
# Infobox size in Percent of Page / 5 %
my $INFOBOX_BOTTOM = $size_y - ($size_y * 0.05);
my $INFOBOX_HEIGHT = $size_y - $INFOBOX_BOTTOM;
# buffer, so resized pic placed well on content part
my $x_buffer = 50;
my $y_buffer = 50;
# Font size
my $headline_font_size = 120/pt;
my $date_font_size = 60/pt;
my $from_font_size = 30/pt;
my $text_font_size = "";
my $verbose_font_size = 30/pt;
my $scale = 1;
# some arrays
our @text;
our @images;
our $text_as_line;
# Include some vars from config.pl
my %config = do '/Users/markus/git/mail2pdf/config.pl';
my $username = $config{username} or die("missing username from config.pl");
my $oauth_token = $config{oauth_token} or die("missing oauth_token from config.pl");
my $path = $config{path} or die("missing path from config.pl");
my $filename = $config{filename} or die("missing filename");
my $s3mount = $config{s3mount};
# --------------------------------------------------
# Getopt definition
# --------------------------------------------------
GetOptions( "mboxfile=s" => \$mboxfile, # string
"verbose" => \$verbose,
"debug" => \$debug,
"help" => \$help,
"type=s" => \$type,
"hash=s" => \$hash,
"filename=s" => \$filename,
"path=s" => \$path,
"testlimit=s" => \$testlimit,
"year=i" => \$year,
) # flag
or die("Error in command line arguments\n");
if(!$type or $help) {
print "./mbox2pdf --help\n\n";
print "--mboxfile=FILE choose mbox file\n";
print "--verbose enable verbose logging\n";
print "--debug enable debugging\n";
print "--type (mbox|imap|s3mount) choose whether you want to use a local mbox file,a remote imap account or a directory with files per each email\n";
print "--testlimit=Start(,End) choose at which position you want to start to generate the pdf file\n";
print "--year=YEAR only print YEAR Content to PDF\n";
exit;
}
print Dumper \%config if($verbose);
# Some Logging
logging("VERBOSE", "Size: x: '$size_x' y: '$size_y' CropSize: '$crop_size' Infobox Bottom: '$INFOBOX_BOTTOM' Height: '$INFOBOX_HEIGHT' DPI: '".DPI."' scale '$scale'");
# Testlimit is set
if($testlimit =~ /([\d]+),([\d]+)/) {
$start = $1;
$end = $2;
$testlimit = $2;
if($start > $end) {
$end = $start + $end;
logging("INFO", "End looks like an Offset. Recalculate end ($end)");
}
logging("VERBOSE", "Testlimit between Message '$start' '$end'");
}
MIME::Tools->debugging(1) if($debug);
MIME::Tools->quiet(0) if($verbose);
if($type eq "mbox") {
# --------------------------------------------------
# Check file
# --------------------------------------------------
if (!check_mbox_file($mboxfile)) {
error("FATAL", "File '$mboxfile' does not fit");
exit;
}
# --------------------------------------------------
# new FileObjekt
# --------------------------------------------------
my $filehandle = new FileHandle($mboxfile);
# Set up cache
# Mail::Mbox::MessageParser::SETUP_CACHE( { 'file_name' => '/tmp/cache' } );
# --------------------------------------------------
# new MboxParser Objekt
# --------------------------------------------------
my $mbox = new Mail::Mbox::MessageParser( {
'file_name' => $mboxfile,
'file_handle' => $filehandle,
'enable_cache' => 0,
'enable_grep' => 1,
'debug' => $debug,
} );
die $mbox unless ref $mbox;
# --------------------------------------------------
# Any newlines or such before the start of the first email
# --------------------------------------------------
my $prologue = $mbox->prologue;
# --------------------------------------------------
# value for logging
# --------------------------------------------------
my $email_count = 1;
# --------------------------------------------
# create a pdf file / pdf object $pdf
# --------------------------------------------
my $pdf = pdf_file("", "create");
## Embed a TTF
my $courier = $pdf->ttfont('/Library/Fonts/Courier New.ttf');
my $courier_bold = $pdf->ttfont('/Library/Fonts/Courier New Bold.ttf');
# --------------------------------------------------
# This is the main loop. It's executed once for each email
# --------------------------------------------------
while(! $mbox->end_of_file() )
{
# last if($email_count > $testlimit);
logging("VERBOSE", "Start Parsing Email '$email_count'");
# Fetch Email Content
my $content = $mbox->read_next_email();
# if in testlimit mode, check, whether to add this email
# or not
my $res = handle_testlimit($email_count, $testlimit, $start, $end);
# if handle_testlimit skips email, go to next one
next if ($res == 0);
my $parser = new MIME::Parser;
$parser->ignore_errors(0);
$parser->output_to_core(0);
### Tell it where to put things:
$parser->output_under("/tmp");
my $entity = $parser->parse_data($content);
my $header = $entity->head;
# Sanity checks
next if ($header->get('From') =~ /facebook/);
my $error = ($@ || $parser->last_error);
handle_mime_body($email_count,$entity);
pdf_add_email($pdf, $header, $email_count);
$email_count++;
}
pdf_file($pdf, "close");
}
elsif($type eq "imap") {
logging("VERBOSE", "type: imap\n");
# connect to google imap server
my $imap = gmail($oauth_token, $username);
# choose label/folder
my $folder = "Inbox";
$imap->exists($folder) or warn "$folder not found: $@\n";
# select folder
$imap->select($folder) or warn "$folder not select: $@\n";
# how many messages are their?
my $msgcount = $imap->message_count($folder);
defined($msgcount) or die "Could not message_count: $@\n";
logging("VERBOSE", "msg count = '$msgcount'");
# --------------------------------------------
# create a pdf file / pdf object $pdf
# --------------------------------------------
my $pdf = pdf_file("", "create");
## Embed a TTF
my $courier = $pdf->ttfont('/Library/Fonts/Courier New.ttf');
my $courier_bold = $pdf->ttfont('/Library/Fonts/Courier New Bold.ttf');
# import messages
my @msgs = $imap->messages() or die "Could not messages: $@\n";
# generate a /tmp/ subdirectory ..
$tmp_dir_hash = md5_hex( $imap . $pdf . $msgcount );
mkdir "/tmp/" . $tmp_dir_hash;
logging("VERBOSE", "create tmp sub dirctory /tmp/$tmp_dir_hash");
# loop
my $msg_cnt = 0;
foreach my $i (@msgs)
{
# increase email/message count
$msg_cnt++;
# if in testlimit mode, check, whether to add this email
# or not
my $res = handle_testlimit($msg_cnt, $testlimit, $start, $end);
# if handle_testlimit skips email, go to next one
next if ($res == 0);
logging("VERBOSE", "\n++++++++++++++++++++++++++++++++++++++++++++++++++++");
logging("VERBOSE", "IMAP Message $msg_cnt from $msgcount");
# if in year mode, check if email year match
logging("DEBUG", "Fetch Date Header");
my $date = $imap->get_header($i, "Date");
# return 0: ignore | return 1: match
my $res_hoy = handle_option_year($year, $date);
if($res_hoy == 0) {
logging("DEBUG", "handle_option_year: ignore email based on year ($year)");
next;
}
# get message content
logging("DEBUG", "Fetch message");
my $content = $imap->message_string($i);
# start MIME Parser
my $parser = new MIME::Parser;
$parser->ignore_errors(0);
$parser->output_to_core(0);
# tell it where to put things
$parser->output_under("/tmp");
my $entity = $parser->parse_data($content);
my $header = $entity->head;
# Sanity checks
# e.g. if email from facebook -> ignore it
next if ($header->get('From') =~ /facebook/);
# if error, get it
my $error = ($@ || $parser->last_error);
# handle body
handle_mime_body($i,$entity);
# add email to pdf
pdf_add_email($pdf, $header, $courier, $courier_bold, $msg_cnt);
undef $parser;
}
pdf_file($pdf, "close");
}
elsif($type eq "s3mount") {
my $dir = sprintf("/mnt/s3/%s/", $hash);
logging("VERBOSE", "opening $dir. Looking for files");
opendir my $mount, $dir or die "Cannot open directory: '$!' ($dir)";
my @files = readdir $mount;
closedir $mount;
# how many messages are their?
my $msgcount = @files;
defined($msgcount) or die "Could not message_count: $@\n";
logging("VERBOSE", "msg count = '$msgcount'");
# --------------------------------------------------
# value for logging
# --------------------------------------------------
my $msg_cnt = 0;
# --------------------------------------------
# create a pdf file / pdf object $pdf
# --------------------------------------------
my $pdf = pdf_file("", "create");
## Embed a TTF
my $courier = $pdf->ttfont('/Library/Fonts/Courier New.ttf');
my $courier_bold = $pdf->ttfont('/Library/Fonts/Courier New Bold.ttf');
# generate a /tmp/ subdirectory ..
$tmp_dir_hash = md5_hex( $mount . $pdf . $hash );
mkdir "/tmp/" . $tmp_dir_hash;
# Parser Object
my $parser = new MIME::Parser;
$parser->ignore_errors(0);
$parser->output_to_core(0);
### Tell it where to put things:
$parser->output_under("/tmp");
# walk through the array of files
foreach(@files){
if (-f $dir . "/" . $_ ){
# increase email/message count
$msg_cnt++;
# if in testlimit mode, check, whether to add this email
# or not
my $res = handle_testlimit($msg_cnt, $testlimit, $start, $end);
# if handle_testlimit skips email, go to next one
next if ($res == 0);
logging("VERBOSE", $_ . " : file\n");
# which file to work on, add dir to loop/foreach value
my $file = $dir . "/" . $_;
# Handler
my $entity = $parser->parse_open($file);
my $header = $entity->head;
# Sanity checks
next if ($header->get('From') =~ /facebook/);
my $error = ($@ || $parser->last_error);
handle_mime_body($msg_cnt, $entity);
pdf_add_email($pdf, $header, $courier, $courier_bold, $msg_cnt);
} elsif(-d $dir . "/" . $_){
logging("VERBOSE", $_ . " : folder\n");
next;
} else{
logging("VERBOSE", $_ . " : other\n");
next;
}
}
pdf_file($pdf, "close");
}
else {
print "Error: wrong type '$type'\n";
print "Please choose --type imap|mbox\n";
exit;
}
print "File was generated. Have fun\n";
exit;
# --------------------------------------
# Handle year if option for a special
# year is set
# --------------------------------------
sub handle_option_year {
my ($year, $date) = @_;
# no getopts for value year
return -1 if($year == -1);
# extract year from email date line (RFC822 format)
my ($ss,$mm,$hh,$day,$month,$emailyear,$zone) = strptime($date);
# Have to add offset 1900
$emailyear = $emailyear + 1900;
if($emailyear && $emailyear != $year ) {
logging("VERBOSE", "option 'year - $year' is active and this email is from '$emailyear' - skip");
return 0;
}
return 1;
}
# --------------------------------------
# Handle Testlimit
# --------------------------------------
sub handle_testlimit {
my ($msg, $testlimit, $start, $end) = @_;
# Check Options (testlimit) for debugging
if($testlimit > 0 ) {
if($start > 0 && $end > 0 ) {
# process
if($msg >= $start && $msg <= $end ) {
logging("DEBUG", "testlimit ($start,$end) - process email number '$msg'");
return 1;
}
# skip processing
else {
logging("DEBUG", "testlimit ($start,$end) - skip email number '$msg'");
return 0;
}
last if($msg > $end);
}
else {
# stop processing
logging("VERBOSE", "Stop processing .. '$msg > $testlimit' ");
if($msg > $testlimit) {
return 0;
}
}
}
return 1;
}
# --------------------------------------------------------
# Handle Body
# --------------------------------------------------------
sub handle_mime_body {
my $email_count = shift;
my $entity = shift;
my $nested = shift || 0;
my $plain_body = "";
my $html_body = "";
my $content_type;
$found_video = 0;
# erase global array content
# only, if not a nested multipart handling
if($nested == 0) {
logging("DEBUG", "handle_mime_body: is nested - $nested");
@text = ();
@images = ();
$text_as_line = "";
}
logging("DEBUG", "handle_mime_body: entity->parts " . $entity->parts);
# --------------------------------------------
# get email body
# --------------------------------------------
if ($entity->parts > 0){
for (my $i=0; $i<$entity->parts; $i++){
# Mime Parts
my $subentity = $entity->parts($i);
# --------------------------------------
# Content Type of Part
# --------------------------------------
my $ct = $subentity->mime_type;
logging("DEBUG", "handle_mime_body: mime_type " . $ct);
# For "singlepart" types (text/*, image/*, etc.), the unencoded body data is referenced
# via a MIME::Body object, accessed via the bodyhandle() method
if($ct =~ "text/plain") {
# -----------------------------------
# Get the text as list
# -----------------------------------
my @lines = $subentity->bodyhandle->as_lines;
foreach(@lines) {
my $text = handle_text($_);
if(defined $text && length($text) > 0) {
$text_as_line = $text_as_line . $text . " ";
logging("VERBOSE", "Part '$i' - Adding Content Type '$ct' '$text'");
}
else {
logging("VERBOSE", "skip Mailfooter ..");
last;
}
}
}
if($ct =~ "image") {
my $path = $subentity->bodyhandle->path;
my $image = Image::Magick->new(magick=>'JPEG');
$image->Read($path);
my $width = $image->Get('width');
my $height = $image->Get('height');
logging("VERBOSE", "Part '$i' - Size '$width' x '$height' Adding Content Type '$ct' '$path' ");
# Todo: Change to hash and add Image Size
push(@images, $path);
}
if($ct =~ "text/html") {
logging("VERBOSE", "Part $i - Type '$ct'");
}
if($ct =~ "video") {
$found_video++;
logging("VERBOSE", "Part $i - Type '$ct'");
}
# nested multipart in an subentity
if( $ct =~"multipart/related" ) {
handle_mime_body($email_count, $subentity, 1);
logging("VERBOSE", "Part $i - Type '$ct'");
}
}
}
else {
logging("INFO", "No Body-Part found");
return 0;
}
# Return array be reference
return 1;
}
# --------------------------------------------------------
# Handle PDF
# --------------------------------------------------------
sub pdf_file {
my $pdf = shift;
my $task = shift;
# Values are included in config.pl
my $filename = $path . $filename;
# ---------------------------------------------------
# Create PDF object
# ---------------------------------------------------
if($task eq "create") {
my $pdf;
logging("VERBOSE", "create file '$filename'");
logging("DEBUG", "creating PDF '$filename'");
$pdf = PDF::API2->new( -file => "$filename" );
return $pdf;
}
elsif($task eq "close") {
$pdf->save;
$pdf->end;
logging("VERBOSE", "Remove /tmp/" . $tmp_dir_hash . "/");
rmtree "/tmp/".$tmp_dir_hash."/" or warn("Could not delete, not empty");
}
elsif($task eq "delete") {
unlink $filename;
}
else {
logging("ERROR", "Wrong task");
}
}
# --------------------------------------------------------
# Add Email to an existing PDF File
# Each Email should be one page
# --------------------------------------------------------
sub pdf_add_email {
my $pdf = shift;
my $header = shift;
my $courier = shift;
my $courier_bold = shift;
my $email_count = shift;
# get date headers
my $date = $header->get('Date');
# Convert Date
# the year is the number of years since 1900, and the month is zero-based (0 = January)
my ($ss,$mm,$hh,$day,$month,$emailyear,$zone) = strptime($date);
$date = sprintf("%d.%d", $day, $month + 1);
chomp($date);
$emailyear = $emailyear + 1900;
# get more headers and check content if neccessary
my $subject = $header->get('Subject');
my $to = $header->get('To');
chomp($to);
my $from = $header->get('From');
chomp($from);
my ($name, $email) = check_from($from);
my $contenttype = $header->get("Content-Type");
chomp($contenttype);
# Logging
logging("VERBOSE", "'$date' Email from '$from'");
# Add new Page
my $page = $pdf->page;
# printting details
$page->mediabox( 0,0, $size_x, $size_y);
#$page->cropbox( $crop_left, $crop_bottom, $crop_right, $crop_top );
# Make InfoBox variable
# Add a box with background color
if($ADD_INFOBOX) {
my $color_box = $page->gfx;
$color_box->fillcolor('orange');
$color_box->rect( 0 , # left
$INFOBOX_BOTTOM, # bottom
$size_x, # width
$INFOBOX_HEIGHT); # height
$color_box->fill;
# or just space with a line
} else {
my $line = $page->gfx;
$line->strokecolor('black');
$line->linewidth(5);
$line->move( 0, $INFOBOX_BOTTOM );
$line->line( $size_x, $INFOBOX_BOTTOM );
$line->stroke;
}
# Debug/Verbose - Mode: print Email-Number on Page
if($verbose || $debug) {
my $headline_page_count = $page->text;
$headline_page_count->font( $courier_bold , $verbose_font_size);
$headline_page_count->fillcolor('black');
$headline_page_count->translate( $size_x * 0.1 , $size_y - ($INFOBOX_HEIGHT * 0.9) );
$headline_page_count->text_center($email_count);
}
# Headline Information
#
# Todo: calculate the position new/correct based on font size etc
#
# Date
my $headline_date = $page->text;
$headline_date->font( $courier , $date_font_size);
$headline_date->fillcolor('black');
$headline_date->translate( $size_x * 0.08 , $size_y - ( $INFOBOX_HEIGHT * 0.7 ));
$headline_date->text_center($date);
# Year
my $headline_year = $page->text;
$headline_year->font( $courier, $date_font_size);
$headline_year->fillcolor('black');
$headline_year->translate( $size_x - ($size_x * 0.02) , $size_y - ( $INFOBOX_HEIGHT * 0.7 ) );
$headline_year->text_right($emailyear);
# --------------------------------------
# print subject
# --------------------------------------
if($subject) {
# Fix encoding
chomp($subject);
if( $subject =~ /.*(utf-8|utf8|UTF-8|UTF8).*/) {
my $decoded = decode_mimewords($subject);
$subject = decode("utf8", $decoded);
logging("VERBOSE", "Subject encoding is utf8 .. decoded - '$subject'");
}
my $tb = PDF::TextBlock->new({
pdf => $pdf,
page => $page,
text => $subject,
align => 'justify',
x => $size_x * 0.15,
y => $size_y - ( $INFOBOX_HEIGHT * 0.7 ),
w => $size_x * 0.8,
h => $INFOBOX_HEIGHT * 0.7,
lead => 35 * 1.2,
fonts => {
default => PDF::TextBlock::Font->new({
pdf => $pdf,
font => $courier,
size => 35,
}),
},
});
my($endw, $ypos, $overflow)= $tb->apply();
logging("VERBOSE", "subject - X:$endw,Y:$ypos, '$overflow' .. result for tb-apply()");
logging("VERBOSE", "subject: '$subject' länge: '" . length($subject) . "'");
}
# ----------------------------------------------------------------
# ContentText
# ----------------------------------------------------------------
my $text_length = length($text_as_line);
if($text_length > 0) {
logging("VERBOSE", "Text: '$text_as_line' length: '" . $text_length . "'");
my $text = $page->text;
my $tb= PDF::TextBlock->new({
pdf => $pdf,
page => $page,
text => $text_as_line,
align => 'justify',
x => $size_x * 0.15,
y => $size_y - ( $INFOBOX_HEIGHT + 50),
w => $size_x * 0.7,
h => $text_length > 150 ? $INFOBOX_HEIGHT * 2 : $INFOBOX_HEIGHT,
lead => 30 * 1.2,
fonts => {
default => PDF::TextBlock::Font->new({
pdf => $pdf,
font => $courier,
size => 30,
fillcolor => '#000000',
}),
},
});
my($endw, $ypos, $overflow)= $tb->apply();
logging("VERBOSE", "text - x:$endw ,y:$ypos, $overflow .. result for tb-apply()");
my $y_value = calculate_y_value(length($text_as_line), "line");
# add another line depending on text_length
my $line = $page->gfx;
$line->strokecolor('black');
$line->linewidth(5);
$line->move( 0, $y_value );
$line->line( $size_x, $y_value );
$line->stroke;
logging("VERBOSE", "Adding black line add y '$y_value'");
}
# --------------------------------------------------------
# TODO: check orientation of image
# -> AUTO ROTATION
# --------------------------------------------------------
my $image = Image::Magick->new(magick=>'JPEG');
$image->set(verbose=>'true') if($verbose);
$image->set(debug=>'true') if($debug);
$image->set(compression=>'none');
# --------------------------------------------------------
# Generate perfectly-balanced-photo-gallery
# --------------------------------------------------------
my $arrSize = @images;
# this will be the montage file
my $file = "/tmp/" . $tmp_dir_hash . "/" . md5_hex(rand($ss).$from.$date.$name.rand(50)) . ".jpg";
my $x;
my $tile;
# --------------------------------------------------------
# Image Position
# --------------------------------------------------------
my $xpos = 0;
my $ypos = 0;
my $w = 0;
my $h = 0;
my $d = DENSITY;
# --------------------------------------------------------
# Resize to fit under the info/mediabox
# thats why we sub 50 from size_y
# y start point depends on text length
# --------------------------------------------------------
my $y_value = calculate_y_value(length($text_as_line), "pic");
my $geometry = sprintf("%ix%i", $size_x - $x_buffer, $y_value ) ;
# Single Image Email
if($arrSize == 1) {
logging("VERBOSE", "Found 1 Picture .. do some magic .. ");
# Get Image
$image->Read($images[0]);
$image->AutoOrient();
$w = $image->Get("width");
$h = $image->Get("height");
# Check, if pic size fits content space
# h: if text is available, Text-Box will be added
if( $w > $size_x || $h > $y_value ) {
logging("VERBOSE", "resize PIC cause width w '$w' is greater then size_x '$size_x' ($geometry)" );
$image->Resize( geometry => $geometry, compress => 'none' );
}
elsif($w < 2000 || $h < 2000) {
logging("VERBOSE", "resize PIC cause width is small ($w) then $size_x ($geometry)" );
$image->Resize( geometry => $geometry, compress => 'none' );
}
# Resized values
$w = $image->Get("width");
$h = $image->Get("height");
$image->Set(density => DENSITY);
$x = $image->Write('jpg:'.$file);
}
# Multi Image Email
elsif ($arrSize > 1) {
# is calculate in y_value / see above
my $geo_size_y = $y_value;
logging("VERBOSE", "size for pic: '$geo_size_y' text_as_line: '$text_as_line'");
if($arrSize == 2) {
$geometry = sprintf("%sx%s", $size_x , ($geo_size_y) / 2 );
$tile = "1x2";
}
elsif($arrSize == 3) {
$geometry = sprintf("%sx%s", $size_x / 2 , ($geo_size_y) / 2);
$tile = "2x2";
}
elsif($arrSize == 4) {
$geometry = sprintf("%ix%i", $size_x / 2 , ($geo_size_y) / 2);
$tile = "2x2";
}
elsif($arrSize == 5) {
$geometry = sprintf("%sx%s", $size_x / 3 , ($geo_size_y) / 2 );
$tile = "3x2";
}
elsif($arrSize == 6 ) {
$geometry = sprintf("%sx%s", $size_x / 3 , ($geo_size_y) / 2 );
$tile = "3x";
}
elsif($arrSize == 7 || $arrSize == 8) {
$geometry = sprintf("%sx%s", $size_x / 2 , ($geo_size_y) / 4 );
$tile = "2x";
}
elsif($arrSize == 9 || $arrSize == 10) {
$geometry = sprintf("%sx%s", $size_x / 2 , ($geo_size_y) / 5 );