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…