Sublime Forum

Printing from sublime

#21

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:

http://www.reportlab.com/software/opensource/rl-toolkit/download/

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:

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.drawText(tx)
                    canv.showPage()
                    canv.setFont("Courier", 8)
                    tx = canv.beginText(left_margin, top_margin - 0.5*inch)

            if tx:
                canv.drawText(tx)
                canv.showPage()

            canv.save()
            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

Keybinding

    { "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…

0 Likes

#22

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…

0 Likes

#23

and another happy sublime user with urgent demand for a simple print functionality.
Or is there a puglin that does printing?

0 Likes

#24

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?

0 Likes

#25

+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.

0 Likes

#26

Printing is essential. Is there a textmate bundle that could fill the gap for now?

0 Likes

#27

old.nabble.com/Q:-preserve-synta … 38354.html

0 Likes

#28

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.

0 Likes

#29

This is another chime for printing. How many does it take for this feature to bubble up high enough that it gets implemented?

0 Likes

#30

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.

0 Likes

#31

Printing would definitely be useful for me. For the moment, I just open the file in TextWrangler to print, but that’s a step I could live without.

0 Likes

#32

this is a necessary evil IMO - and it is a pain to have to leave sublime just to print!

0 Likes

#33

Just another vote here. We use Sublime exclusively now at work (bought licenses for everyone) and I also use it at home. I do everything in it, all day, including keeping notes, bookkeeping, taxes, etc. I need to print out tax stuff, API documentation, JSON/XML examples etc., pretty often.

Since “done” is better than “perfect”, I would settle for a non-syntax highlighted, minimal print.

0 Likes

#34

Take a look at HTML Export plugin available via Package Control. Exports your current file to your web browser with syntax highlighting. From there you can print…

Includes the line numbers if you have them showing, so keep that in mind.

0 Likes

#35

I agree. If there is at least a workaround for now, I would rather the developer’s effort be focused on bugs and getting ST2 out of beta.

Just my 2 cents.

0 Likes

#36

[quote=“jimbob”]Take a look at HTML Export plugin available via Package Control. Exports your current file to your web browser with syntax highlighting. From there you can print…

Includes the line numbers if you have them showing, so keep that in mind.[/quote]

Very nice.

0 Likes

#37

Discovered Sublime today. Been using it for about an hour and was liking it…until I tried to print. No printing function is a huge deal breaker for me.

If I could print with syntax highlighting, it’d be a definite buy for me (especially since Visual Studio dropped color printing with VS2010).

0 Likes

#38

Here’s a really simple solution for printing from Sublime Text. It is a plugin that sends the file of the current view to an external command that handles the printing. This works well enough for me. You can get it here if you want to try it: github.com/svenax/SublimePrint

Note that it currently doesn’t support Windows, though it will probably not be too hard to fix that.

0 Likes

#39

Here’s another vote for printing.

0 Likes

#40

Just another person stepping in to say I’d really like the print feature!

0 Likes