Convert Text File to PDF and then place on remote Windows Share

Written by James McDonald

April 14, 2015

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

  1. Sed is removing the ^L form feed character from the end of the file to stop enscript creating an extra blank page
  2. Enscript is creating a PostScript file with the title of the contents of $pdf_jobname
  3. ps2pdf converts the PostScript file to PDF
  4. 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 and if you had a text file name mytext.txt you run it as follows

cat mytext.txt | ./


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;



Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…