dabo Commit
Revision 7114
Date: 2012-03-13 13:57:48 -0700 (Tue, 13 Mar 2012)
Author: Paul
Trac: http://trac.dabodev.com/changeset/7114

Changed:
U   trunk/dabo/lib/reportUtils.py

Log:
Modified dabo.lib.reportUtils.printPDF() to allow specifying a printer name to 
print to instead of the default printer. This will only work if gsprint is on
the path.

Tested on Windows.  



Diff:
Modified: trunk/dabo/lib/reportUtils.py
===================================================================
--- trunk/dabo/lib/reportUtils.py       2012-03-12 23:09:01 UTC (rev 7113)
+++ trunk/dabo/lib/reportUtils.py       2012-03-13 20:57:48 UTC (rev 7114)
@@ -5,8 +5,17 @@
 import tempfile
 import datetime
 from decimal import Decimal
+import cStringIO
 
+# If gsprint is available, use it for printing:
+gsprint = True
+try:
+       p = subprocess.Popen(("gsprint",), stdout=subprocess.PIPE, 
stderr=subprocess.PIPE)
+       stdout, stderr = p.communicate()
+except OSError:
+       gsprint = False
 
+
 class TempFileHolder(object):
        """Utility class to get temporary file names and to make sure they are
        deleted when the Python session ends.
@@ -67,14 +76,28 @@
                                        subprocess.Popen((viewer, path))
 
 
-def printPDF(path):
-       """Print the passed PDF file to the default printer."""
-       try:
-               os.startfile(path, "print")
-       except AttributeError:
-               # startfile() only available on Windows
-               subprocess.Popen(("lpr", path))
+def printPDF(path, printerName=None):
+       """Print the passed PDF file to the default printer.
 
+       If gsprint is installed and on the path, the printerName parameter
+       can be used to specify which printer to output to. NOTE: gsprint
+       is part of gsview, and gsview depends on ghostscript. These packages
+       are GPL - to avoid legal issues make sure your end user installs them
+       separately from your application.
+       """
+       if gsprint:
+               args = ["gsprint", path]
+               if printerName:
+                       args.insert(-1, '-printer')
+                       args.insert(-1, "%s" % printerName)
+               p = subprocess.Popen(args, stderr=subprocess.PIPE, 
stdout=subprocess.PIPE)
+               p.communicate()
+       else:
+               try:
+                       os.startfile(path, "print")
+               except AttributeError:
+                       # startfile() only available on Windows
+                       subprocess.Popen(("lpr", path))
 
 
 def getTestCursorXmlFromDataSet(dataset):



_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message: 
http://leafe.com/archives/byMID/[email protected]

Reply via email to