Sublime Forum

Printing from Sublime Text 2?

#21

[quote=“qgates”]A simple solution is to use the Export HTML plugin together with your browser. Apart from simple, it’s also pretty darned flexible, because once your printable code is in HTML/CSS you can do lots with it in before printing (ie. on the fly styling of fonts, colours, spacing, pagination, etc.).
For the above reason I really don’t understand why people are so hung up on printing. Maybe I’m missing something.[/quote]

I completely agree.
Futhermore, adding that I seldom print my code (or anything) …this is really a non-issue to me.

0 Likes

#22

[quote=“TonyW”]

I must be the 1% :smiley:

Because when I do “Export HTML | Show Export menu | Browser print: Color”, my html editor starts up (NVu)

Without having looked at the source code… I suppose the plugin requires “htm(l)” file types to be associated with a browser, which is a no-go for me. Is there a way to say which process to start?

Note: I used github.com/facelessuser/ExportHtml - maybe there is an alternative?[/quote]

Looking at the ExportHtml source code that you linked to, the last few lines of “ExportHtml.py” have the following code:

        if inputs"view_open"]:
            self.view.window().open_file(the_html.name)
        else:
            # Open in web browser; check return code, if failed try webbrowser
            status = desktop.open(the_html.name, status=True)
            if not status:
                webbrowser.open(the_html.name, new=2)

So it looks like the plugin is trying to open the html file with the default application (desktop.open), but the fallback code is what you want (webbrowser.open). A simple edit of the code should give you the preferred behaviour. If editing the code is out of the question for you, then maybe you could create a new issue with the developer asking him for a setting to control this behaviour (github.com/facelessuser/ExportHtml/issues)?

0 Likes

#23

[quote=“jbjornson”]

I must be the 1% :smiley:

Because when I do “Export HTML | Show Export menu | Browser print: Color”, my html editor starts up (NVu)

Without having looked at the source code… I suppose the plugin requires “htm(l)” file types to be associated with a browser, which is a no-go for me. Is there a way to say which process to start?

Note: I used github.com/facelessuser/ExportHtml - maybe there is an alternative?

Looking at the ExportHtml source code that you linked to, the last few lines of “ExportHtml.py” have the following code:

        if inputs"view_open"]:
            self.view.window().open_file(the_html.name)
        else:
            # Open in web browser; check return code, if failed try webbrowser
            status = desktop.open(the_html.name, status=True)
            if not status:
                webbrowser.open(the_html.name, new=2)

So it looks like the plugin is trying to open the html file with the default application (desktop.open), but the fallback code is what you want (webbrowser.open). A simple edit of the code should give you the preferred behaviour. If editing the code is out of the question for you, then maybe you could create a new issue with the developer asking him for a setting to control this behaviour (github.com/facelessuser/ExportHtml/issues)?[/quote]

Thanks for the suggestions. After some looking around, I figured out how to edit the source and made sure only webbrowser.open is called. Unfortunately, that too opens NVu (or whatever the default application is)

0 Likes

#24

How about trying this instead?

webbrowser.open('file://' +the_html.name, new=2)
0 Likes

#25

…or maybe this:

webbrowser.get("firefox").open(the_html.name, new=2)

from http://www.daniweb.com/software-development/python/threads/285581/how-do-i-call-the-default-browser-from-a-python-program.

Check here for more browser types other than firefox: http://docs.python.org/2/library/webbrowser.html

0 Likes

#26

[quote=“jbjornson”]…or maybe this:

webbrowser.get("firefox").open(the_html.name, new=2)

from http://www.daniweb.com/software-development/python/threads/285581/how-do-i-call-the-default-browser-from-a-python-program.

Check here for more browser types other than firefox: http://docs.python.org/2/library/webbrowser.html[/quote]

Chrome (my only browser) is not on that list. Chrome support was added in Python 3.3.x apparently - but still no go.

I think I’ll give up for now, and I guess this proves the point about printing as a native feature…

0 Likes

#27

One last try…the correct documentation link is: http://docs.python.org/3/library/webbrowser.html. Try one of the following: ‘google-chrome’, ‘chrome’, ‘chromium’, ‘chromium-browser’

0 Likes

#28

None of those works - the associated application still starts. Since Python has four (!) different chrome names, I guess this is shaky business, and my bleeding edge chrome beta isn’t found under those names?

0 Likes

#29

I suppose you are on a Windows box.
webbrowser module is next to useless for anything except the default system browser.
It search the browser name in the system path, which is not a good idea on Windows.

You can check which browser was found in the console:

import webbrowser webbrowser._browsers

Something like that must work, but not very useful:

webbrowser.BackgroundBrowser('C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe').open('http://www.myurl.com')

Open an issue for the Export HTML plugin, facelessuser is a nice and brilliant guy.

0 Likes

#30

I’m one of “those guys” that considers integrated printing essential to their work. I really don’t understand the virulence and contempt with which the people with the opposite views seem to regard this feature request.

I’ve been programming for 20 something years, and an essential part of the way I work is to print out and annotate my own code, and to share it with others for comment and revision. I understand that not everyone works that way, but because it is so important to the way I work, my primary editor will have that feature.

I have tried the other printing options, and they are just not “there.” Print to HTML trashes programming indentation, particularly when there is a mix of spaces and tabs in the file. So I come to this forum simply to state that integrated printing is an essential feature for me, and to express my wish that it be added to Sublime 3. The developer of Sublime can do with this what he will, but I’m not going to buy the editor unless it has this feature.

If you don’t care about this feature, fine. But it’s important to me, because it’s part of the way that I work and code.

I really, really like Sublime: it is elegant, lightweight, and has personality. But the lack of integrated printing keeps me using editors that are otherwise clunkier (Notepad++, MultiEdit, SlickEdit).

0 Likes

#31

I am programming for moreless 28 years, rarely I print code, but its a important feature, even Notepad has print option. Sublime Text is the best code editing tool, I bought it to support developers, they should implement this feature to support users :smile:

0 Likes

#32

I wrote this plugin for my own. I hope it helps you all. Regards!

[code]

Coded by: Johan Maximus

This plugin for SublimeText3 prints the content of the current window over Windows platforms

Installation: place this “print.py” file into you user packages folder. Eg.: sublimeFolder/Data/Packages/User/print.py

After that, bind the command “print” to a hot key you feel comfortable with. Go to “Preferences”, “Key Bindings - User” and write the next few lines:

{ “keys”: “ctrl+alt+p”], “command”: “print” }

]

import sublime, sublime_plugin

class PrintCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Reference wherever you want in your computer
path = “C:\Users\Maxi\Documents\printFile.txt”
# Select all in the current window
content = self.view.substr(sublime.Region(0, self.view.size()))
# It opens the file “printFile” for writing
text_file = open(path, “w”)
# Writes our content
text_file.write(content)
# Close the file
text_file.close()
# The sad part. It opens the file we created, containing what we want to print, with your default OS text editor (notepad in my case) and sends the print command to it.
os.startfile(path, “print”)
# Notepad closes after printing. That’s all…[/code]

0 Likes