diff -ur QupZilla-2.0.1/src/lib/app/browserwindow.cpp QupZilla-2.0.1-printing/src/lib/app/browserwindow.cpp --- QupZilla-2.0.1/src/lib/app/browserwindow.cpp 2016-06-08 16:24:44.000000000 +0200 +++ QupZilla-2.0.1-printing/src/lib/app/browserwindow.cpp 2016-07-29 04:13:31.479593683 +0200 @@ -81,6 +81,15 @@ #include #include #include +#include +#include +#include + +#ifdef Q_OS_WIN +#include +#else +#include +#endif #ifdef QZ_WS_X11 #include @@ -614,6 +623,39 @@ } } +void BrowserWindow::printPage() +{ +#if QT_VERSION >= QT_VERSION_CHECK(5,7,0) + QPrintDialog* dialog = new QPrintDialog(this); + dialog->setOptions(QAbstractPrintDialog::PrintToFile | QAbstractPrintDialog::PrintShowPageSize); + dialog->printer()->setCreator(tr("QupZilla %1 (%2)").arg(Qz::VERSION, Qz::WWWADDRESS)); + dialog->printer()->setDocName(QzTools::getFileNameFromUrl(weView()->url())); + + if (dialog->exec() == QDialog::Accepted) { + if (dialog->printer()->outputFormat() == QPrinter::PdfFormat) { + weView()->page()->printToPdf(dialog->printer()->outputFileName(), dialog->printer()->pageLayout()); + } else { + QTemporaryFile tempFile(QDir::tempPath() + QSL("/QupZillaPrintXXXXXX.pdf")); + if (tempFile.open()) { // create the file name, and the file to reserve it + tempFile.close(); // close, but don't delete the file + weView()->page()->printToPdf(tempFile.fileName(), dialog->printer()->pageLayout()); +#ifdef Q_OS_WIN + // This may bring up a PDF viewer window, and even keep it open, but it is the best we can do without adding third-party dependencies. + // lpr is not installed by default on Windows, and it also can only print PDF if the printer handles it in hardware. + ShellExecuteW(winId(), L"printto", tempFile.fileName().constData(), ('"' + dialog->printer()->printerName() + '"').constData(), NULL, SW_HIDE); +#else + // TODO: We should probably pass some additional options such as media size. See, e.g., Okular's fileprinter.cpp. + QProcess::execute(QSL("lpr"), QStringList() << QSL("-P") << dialog->printer()->printerName() << tempFile.fileName()); +#endif + } + // The temporary file is automatically deleted here. + } + } + + delete dialog; +#endif +} + void BrowserWindow::bookmarkPage() { TabbedWebView* view = weView(); diff -ur QupZilla-2.0.1/src/lib/app/browserwindow.h QupZilla-2.0.1-printing/src/lib/app/browserwindow.h --- QupZilla-2.0.1/src/lib/app/browserwindow.h 2016-06-08 16:24:44.000000000 +0200 +++ QupZilla-2.0.1-printing/src/lib/app/browserwindow.h 2016-07-29 02:23:01.041552649 +0200 @@ -155,6 +155,7 @@ void webSearch(); void searchOnPage(); void changeEncoding(); + void printPage(); void saveSettings(); void hideNavigationSlot(); diff -ur QupZilla-2.0.1/src/lib/app/mainmenu.cpp QupZilla-2.0.1-printing/src/lib/app/mainmenu.cpp --- QupZilla-2.0.1/src/lib/app/mainmenu.cpp 2016-06-08 16:24:44.000000000 +0200 +++ QupZilla-2.0.1-printing/src/lib/app/mainmenu.cpp 2016-07-29 02:24:06.115900117 +0200 @@ -81,6 +81,9 @@ superMenu->addAction(m_actions[QSL("File/OpenFile")]); superMenu->addSeparator(); superMenu->addAction(m_actions[QSL("File/SendLink")]); +#if QT_VERSION >= QT_VERSION_CHECK(5,7,0) + superMenu->addAction(m_actions[QSL("File/Print")]); +#endif superMenu->addSeparator(); superMenu->addAction(m_actions[QSL("Edit/SelectAll")]); superMenu->addAction(m_actions[QSL("Edit/Find")]); @@ -522,7 +525,9 @@ ADD_ACTION("File/CloseWindow", m_menuFile, QIcon::fromTheme(QSL("window-close")), tr("Close Window"), SLOT(closeWindow()), "Ctrl+Shift+W"); m_menuFile->addSeparator(); ADD_ACTION("File/SendLink", m_menuFile, QIcon::fromTheme(QSL("mail-message-new")), tr("Send Link..."), SLOT(sendLink()), ""); +#if QT_VERSION >= QT_VERSION_CHECK(5,7,0) ADD_ACTION("File/Print", m_menuFile, QIcon::fromTheme(QSL("document-print")), tr("&Print..."), SLOT(printPage()), "Ctrl+P"); +#endif m_menuFile->addSeparator(); m_menuFile->addAction(m_actions[QSL("Standard/Quit")]); diff -ur QupZilla-2.0.1/src/lib/lib.pro QupZilla-2.0.1-printing/src/lib/lib.pro --- QupZilla-2.0.1/src/lib/lib.pro 2016-06-08 16:24:44.000000000 +0200 +++ QupZilla-2.0.1-printing/src/lib/lib.pro 2016-07-29 03:25:23.769602524 +0200 @@ -1,4 +1,4 @@ -QT += webenginecore webenginewidgets webchannel network widgets sql quickwidgets +QT += webenginecore webenginewidgets webchannel network widgets sql quickwidgets printsupport TARGET = QupZilla TEMPLATE = lib