This is a snippet of Perl that takes the contents of text file on stdin and then converts it to a PDF using a tool chain as follows
- Sed is removing the ^L form feed character from the end of the file to stop enscript creating an extra blank page
- Enscript is creating a PostScript file with the title of the contents of $pdf_jobname
- ps2pdf converts the PostScript file to PDF
- smbclient logs into a remote server share as the correct user and puts the file on the remote share
You could probably do it all using Perl modules but sometimes you don’t have time to find the appropriate module, translate the functionality into the specific Perl module authors syntax and make it work in pure Perl.
I was calling this from a printer script located in /etc/cups/interfaces. To get it work when it is being called via cups you need to make sure either apparmor (Ubuntu) or seLinux (Fedora) is not stopping cups from calling the different utilities.
To use the following snippet from the command line you might save it as test.pl and if you had a text file name mytext.txt you run it as follows
cat mytext.txt | ./test.pl
#!/usr/bin/perl # test.pl use strict; use warnings; my @input = <STDIN>; my $pdf_jobname = "My PDF File Test"; my $smb_cmd = "| sed 's/\$(echo -e '\\014')//g' | /usr/bin/enscript -J \"$pdf_jobname\" -B -o - -f 'Courier\@12/12' | ps2pdf - - | /usr/bin/smbclient '//servername/share_name\$' -U DOMAIN/username%'SecretPassword' -c \"put - \"$pdf_jobname.pdf\"; exit;\""; open(SMB, $smb_cmd) or die $!; print SMB @input; close(SMB);