0

I am using Qt4.8, What I want is open a pdf and print that pdf automatically through cmd.exe, without clicking on print button in pdf reader by using QProcess:

I have two different code that do two different task: Opne Pdf

QString scmd= "cmd.exe";
list.push_back("/C");
list.push_back("test.pdf");
Process.start(scmd, list);
Sleep(2000);

Print pdf without open it

QString scmd2 = "C:/Program Files (x86)/Adobe/Reader 11.0/Reader/AcroRd32.exe.exe"
list2.push_back("/t");
list2.push_back("test.pdf");
Process.start(scmd2, list2);
Sleep(2000);

So I want to merge this command, I dont know how I can do that? Please suggest me something

  • Is there any reason to use `Sleep(2000)`? What exactly do you mean by *merging* commands? what would be the problem if you just had those commands executing one after the other? – Mike Dec 26 '16 at 09:40
  • No reason for sleep() its just copy paste mistake forgot to remove. Merging means run simultaneously which means open pdf file and print it. When I run one after another this will not executed ( both command failed) . – User041188 Dec 26 '16 at 09:49
  • and I want to use default PDF reader not the hard coded one scmd2 variable – User041188 Dec 26 '16 at 09:51
  • 1
    What is exactly failing when you run the commands one after the other? you can use [`QDesktopServices::openUrl()`](https://doc.qt.io/qt-4.8/qdesktopservices.html#openUrl) to open a file using its default application. But there is no standard argument to pass for printing (if the user had a program other than acrobat reader installed). If you want a portable solution for printing PDF files, I think that you need to implement it in your program instead of executing commands like that, see [Handling PDF files in Qt](https://wiki.qt.io/Handling_PDF). – Mike Dec 26 '16 at 10:02

1 Answers1

1

You can fetch all information from HKEY_CLASSES_ROOT of windows registry.

Here is a example how to fetch default path to printing software and how to run it. I tested it on Qt 5.7

#include <QSettings>
#include <QProcess>
#include <QDebug>

int main(int argc, char *argv[])
{
    const QString classesRoot = "HKEY_CLASSES_ROOT";

    // get ID of .pdf extension
    QSettings pdfSettings(classesRoot + "\\.pdf", QSettings::NativeFormat);
    QString pdfId = pdfSettings.value("Default").toString();

    // get path to default program that associated with PDF files
    QString printPath = QSettings(classesRoot + "\\" + pdfId + "\\shell\\print\\command", QSettings::NativeFormat).value("Default").toString();
    QString openPath = QSettings(classesRoot + "\\" + pdfId + "\\shell\\open\\command", QSettings::NativeFormat).value("Default").toString();
    qDebug() << "print path" << printPath;
    qDebug() << "open path" << openPath;

    // open .pdf file
    QProcess::startDetached(openPath.arg("full path to pdf file.pdf") );

    // print .pdf file
    QProcess printProcess;
    printProcess.start(printPath.arg("full path to pdf file.pdf") );
    printProcess.waitForFinished(-1);

    return 0;
}
ramzes2
  • 773
  • 5
  • 13
  • Thanks ramzes2, thats actually worked as expected, Unfortunately it will not open the pdf. Can you suggest something? – User041188 Dec 27 '16 at 09:55
  • You can similarly fetch open command from "shell\open\command" subkey. I added sample to my answer. – ramzes2 Dec 27 '16 at 10:20
  • its give error while opening the file. Error message "There was error while opening this document. file not found" – User041188 Dec 27 '16 at 12:27
  • When i remove this "shell\open\command" command everything works perfectly. – User041188 Dec 27 '16 at 12:29
  • Try to use `QDesktopServices::openUrl()` for opening, as proposed Mike – ramzes2 Dec 27 '16 at 12:31
  • Sorry my mistake, not passing file path correctly. works perfectly. Thanks ramzes2 – User041188 Dec 27 '16 at 12:49
  • Can you tell me if pdfreader.exe is open and I have to close it and Then re-open with New Window, How we can achieve closing of pdfreader.exe – User041188 Dec 28 '16 at 10:05
  • if pdfreader started from your code then you can get status and terminate id by corresponding QProcess object. otherwise you can try to find window of pdfreader by FindWindow winapi function and send WM_CLOSE to it or enumerate all processes and terminate needed. – ramzes2 Dec 28 '16 at 12:12
  • ok,. When I send first print command and wait for finished, after that I will send another print command it will hang the application. May I know why? – User041188 Dec 29 '16 at 06:05
  • I think due to waitForFinished function, Is there any alternative for that? – User041188 Dec 29 '16 at 06:08
  • I recently update my adobe reader with new version, and printPath not showing the path, but on the other hand open command is working perfectly, Can you tell why this is happening? – User041188 Jan 09 '17 at 10:22
  • How I can add specific printer in this code don't want to use default printer configured in windows...any Idea? – User041188 Feb 07 '17 at 13:29