Sublime Forum

Printing in Sublime4

#1

I searched the forum and wasn’t able to find a recent answer on this. And please - no hate mail for asking about printing. I just upgraded (and paid) for the latest version and I’m thrilled there is an option to print (hardcopy). When I click print nothing happens. Thoughts?

0 Likes

#2

Do you see any errors in the console? (View > Show Console)

0 Likes

#3

Hi Benjamin - There aren’t any messages in the console.

0 Likes

#4

When I deliberately make it fail I get a log message on my end. Are you sure nothing gets logged when you select print? Does it happen in safe mode?

0 Likes

#5

As far as I know I’m not running in Safe mode. I uninstalled Sublime 3 just in case there was a conflict (and I rebooted in between). Here is the only thing showing up in the console:

DPI mode: per-monitor v2
startup, version: 4152 windows x64 channel: stable
executable: /C/Program Files/Sublime Text/sublime_text.exe
application: /C/Program Files/Sublime Text
working dir: /C/Program Files/Sublime Text
packages path: /C/Users/sjewe/AppData/Roaming/Sublime Text 3/Packages
state path: /C/Users/sjewe/AppData/Roaming/Sublime Text 3/Local
zip path: /C/Program Files/Sublime Text/Packages
zip path: /C/Users/sjewe/AppData/Roaming/Sublime Text 3/Installed Packages
ignored_packages: [“Vintage”]
pre session restore time: 1.66392
startup time: 1.88992
first paint time: 1.90292
reloading plugin Default.arithmetic
reloading plugin Default.auto_indent_tag
reloading plugin Default.block
reloading plugin Default.colors
reloading plugin Default.comment
reloading plugin Default.convert_color_scheme
reloading plugin Default.convert_syntax
reloading plugin Default.copy_path
reloading plugin Default.echo
reloading plugin Default.exec
reloading plugin Default.fold
reloading plugin Default.font
reloading plugin Default.goto_line
reloading plugin Default.history_list
reloading plugin Default.html_print
reloading plugin Default.indentation
reloading plugin Default.install_package_control
reloading python 3.3 plugin 0_package_control_loader.00-package_control
reloading plugin Default.keymap
reloading python 3.3 plugin Package Control.1_reloader
reloading plugin Default.kill_ring
reloading python 3.3 plugin Package Control.2_bootstrap
reloading plugin Default.mark
reloading plugin Default.new_templates
reloading plugin Default.open_context_url
reloading plugin Default.open_in_browser
reloading plugin Default.pane
reloading plugin Default.paragraph
reloading plugin Default.paste_from_history
reloading plugin Default.profile
reloading plugin Default.quick_panel
reloading plugin Default.rename
reloading plugin Default.run_syntax_tests
reloading plugin Default.save_on_focus_lost
reloading plugin Default.scroll
reloading plugin Default.set_unsaved_view_name
reloading plugin Default.settings
reloading plugin Default.show_scope_name
reloading plugin Default.side_bar
reloading plugin Default.sort
reloading plugin Default.switch_file
reloading plugin Default.symbol
reloading plugin Default.transform
reloading plugin Default.transpose
reloading plugin Default.ui
reloading plugin CSS.css_completions
reloading plugin Diff.diff
reloading plugin HTML.encode_html_entities
reloading plugin HTML.html_completions
reloading python 3.3 plugin Package Control.Package Control
plugins loaded
Package Control: Skipping automatic upgrade, last run at 2023-08-17 12:43:18, next run at 2023-08-17 13:43:18 or after

0 Likes

#6

Probably saved his document (program) in the Program Files folder.
It shouldn’t be there.

0 Likes

#7

The idea there was that you start Sublime in safe mode using the instructions at the link (they differ by platform) and see if the problem still persists. This troubleshooting step helps to determine if the problem is related to a package or setting getting in the way or something more intrinsic to Sublime in general.

0 Likes

#8

Did you save the document before you printed it?

0 Likes

#9

Catching up on the suggestions. The file I’m trying to print is 1) saved and 2) on my desktop (i.e. not in the Program Files folder). I started Sublime in Safe Mode (I’m on Windows 11) and I’m still not getting messages in the console and it’s still not printing.

0 Likes

#10

Apologies Brian, I missed the link you posted. I started Sublime in safe mode using shift+alt and I still don’t get any messages when I try printing.

0 Likes

#11

DPI mode: per-monitor v2
startup, version: 4152 windows x64 channel: stable
SAFE MODE - overriding packages, state and cache paths

FYI

0 Likes

#12

Sublime’s printing is powered by generating an HTML file and then invoking the browser to fix it; so a thing to test might be to verify that the default browser is set up correctly.

Does Help > Documentation correctly open the Sublime docs in your browser?

0 Likes

#13

Hi OdatNurd - I tried that and yes, I’m able to open the documentation and print it. Thanks for the suggestion

0 Likes

#14

Could you try running the following in the Sublime Text console (View > Show Console):

import webbrowser; webbrowser.open_new_tab("https://sublimetext.com")
0 Likes

#15

Okay, I tried that. It returned “true” and opened the sublimetext webpage.
I tried printing and it’s still not working. No messages to the console.

0 Likes

#16

Could you try running: sublime.log_commands(True); view.run_command('html_print')

0 Likes

#17

Okay. Here’s what I got

sublime.log_commands(True); view.run_command(‘html_print’)
command: html_print

I tried the print command again and it logged to the console (as “command: html_print”) but nothing printed

0 Likes

#18

Could you install the following plugin:

import sublime_plugin
import tempfile
import pathlib
import webbrowser
import sys
import os


class HtmlPrint2Command(sublime_plugin.TextCommand):
    def run(self, edit):
        view = self.view
        print(view)

        html = view.export_to_html(enclosing_tags=True, font_size=False)
        print(len(html), html[:20])

        extra_args = {}

        # On Linux flatpak and snap don't allow opening files from /tmp, so we
        # need to put them somewhere else.
        if sys.platform == "linux":
            downloads_path = os.path.expanduser("~/Downloads")

            if os.path.exists(downloads_path):
                extra_args['dir'] = downloads_path

        with tempfile.NamedTemporaryFile('w', suffix=".html", encoding='utf-8', delete=False, **extra_args) as f:
            print(f.name)
            f.write("<html><head><meta charset=\"UTF-8\"></head><body>")
            f.write(html)
            f.write('<script>window.print()</script></body></html>')

        url = pathlib.Path(f.name).as_uri()
        print(url)
        controller = webbrowser.get(using=view.settings().get('print_using_browser'))
        print(view.settings().get('print_using_browser'))
        controller.open_new_tab(url)

You can do that by going to Tools > Developer > New Plugin…, pasting the plugin and then saving as test.py. The name doesn’t matter as long as the extension is .py.

You can then run view.run_command('html_print2') and it should log some data that’ll narrow down the issue.

1 Like

#19

Thanks Ben. Here’s what we written to the console

view.run_command(‘html_print2’)
View(13)
9566 <div style="white-sp
C:\Users\sjewe\AppData\Local\Temp\tmp167rdo1t.html
file:///C:/Users/sjewe/AppData/Local/Temp/tmp167rdo1t.html
None

0 Likes

#20

I don’t see how that could possibly not be working. Could you run import webbrowser; webbrowser.open_new_tab('file:///C:/Users/sjewe/AppData/Local/Temp/tmp167rdo1t.html')

0 Likes