Add :print --pdf flag to skip printing dialog

This commit is contained in:
Jeremy Kaplan 2016-07-10 21:42:43 -04:00
parent 3bb5b5d1c9
commit a6a030e92f
2 changed files with 14 additions and 3 deletions

View File

@ -487,12 +487,13 @@ If the pasted text contains newlines, each line gets opened in its own tab.
[[print]]
=== print
Syntax: +:print [*--preview*]+
Syntax: +:print [*--preview*] [*--pdf* 'file']+
Print the current/[count]th tab.
==== optional arguments
* +*-p*+, +*--preview*+: Show preview instead of printing.
* +*-f*+, +*--pdf*+: The file path to write the PDF to.
==== count
The tab index to print.

View File

@ -31,7 +31,7 @@ from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWidgets import QApplication, QTabBar
from PyQt5.QtCore import Qt, QUrl, QEvent
from PyQt5.QtGui import QKeyEvent
from PyQt5.QtPrintSupport import QPrintDialog, QPrintPreviewDialog
from PyQt5.QtPrintSupport import QPrintDialog, QPrinter, QPrintPreviewDialog
from PyQt5.QtWebKitWidgets import QWebPage
import pygments
import pygments.lexers
@ -301,12 +301,14 @@ class CommandDispatcher:
@cmdutils.register(instance='command-dispatcher', name='print',
scope='window')
@cmdutils.argument('count', count=True)
def printpage(self, preview=False, count=None):
@cmdutils.argument('pdf', flag='f')
def printpage(self, preview=False, count=None, *, pdf=None):
"""Print the current/[count]th tab.
Args:
preview: Show preview instead of printing.
count: The tab index to print, or None.
pdf: The file path to write the PDF to.
"""
if not qtutils.check_print_compat():
# WORKAROUND (remove this when we bump the requirements to 5.3.0)
@ -322,6 +324,14 @@ class CommandDispatcher:
Qt.WindowMinimizeButtonHint)
diag.paintRequested.connect(tab.print)
diag.exec_()
elif pdf:
pdf = os.path.expanduser(pdf)
directory = os.path.dirname(pdf)
if directory and not os.path.exists(directory):
os.mkdir(directory)
printer = QPrinter()
printer.setOutputFileName(pdf)
tab.print(printer)
else:
diag = QPrintDialog()
diag.setAttribute(Qt.WA_DeleteOnClose)