Home Download Buy Blog Forum Support

Printing from sublime

Re: Printing from sublime

Postby ehamiter on Tue Aug 09, 2011 7:29 am

So this is more proof of concept than pretty functionality, but it's possible to write a plugin that sends the current view to a .pdf, then you can print it off from Reader or Foxit or Preview or whatever you use.


There's an open source PDF toolkit by Reportlab that you can download from here:


Install it with "python setup.py install" and additionally throw the /reportlab directory in your /User directory. Then put this plugin in the /User directory as well:

Code: Select all
import time, os
import sublime, sublime_plugin
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import A4

class PrintToPdfCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        if len(self.view.file_name()) > 0:
            page = self.view.substr(sublime.Region(0, self.view.size()))
            top_margin = A4[1] + .25*inch
            bottom_margin = inch
            left_margin = .25*inch
            started = time.time()
            pdfname = self.view.file_name() + ".pdf"
            canv = canvas.Canvas(pdfname, invariant=1)

            canv.setFont("Courier", 8)
            tx = canv.beginText(left_margin, top_margin - 0.5*inch)
            sublime.status_message("Processing PDF...")
            tx.textLines(page, trim=0)
            for line in page:
                y = tx.getY()
                if y < bottom_margin + 0.5*inch:
                    canv.setFont("Courier", 8)
                    tx = canv.beginText(left_margin, top_margin - 0.5*inch)

            if tx:

            print "%s successfully created." % (pdfname)
            sublime.status_message("PDF successfully processed.")

            finished = time.time()
            elapsed = finished - started
            pages = canv.getPageNumber()-1
            speed =  pages / elapsed
            fileSize = os.stat(pdfname)[6] / 1024
            print "%d pages in %0.2f seconds = %0.2f pages per second, file size %d kb" % (
                        pages, elapsed, speed, fileSize)

        def is_enabled(self):
            return self.view.file_name() and len(self.view.file_name()) > 0

Code: Select all
    { "keys": ["ctrl+alt+p"], "command": "print_to_pdf" }

This works for me, but it's not very pretty and multiple pages will break the formatting for now. If someone wants to improve it, go for it. Just a start for someone desperate for printing capabilities...
Posts: 67
Joined: Wed Apr 27, 2011 2:42 am

Re: Printing from sublime

Postby lluuuk on Wed Oct 12, 2011 1:31 pm

This would be perfect! Native printing is not so important, but if we could generate a PDF out of the view in sublime text 2, it would be great. I just don't get it with this plugin/installation so it would be nice if this could be added to the native features...
Posts: 3
Joined: Sun Oct 09, 2011 11:28 pm

Re: Printing from sublime

Postby eriq on Mon Oct 31, 2011 8:43 am

and another happy sublime user with urgent demand for a simple print functionality.
Or is there a puglin that does printing?
Posts: 2
Joined: Mon Oct 31, 2011 8:37 am

Re: Printing from sublime

Postby rwhite on Sat Dec 03, 2011 7:54 am

No printing in Sublime 2? Really?!

I happily stumbled onto Sublime 2, guided by friends who said that TextMate is no longer actively supported. And perhaps it's just because I do more coding in Sublime 2 than anything that I haven't noticed until now that it doesn't have the simple ability to Print.

With great respect for the hard-working people who have already made Sublime 2 a great product, and without appearing to be too snarky... it's pretty rare for me to have to resort to a "dead product" (Textmate in this case) for simple functionality that is present in every other commercial text editor I've ever used.

According to these forums, there have been requests for this simple functionality for at least two years now. Active development?
Posts: 3
Joined: Tue Oct 25, 2011 5:09 am

Re: Printing from sublime

Postby dancablam on Tue Jan 10, 2012 10:18 pm

+1 for printing. I use Sublime as a scratch pad often and sometimes need to print a few lines of my notes when I go to a meeting, or to configure a server, etc. Having to copy/paste into another editor just so I can print is ghetto. It doesn't help me "go green" - it just wastes my time. I think it's time this "duh" feature is just implemented so we can all stop complaining about it.
Posts: 1
Joined: Tue Jan 10, 2012 10:15 pm

Re: Printing from sublime

Postby philosopherdog on Wed Jan 11, 2012 8:23 pm

Printing is essential. Is there a textmate bundle that could fill the gap for now?
Posts: 34
Joined: Sat Nov 26, 2011 3:08 pm

Re: Printing from sublime

Postby philosopherdog on Wed Jan 11, 2012 8:25 pm

Posts: 34
Joined: Sat Nov 26, 2011 3:08 pm

Re: Printing from sublime

Postby BobSherer on Sat Jan 14, 2012 5:43 pm

I often need to print my works done on my paid Sublime Text 2 editor and cannot. I hate having to add other editors to my clean OS just to be able to print with some level of formatting. I could have paid less for other editor options and had to do make far less effort to produce printed output, which sometimes is still necessary.
Posts: 1
Joined: Sat Jan 14, 2012 5:32 pm

Re: Printing from sublime

Postby PatriceBG on Mon Jan 30, 2012 6:41 pm

This is another chime for printing. How many does it take for this feature to bubble up high enough that it gets implemented?
Posts: 1
Joined: Mon Jan 30, 2012 6:38 pm

Re: Printing from sublime

Postby grobot on Mon Jan 30, 2012 11:42 pm

I've bought Sublime 2. I love it. I would use printing. Mostly I print code for review, so what I most want is:

monospaced +
syntax highlighting +
sensible colours (not my dark theme!) +
line wrapping to fit the entire page in.

+1, etc. I promise to not make anyone who doesn't want printing use the functionality.
Posts: 4
Joined: Tue Jan 10, 2012 1:01 am


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 14 guests