need to print a PDF file under ubuntu

We have an application that generates PDF files from time to time,.  This will operate under Ubuntu.

I need to automate the printing of these.

What I mean is that I need to be able to have a simple script that will cause the PDF file to be printed on the default printer.

I need to add that while I believe that I know what I am doing as a programmer I have NO experience in sending anything to the printer other than from applications such as openOffice.

The one other thing that is related that I need to do is determine how many pages the print will actually take.

I sort of suspect that ghostscript is to be used here with the bbox device but even this is not obvious to me as to what it is telling me
LVL 8
jhurstAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cjl7freelance for hireCommented:
Hmm,

Printing a document in linux (Unix) is dead simple...

lp <full path to document>

So if you have a directory containing files that needs to be printed you could do something like so:



If you need this to happen once a day you could place this file in /etc/cron.daily and make sure it has perms 755 and is owned by root.

chmod 755 <file>
chown root:root <file>

//jonas

#!/bin/bash                                                                     

# example print                                                                 
SRC_DIR="/var/tmp/"
DST_DIR="/var/tmp/printed"
mkdir -p $DST_DIR

cd $SRC_DIR
for i in *.pdf
do /usr/bin/lp $i
if [ $? -eq 0 ]; then
mv $i $DST_DIR
fi
done

Open in new window

0
nociSoftware EngineerCommented:
And if you want the queue to be different from the default printing queue then you can specify the destination queue with -dprinter if the queue is named printer

lp -dprinter somefile.pdf

Do you need to known the number of pages AFTER printing (that is information provided by cups in the leftover jobfiles )
If you want to count them there are various pdf toolkits.
http://www.cityinthesky.co.uk/pdf2svg.html           - convert them to single page files
http://www.pdfhacks.com/pdftk/                            - generic toolkit that handles PDF

then there are various toolkits for Perl/Gem for handling files (reading/generating) they might be more approprate for your specific counting question.
0
jhurstAuthor Commented:
Are you telling me that the system will recognize that the file is a PDF and make the appropriate formatting decisions so that it will send the correct format to the printer?  If so, what other formats does it know.

And, what are you referring to when you say that there are Perl things for handling this.  Our applications are all perl so this would be fine
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

nociSoftware EngineerCommented:
You can check the /etc/cups/mime.conv & /etc/cups/mime.type files. The first defines conversions (and pdftops is a predefined conversion..)
and  the latter has rules to recognize file types.

There is a special vendor-postscript  type to account for postscript files with cups hints & options embedded.
You may need additional software (poppler) to do the actual conversion.

PDF-API2  can be used to modify & create PDF files.
PDF-Extract or PDF-Xtract to pull pages from a file. You can try to determine the last page by (attempting to) extracting it.
I haven't dug deep into the modules just a module search btw.
0
jhurstAuthor Commented:
+You can check the /etc/cups/mime.conv & /etc/cups/mime.type files. The first defines conversions
+(and pdftops is a predefined conversion..)
+and  the latter has rules to recognize file types.

What conversions?  As I said I already have the PDFs and I need to know how to get them to the printer

+There is a special vendor-postscript  type to account for postscript files with cups hints & options
+embedded.

Meaning?

+You may need additional software (poppler) to do the actual conversion.

What conversion?  Or is this what I asked about

+PDF-API2  can be used to modify & create PDF files.

I have them

+PDF-Extract or PDF-Xtract to pull pages from a file. You can try to determine the last page by
+(attempting to) extracting it.

But this seems a poor way surely

+I haven't dug deep into the modules just a module search btw.

I hope for more - I am not sure that there is ANYTHING that I can use here
0
nociSoftware EngineerCommented:
2nd question, you asked how cups can handle PDF. That is by converting it from PDF -> Postscript.
mime.conv holds the conversion specification that CUPS uses to transform data mostly from anything to internal PostScript  and then to final printer format.

Internal Cups Postscript is postscript with postscript comments to show where there are new pages, if landscape was requested through a commandline options, what tray needs to be used on a printer etc. etc.

Wat I meant for the modules: If a modules can read and process on a high level then there are lower level that UNDERSTAND how the format works.
I am sure that if a modules can select f.e. page 15 from a document it keeps a list internally where the pagenumbers reside. I hav'nt dug deep enough in a sense that I haven't dived into the modules to tell you which array / hash to use for this.

I probably won't have the time for that until Friday.
0
jhurstAuthor Commented:
+2nd question, you asked how cups can handle PDF. That is by converting it from PDF -> Postscript.
+mime.conv holds the conversion specification that CUPS uses to transform data mostly from anything
+to internal PostScript  and then to final printer format.

So, are you saying that when I print something it is converted internally into postscript anyway and then the printer driver converts from the postscript to what is needed by that printer?

+Internal Cups Postscript is postscript with postscript comments to show where there are new pages,
+if landscape was requested through a commandline options, what tray needs to be used on a printer
+etc. etc.

+Wat I meant for the modules: If a modules can read and process on a high level then there are lower
+level that UNDERSTAND how the format works.
+I am sure that if a modules can select f.e. page 15 from a document it keeps a list internally where the
+pagenumbers reside. I hav'nt dug deep enough in a sense that I haven't dived into the modules to tell
+you which array / hash to use for this.

I was hoping that there is some utility method that I can use without needing to dig

+I probably won't have the time for that until Friday.

TY
0
nociSoftware EngineerCommented:
Yes.  Cups core language is postscript. (see also all conversion that it handles in mime.conv).
Unless a print queue / print job is defined as "RAW" printing, then the file is passed on AS-IS.

And the printer driver (in most cases) is GhostScript to convert from Vendor.PostScript -> target device format.
0
nociSoftware EngineerCommented:
Ok digging wasn't really necessary.. (perldoc PDF::Extract tells the story).

something along:
---8<---
use PDF::Extract;

$pdf = new PDF::Extract( PDFDoc=>'my.pdf' );
$pageCount=$pdf->getVars("PDFPageCount");
$pageCount = 0 if (not defined ($pageCount) or $pageCount eq '');
print $pageCount,"\n";
---8<---

0
jhurstAuthor Commented:
so, are you saying that I can just use lp and since the file is in PDF that it will be converted to the format required by the printer?  Is it really that simple?

And on the PDF::Extract I will install that today and test
0
nociSoftware EngineerCommented:
When the normal install has been followed Yes.

You can check it out, cups has a filter driver directory (/usr/libexec/cups/filter maybe /usr/lib/cups/filter, it can be setup differently for distrbutions), if there is the pdftops program then that can be setup to either call ghostscript or call /usr/bin/pdftops (from poppler)

At least it does work by default on gentoo.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.