On Sat, Nov 8, 2008 at 9:54 AM, Thomas Mills Hinkle <span dir="ltr"><<a href="mailto:tmhinkle@gmail.com">tmhinkle@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
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?<br>
<br></blockquote><div><br></div><div>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.</div>
<div><br></div><div>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:</div><div><br></div><div><div><font class="Apple-style-span" face="'courier new', monospace">import gtk</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">import poppler</font></div><div><font class="Apple-style-span" face="'courier new', monospace">import os.path</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br>
</font></div><div><font class="Apple-style-span" face="'courier new', monospace">class PDFPrinter:</font></div><div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace"> def __init__ (self, fn):</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> self.d = poppler.document_new_from_file(fn,None)</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> </font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> def draw_page (self,operation, context, page_num):</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> page = self.d.get_page(page_num)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> page.render_for_printing(context.get_cairo_context())</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> </font></div>
<div><font class="Apple-style-span" face="'courier new', monospace">def setup_printer (pp):</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> po = gtk.PrintOperation()</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> po.set_n_pages(pp.d.get_n_pages())</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> po.connect('draw_page',pp.draw_page)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> po.set_export_filename('/tmp/foo.pdf')</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> po.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG)</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">def print_pdf (pdf_filename):</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> if not pdf_filename.startswith('file'):</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"> pdf_filename = 'file://' + os.path.realpath(pdf_filename)</font></div><div><font class="Apple-style-span" face="'courier new', monospace"> setup_printer(PDFPrinter(pdf_filename))</font></div>
<div><font class="Apple-style-span" face="'courier new', monospace"><br></font></div><div><font class="Apple-style-span" face="'courier new', monospace">print_pdf(PATH_TO_FILE)</font></div></div></div>