how to select file and then print to printer ?
-
Hello , i want to select file ( most probably PDF ) and then want to print it on paper.
So far i have tried this
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QPrinter> #include <QPrintDialog> #include <QFile> #include <QFileDialog> #include <QDebug> #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_select_file_clicked() { file_path = QFileDialog::getOpenFileName(this,"Select File","c://"); qDebug()<<file_path; } void MainWindow::on_print_clicked() { QPrinter printer; QPrintDialog print_dialog(&printer,this); if(print_dialog.exec() == QDialog::Rejected) { QMessageBox msg; msg.setText("ERROR"); msg.show(); } }
-
@Qjay said in how to select file and then print to printer ?:
QPrintDialog: Cannot be used on non-native printers
Well it would have been useful if you had explained that was the situation in the first place :)
For PDF under Windows, read https://forum.qt.io/topic/5958/qprintdialog-error-on-qt-windows-environment/2:
QPrintDialog can't be used with pdfs on Windows because it is a native print dialog and has no knowledge of Qt's PDF printer.
-
Hi
Since you want to print an EXISTING pdf file, its not
possible directly with Qt.
Qt can create a pdf file but its not possible to load/display/print
external one as far as i know.
(its outside Qt to display pdfs)
So i think you need external lib for that.
https://poppler.freedesktop.org/Can I ask you actual use case and what platform you need to support ?
-
hey , the platform is windows (windows server) only . The actual use case is to print the pdf to a network printer or any printer available. the application will be on a RDP server.
Why it's needed ?
Guacamole client cannot print files. https://guacamole.apache.org/faq/#local-printers -
@Qjay
hi
Ok, if you dont need processing in the Qt app , you could just use external tool to make it possible to print pdf via commandlinehttp://www.win10pdf.com/pdf-to-printer.html
or for a more complex one, use ghostscript.
https://stackoverflow.com/questions/2599925/how-to-print-pdf-on-default-network-printer-using-ghostscript-gswin32c-exe-shesome pdf viewers also support the print % options and could be used.
So you could use QProcess to start app that prints the pdf.
-
@Qjay
This may not help because I do not know quite how you would do this directly from Qt, but when we want to print a PDF under Windows we use the underlying Win32 functionShellExecute()
(https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153(v=vs.85).aspx) to go (effectively)ShellExecute("print", native-path-to-file)
, and let Windows shell sort it out.This is effectively the same as right-clicking on the file and picking the
Print
action. So you don't need to know anything about the actual process which does the printing, its arguments etc.