On 07/18/2010 09:32 AM, John wrote:
> On Sunday 18 July 2010 09:20:11 am Jeff Johnson wrote:
>    
>> On 07/18/2010 08:32 AM, Paul McNett wrote:
>>      
>>> On Jul 18, 2010, at 8:19, Jeff Johnson<[email protected]>   wrote:
>>>        
>>>> Paul:  You mentioned the other day that you have a Print and Preview
>>>> button on your reports.  I have only seen things "previewed" because
>>>> they are sent to a pdf.  Would you be willing to share your technique
>>>> for printing directly to the printer?
>>>>          
>>> While I have preview and print buttons, they do the same thing. I
>>> simply haven't gotten around to printing directly to the printer yet.
>>> Sorry for the misinformation!
>>>
>>> Paul
>>>        
>> Thanks Paul.
>>      
> How to print Directly from Dabo
>
> Dabo uses ReportLab to generate PDF's  (please review the report designer
> screen cast on the dabo website) from a dataset.  But an immeditate question
> is rasied “How do you print the report directly to the printer”.  Below is
> one way to print PDF's using free products.
>
> On windows two app's are required:
> 1.Ghostscript (http://pages.cs.wisc.edu/~ghost/)
> 2.Python Win32 Extensions (http://sourceforge.net/projects/pywin32/files/)
>
>       Ghostscript accepts the PDF file and converts the file to PostScript.
> The Win32 Extensions provides an easy way to interface with the windows OS to
> select the default printer.
>
> if sys.platform == 'win32':
>         if selectedPrinter=='Default Printer':
>         import win32print
>         selectedPrinter=win32print.GetDefaultPrinter()
>
>         cmd='.\\ghostview\\gsview\\gsprint.exe -ghostscript ".
> \\ghostscript\\gs8.64\\bin\\gswin32c.exe" %s %s -printer "%s" "%s" 
> '%(orientation, '-query',
> selectedPrinter.strip(),outputfile.strip())
> #where outputfile is the PDF file produced by reportWriter
> # where selectedPrinter is what the user picked
> # then send the command to the OS.
>         sendtoprint = Popen(cmd)
>
> I leave it up to the reader design a dialog to retrieve a list of printers
> (hint try “win32print.EnumPrinters()”)
>
> On Linux most distro's already have GhostScript and all printing is done via
> PostScript so there is nothing to download or install.  Linux reconizes the
> file type of PDF and converts the file to PostScript (in the background linux
> uses GhostScript)    Therefore there is little that is required in the code.
>
> cmd= "lp -d %s '%s' " %  (selectedPrinter.strip(), outputfile.strip(),)
> sendtoprint = Popen(cmd, shell = True)
>
> On linux I use the “cups” module to assist in providing a means to retrieve
> available printers (works well with network printers).
>
> You may ask why I didn't use wxPython tools to retrieve printer data.  I found
> that on windows (don't know about Linux) I was not able to select network
> printers.
>
> Johnf
>
>
>
>
>    
Thank you John!

I found this for Windows:  The last line prints the pdf that Dabo generated.

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import inch

import cgi
import tempfile
import win32api

source_file_name = "c:/temp/temp.txt"
pdf_file_name = tempfile.mktemp (".pdf")

styles = getSampleStyleSheet ()
h1 = styles["h1"]
normal = styles["Normal"]

doc = SimpleDocTemplate (pdf_file_name)
#
# reportlab expects to see XML-compliant
#  data; need to escape ampersands&c.
#
text = cgi.escape (open (source_file_name).read ()).splitlines ()

#
# Take the first line of the document as a
#  header; the rest are treated as body text.
#
story = [Paragraph (text[0], h1)]
for line in text[1:]:
   story.append (Paragraph (line, normal))
   story.append (Spacer (1, 0.2 * inch))

doc.build (story)
win32api.ShellExecute (0, "print", pdf_file_name, None, ".", 0)


I am using Ubuntu for development but most of my customers are Windows 
so your information is very helpful.

-- 
Jeff

-------------------

Jeff Johnson
[email protected]



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

Reply via email to