[reportlab-users] Printing Help
Jade Meskill
reportlab-users@reportlab.com
13 Jan 2003 11:11:02 -0700
--=-lB8AAt7TzvX0ukuMGdGY
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Fri, 2003-01-10 at 01:26, Dave Rogers wrote:
> Hi,
>
> I use ReportLab to generate PDF files as part of a python windows service. First of all, is there any kind of printing mechanism with ReportLab? As I'm not aware of any printing interface with ReportLab, I know this may not seem 100% related, but I'm in dire straits here, so I thought I'd ask the PDF experts.
>
> Environment:
>
> OS: Win2K
> Py: 2.1 with Windows extensions
> reportlab 1.15
> Acrobat Reader: 5.1
>
> I guess I could write some sort of DDE interface to Acrobat but that sounds like more Windows garbage than I want to delve in to.
>
I have been fighting this for quite some time now, and had to resort to
DDE to get any sort of reliable behavior from Acrobat. We also tried
Ghostscript, but had a lot of issues with it (it would randomly not want
to print some reportlabs generated PDFs). Attached is the script that I
use for Acrobat (it will launch acrobat minimized and will print the
file; acrobat will remain open, but minimized). Hopefully this will be
of some help, but I really wish there was a good, reliable way to batch
print PDFs in Windows, it sure would make my life a lot easier.
--
Jade Meskill
jade.meskill@libertydistribution.com
--=-lB8AAt7TzvX0ukuMGdGY
Content-Disposition: attachment; filename=acroprint.py
Content-Type: text/x-python; name=acroprint.py; charset=ANSI_X3.4-1968
Content-Transfer-Encoding: 7bit
#
# no copyright, please feel free to use this code however you wish
#
import os, sys
import time
if os.name == 'nt':
import win32ui
import dde
import _winreg
#------------------------------------------------------------------------------
def _printPDF(filename):
""" print a pdf """
if os.name == 'nt':
## create a DDE connection and open acrobat if possible
res = _checkAcrobatOpen(1)
if not res:
return
ddeServer = res[0]
ddeConv = res[1]
ddeConv.Exec("[FilePrintSilent(\"%s\")]" % (filename))
time.sleep(1)
# close dde connection
ddeServer.Destroy()
else:
os.spawnlp(os.P_NOWAIT, 'lpr', 'lpr', filename)
#------------------------------------------------------------------------------
def _launchAcrobat():
""" try to find the acrobat executable and launch it """
if os.name == 'nt':
readerKeys = {'SOFTWARE\\Adobe\\Adobe Acrobat\\5.0\\InstallPath' : 'Acrobat.exe',
'SOFTWARE\\Adobe\\Acrobat Reader\\5.0\\InstallPath' : 'AcroRd32.exe'}
reader = ''
path = ''
for key in readerKeys.keys():
try:
path = _winreg.QueryValue(_winreg.HKEY_LOCAL_MACHINE, key)
reader = readerKeys[key]
break
except:
continue
try:
os.spawnl(os.P_DETACH, path + '\\' + reader, 'acrobat', '/h')
time.sleep(5)
return 1
except:
raise "Acrobat not found"
return 0
#------------------------------------------------------------------------------
def _checkAcrobatOpen(launch = 0):
""" try to create a DDE conversation, if it fails, launch acrobat and try again """
try:
(s, c) = _connectAcrobat()
return (s, c)
except:
if launch:
if not _launchAcrobat():
return 0
for i in xrange(10):
try:
(s, c) = _connectAcrobat()
return (s, c)
except:
pass
#------------------------------------------------------------------------------
def _connectAcrobat():
""" Create a DDE server and conversation with acroview """
s = dde.CreateServer()
s.Create('')
c = dde.CreateConversation(s)
c.ConnectTo('acroview', 'control')
return (s, c)
#------------------------------------------------------------------------------
if __name__ == '__main__':
## this should do everything for you
_printPDF('test.pdf')
--=-lB8AAt7TzvX0ukuMGdGY--