On Sat, Nov 8, 2008 at 9:54 AM, Thomas Mills Hinkle <[email protected]>wrote:

> I'm interested in switching to the new gtk printing system (I still use
> gnomeprint for GNOME applications, LPR for gnome-lib-less linux users, and
> launching Adobe Acrobat to print PDF for Windows users). I can already get
> very nice PDF output using ReportLab -- is there any way I can just hand
> gnomeprint a PDF and have it handle the rest?
>
>
In just under a year, I got back around to this problem and solved it for
myself. The answer is libpoppler for python, which makes it easy to render a
PDF to a cairo surface. That plus the testprint example (which appears a bit
dated -- it has a number of calls that don't work), and I have a working
solution in just about 25 lines.

In case anyone has my same question (just now googling for a solution my
initial question was the top hit I found), here's the code:

import gtk
import poppler
import os.path

class PDFPrinter:

    def __init__ (self, fn):
        self.d = poppler.document_new_from_file(fn,None)

    def draw_page (self,operation, context, page_num):
        page = self.d.get_page(page_num)
        page.render_for_printing(context.get_cairo_context())

def setup_printer (pp):
    po = gtk.PrintOperation()
    po.set_n_pages(pp.d.get_n_pages())
    po.connect('draw_page',pp.draw_page)
    po.set_export_filename('/tmp/foo.pdf')
    po.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG)

def print_pdf (pdf_filename):
    if not pdf_filename.startswith('file'):
        pdf_filename = 'file://' + os.path.realpath(pdf_filename)
    setup_printer(PDFPrinter(pdf_filename))

print_pdf(PATH_TO_FILE)
_______________________________________________
pygtk mailing list   [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://faq.pygtk.org/

Reply via email to