Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Batch Printing in Linux

by jmlynesjr (Pilgrim)
on May 15, 2013 at 19:26 UTC ( #1033731=CUFP: print w/ replies, xml ) Need Help??

I had a bunch of music lyrics that I wanted to print and I didn't want to manually drive gedit. I did, however, want to retain the document formatting. I found a hint in Re^2: using Brother QL-570 printer with Perl and also found sample commands in an Ubuntu Community Documentation post for using the Open Office command line interface for batch printing or viewing. The are also posts out there for doing a similar thing with MS-Word.

The following is what I threw together. It does the job for me, YMMV. I needed to print UTF-8 text files, but Open Office will try to print whatever file you give it using the file extension as a guide to the file format. I have also printed .odt files.

#! /home/xxxx/CitrusPerl/perl/bin/perl # BatchFilePrinter.pl # Batch print text files under linux # Prints ALL of the files in the specified directory # Uses the Open Office Command Line interface # Pass a directory path as a command line argument(@ARGV) or # hard code a default directory path. # Print to a local(default(-p filename) or named(-pt printername filen +ame)) printer # soffice -nologo -p file.txt # Print to default prin +ter # soffice -nologo -pt namedprinter file.txt # Print to named +printer # soffice -nologo -view file.txt # View a read-only v +ersion of the file # Note: The file extension clues Open Office as to the file format(I k +now duh!). # If no extension is present or if the file format doesn't match + the "usual" # usage/definition of said extension, Open Office will pop up an # "ASCII Filter Options" dialog box to ask for the file type inf +ormation. # This messes with unattended printing. So, make sure the extens +ions match # the document types. The print files queue faster than my Deskj +et can print. # Hint and example commands from a post by Jorge Castro on Ubuntu Comm +unity Documentation. # Last Modified: May 15, 2013 by James M. Lynes, Jr. use strict; use warnings; use File::Find; use Data::Dumper; my $directory = shift @ARGV || "/home/xxxx/Guitar-Music/"; my @files; find({ wanted => \&wanted, no_chdir => 1 }, $directory); @files = sort @files; print "\n\n"; foreach my $file(@files) { print "Printing file: $file\n"; `soffice -nologo -pt Deskjet-3900-2 $directory . $file`; } sub wanted { if($_ eq '.') {return}; if($_ eq '..') {return}; push (@files, $File::Find::name); }

James

There's never enough time to do it right, but always enough time to do it over...

Comment on Batch Printing in Linux
Download Code
Re: Batch Printing in Linux
by karlgoethebier (Curate) on May 17, 2013 at 13:04 UTC
    I needed to print UTF-8 text files...

    Why not lpr *.txt?

    I have also printed .odt files...

    Why not lowriter --headless --convert-to pdf *.odt?

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Karl:

      Thank you for the additional options. I figured there would be other solutions which is why I threw in the YMMV. :)

      Why? 1. Lame, but, Perl practice. 2. Somewhere in the mass of posts that I read it was recommended to use Open Office to retain document formatting, so I went and figured out how to make that work and wanted to document a solution rather than repeat the general hints found in the posts. 3. Still pretty new to linux. 4. Retired and doing this for fun. No time constraints.

      James

      There's never enough time to do it right, but always enough time to do it over...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1033731]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (15)
As of 2014-10-24 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (132 votes), past polls