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 *

You May Also Like…

Toggen builds a new website

So I have built and hosted a new website for Roger Clark Pest Management they are a family owned and operated Pest...


Just downloaded WizTree (not a sponsor) from It seems to do what it says on the tin which is...

Scam call from “Telstra”

Just had a call from a man with a sub-continental accent from caller ID 0734946596 Told me there was problem with my...