[Samba] pdf-printer

L.P.H. van Belle belle at bazuin.nl
Wed Jul 1 02:51:06 MDT 2015


Hai Mourik Jan, 
 
This is the script we use, this is the work my collega. ( see the developed by, thats his company ) 
This is a "as is"  share, so read the code, we added as much as possible info in there. 
 
Few extra pointers, 
- speed up the script by useing ramdisk, make sure your ramdisk has the space for the bigest pdf you want to create. 
    we did limit the max size in the mail server. 
- If you have really big pdf files, add a section to this script to save the file on a file server and, send the mail with a link in it for example. 
 
Big thanks to drmaert for letting me share it to on the samba list ! 
 
The version with company papier is NOT included in this version
Be with the with the code below you can create it. 
you need pdftk with the background option, read the manual for pdftk 
 
 
Greetz, 
 
Louis
 
 
#!/usr/bin/perl
# pdfmaker-mailscript, to be used bij postfix (developed by drmaert at drmaert.com)
# returns a pdf of your sent attachement
# can be connected via /etc/aliases
# example entry in /etc/aliases:
# makepdf: |/etc/postfix/bin/makepdf.pl
# dependecies: munpack (to extract the email)
#              mpack (to package returning mail)
#              pdftk (to create pdf) 
# also needed /home/msg/message.msg (messagetext for retrning email)
# further dependencies will be mentioned later on
 
# first store mail message in tmpdir (/tmp)
# prepare to store the mail
$RANDOM = int(rand(65535));
open(MAIL,">/tmp/$RANDOM.eml");
while( $_=<STDIN> ) {
        $regel = $_;
        if( $regel =~ /^From (.*)$/ ) { $from = $1; }
        if( $regel =~ /^Subject: (.*)$/ ) { $subject = $1; }
        #if( $regel =~ /^From: (.*)$/ ) { $from = $1; }
        print MAIL $regel;
}
close(MAIL);
 
# security, only my own domain can use this script :)
if( $from =~ /\@mydomain.tld/ ) {
 
#now extract all attachments
system ("mkdir /tmp/$RANDOM");
system ("/usr/bin/munpack -f -C /tmp/$RANDOM /tmp/$RANDOM.eml 2>> /tmp/unpack.log");
$from = substr $from, 0, index($from, ' ') ;
 
## Enable the sections you want to use below only create a pdf is enable. 
# *********************** start inputfile = pdf ***********************
@files = </tmp/$RANDOM/*.pdf>;
foreach $file (@files) {
   $targetfile = substr $file, rindex($file, '/') + 1;
   system ("/usr/bin/pdftk $file output /tmp/$targetfile 2>/dev/null");
   system("/usr/bin/mpack -s \"Re: $subject\" -d /home/msg/message.msg \"/tmp/$targetfile\" \"$from\"");
   system("rm -f \"/tmp/$targetfile\"");
}
# *********************** end of part inputfile = pdf *********************** 
## Enable the sections you want to use below here.  
# *********************** start inputfile = oxps *********************** 
# extra dependecy:   xpstopdf (to convert to pdf)   (package : libgxps-utils ) 
#@files = </tmp/$RANDOM/*.oxps>;                                              
#foreach $file (@files) {
#  $targetfile = substr $file, rindex($file, '/') + 1;
#  $targetfile = substr $targetfile, 0 , index($targetfile, '.');
#  $targetfile = "$targetfile.pdf";
#  system("xpstopdf $file /tmp/$targetfile");
#  system("/usr/bin/mpack -s \"Re: $subject\" -d /home/msg/message.msg \"/tmp/$targetfile\" \"$from\"");                                                       
#   system("rm -f \"/tmp/$targetfile\"");                     
#}
# *********************** end inputfile = oxps *********************** 
 
# *********************** start inputfile = tiff / tif *********************** 
# extra dependecy:   tiff2pdf (to convert to pdf)  (package: libtiff-tools )                   
#@files = </tmp/$RANDOM/*.tiff>;
#foreach $file (@files) {
# $targetfile = substr $file, rindex($file, '/') + 1;
# $targetfile = substr $targetfile, 0 , index($targetfile, '.'); 
# $targetfile = "$targetfile.pdf"; 
# system("/usr/bin/tiff2pdf -o /tmp/$RANDOM.pdf $file");
# system ("/usr/bin/pdftk /tmp/$RANDOM.pdf output /tmp/$targetfile 2>/dev/null");
# system("/usr/bin/mpack -s \"Re: $subject\" -d /home/msg/message.msg \"/tmp/$targetfile\" \"$from\""); 
# system("rm -f \"/tmp/$targetfile\""); 
# system("rm -f /tmp/$RANDOM.pdf"); 
#} 
#@files = </tmp/$RANDOM/*.tif>;
#foreach $file (@files) {
# $targetfile = substr $file, rindex($file, '/') + 1;
# $targetfile = substr $targetfile, 0 , index($targetfile, '.'); 
# $targetfile = "$targetfile.pdf"; 
# system("/usr/bin/tiff2pdf -o /tmp/$RANDOM.pdf $file");
# system ("/usr/bin/pdftk /tmp/$RANDOM.pdf output /tmp/$targetfile 2>/dev/null");
# system("/usr/bin/mpack -s \"Re: $subject\" -d /home/msg/message.msg \"/tmp/$targetfile\" \"$from\""); 
# system("rm -f \"/tmp/$targetfile\""); 
# system("rm -f /tmp/$RANDOM.pdf"); 
#}
# *********************** end of inputfile = tiff / tif *********************** 
 
# *********************** start of inputfile = doc *********************** 
# extra dependency: abiword (to convert to pdf) (package: abiword) 
#@files = </tmp/$RANDOM/*.doc>;                                               
#foreach $file (@files) {
#   $targetfile = substr $file, rindex($file, '/') + 1;
#   $targetfile = substr $targetfile, 0 , index($targetfile, '.');
#   $targetfile = "$targetfile.pdf";
#   system("abiword --to=pdf --to-name=/tmp/$RANDOM.pdf $file");
#   system ("/usr/bin/pdftk /tmp/$RANDOM.pdf output /tmp/$targetfile 2>/dev/null"); 
#   system("/usr/bin/mpack -s \"Re: $subject\" -d /home/msg/message.msg \"/tmp/$targetfile\" \"$from\"");                                                       
#   system("rm -f \"/tmp/$targetfile\"");              
#   system("rm -f /tmp/$RANDOM.pdf");                          
#}
# *********************** end of inputfile = doc *********************** 
 
#*********************** start of input file = rtf *********************** 
# extra depenency: Ted / rtf2pf (to convert to pdf) (not in debian get the deb here: http://www.nllgg.nl/Ted/ ) 
#@files = </tmp/$RANDOM/*.rtf>;                                               
#foreach $file (@files) {
#   $targetfile = substr $file, rindex($file, '/') + 1;
#   $targetfile = substr $targetfile, 0 , index($targetfile, '.');
#   $targetfile = "$targetfile.pdf";        
#   system("/usr/local/Ted/rtf2pdf.sh $file /tmp/$RANDOM.pdf");
#   system ("/usr/bin/pdftk /tmp/$RANDOM.pdf output /tmp/$targetfile 2>/dev/null");                                                    
#   system("/usr/bin/mpack -s \"Re: $subject\" -d /home/msg/message.msg \"/tmp/$targetfile\" \"$from\"");                                                       
#   system("rm -f \"/tmp/$targetfile\"");
#   system("rm -f /tmp/$RANDOM.pdf");                                           
#}
# *********************** end of inputfile = rtf *********************** 
 
# *********************** start of file htm / html *********************** 
# extra dependencies: xvfb-run (to start a virtual x session) (package : xvfb ) 
#                     wkhtmltopdf (a webbrowser to be started virtual and make a
#                                  screenshot for pdf)  
#@files = </tmp/$RANDOM/*.htm>;                                               
#foreach $file (@files) {
#   $targetfile = substr $file, rindex($file, '/') + 1;
#   $targetfile = substr $targetfile, 0 , index($targetfile, '.');                              
#   $targetfile = "$targetfile.pdf";      
#   system("/usr/bin/xvfb-run --server-args=\"-screen 0, 1280x1024x24\" wkhtmltopdf -T 30 -q $file /tmp/$RANDOM.pdf");                                
#   system ("/usr/bin/pdftk /tmp/$RANDOM.pdf output /tmp/$targetfile 2>/dev/null");
#   system("/usr/bin/mpack -s \"Re: $subject\" -d /home/msg/message.msg \"/tmp/$targetfile\" \"$from\"");                                                       
#   system("rm -f \"/tmp/$targetfile\"");           
#   system("rm -f /tmp/$RANDOM.pdf");                                
#}
#@files = </tmp/$RANDOM/*.html>; 
#foreach $file (@files) {
#   $targetfile = substr $file, rindex($file, '/') + 1;
#   $targetfile = substr $targetfile, 0 , index($targetfile, '.');                                
#   $targetfile = "$targetfile.pdf";
#   system("/usr/bin/xvfb-run --server-args=\"-screen 0, 1280x1024x24\" wkhtmltopdf -T 30 -q $file /tmp/$RANDOM.pdf");                               
#   system ("/usr/bin/pdftk $file output /tmp/$targetfile 2>/dev/null");
#   system("/usr/bin/mpack -s \"Re: $subject\" -d /home/msg/message.msg \"/tmp/$targetfile\" \"$from\"");                                                       
#   system("rm -f \"/tmp/$targetfile\"");
#   system("rm -f /tmp/$RANDOM.pdf");  
#}
# *********************** end of inputfile = htm / html *********************** 
 
# *********************** start of inputfile = docx *********************** 
# extra dependency: abiword
#@files = </tmp/$RANDOM/*.docx>;
#foreach $file (@files) { 
#   $targetfile = substr $file, rindex($file, '/') + 1;
#   $targetfile = substr $targetfile, 0 , index($targetfile, '.');
#   $targetfile = "$targetfile.pdf";
#   system("abiword --to=pdf --to-name=/tmp/$RANDOM.pdf $file"); 
#   system ("/usr/bin/pdftk /tmp/$RANDOM.pdf output /tmp/$targetfile 2>/dev/null"); 
#   system("/usr/bin/mpack -s \"Re: $subject\" -d /home/msg/message.msg \"/tmp/$targetfile\" \"$from\"");
#   system("rm -f \"/tmp/$targetfile\"");
#   system("rm -f /tmp/$RANDOM.pdf");
#}
# *********************** end of inputfile = docx *********************** 
 
#now remove all  tmp-data
system ("rm -f /tmp/$RANDOM/*");
system ("rmdir /tmp/$RANDOM");
}
system ("rm /tmp/$RANDOM.eml");
 

>Hi Louis,
>
>Interesting idea. :-) Could you give some examples..?
>
>MJ
 
 
 
 

>Hi Louis,
>
>Interesting idea. :-) Could you give some examples..?
>
>MJ

 



More information about the samba mailing list