How to print pdf file in Qt
Asked Answered
K

2

5

I have tried to write some code to print a pdf file using Qt but somehow it's not working. If anybody has any idea to solve this problem, please provide your tips.

void ChartViewer::onprintBtnClicked(){ 
    String filename = QFileDialog::getOpenFileName(this,"Open File",QString(),"Pdf File(*.pdf)"); 
    qDebug()<<"Print file name is "<<filename; 
    if(!filename.isEmpty()) { 
        if(QFileInfo(filename).suffix().isEmpty()) 
            filename.append(".pdf"); 

        QPrinter printer(QPrinter::HighResolution);         
        printer.setOutputFormat(QPrinter::PdfFormat);  
        printer.setOutputFileName(filename);
        QPrintDialog*dlg = new QPrintDialog(&printer,this); 

        if(textedit->textCursor().hasSelection()) 
            dlg->addEnabledOption(QAbstractPrintDialog::PrintSelection); 

        dlg->setWindowTitle(tr("Print Document")); 

        if(dlg->exec() == QDialog::Accepted) { 
            textedit->print(&printer); 
        } 

        delete dlg; 
    } 
}
Kirima answered 28/11, 2011 at 12:47 Comment(1)
Perhaps its rather too late, but you can check out the code here.Sexagesimal
I
10

I didn't understand your question, but now I get it. You want to print PDF file using Qt, you don't want to print into PDF, right?

Qt does not have support for loading and display PDF. For PDF support in Qt you need external library poppler. Check this article.

Poppler allows you to render PDF files into QImage and you can easily print QImage like this.

Here is how do you print text into PDF file.

I tried to edit your code so that I can test it a bit and it works for me, can you check? Maybe try to check if QPrinter::isValid() returns true in your environment.

#include <QtGui>
#include <QtCore>

int main(int argc, char **argv) {
    QApplication app(argc, argv);
    QTextEdit parent;
    parent.setText("We are the world!");
    parent.show();

    QString filename = QFileDialog::getOpenFileName(&parent,"Open File",QString(),"Pdf File(*.pdf)"); 
    qDebug()<<"Print file name is "<<filename; 
    if(!filename.isEmpty()) {
        if(QFileInfo(filename).suffix().isEmpty()) {
            filename.append(".pdf"); 
        }

        QPrinter printer(QPrinter::HighResolution);         
        printer.setOutputFormat(QPrinter::PdfFormat);  
        printer.setOutputFileName(filename);
        QPrintDialog*dlg = new QPrintDialog(&printer,&parent); 
        dlg->setWindowTitle(QObject::tr("Print Document")); 

        if(dlg->exec() == QDialog::Accepted) { 
            parent.print(&printer); 
        } 
        delete dlg; 
    } 
    return app.exec();
}
Impignorate answered 28/11, 2011 at 14:22 Comment(9)
@user671112 Does the code above work for you in Windows environment? Did you check if QPrinter::isValid() returns true? Qt is cross-platform, so it should work.Impignorate
Dear Lukas,It's not working for me as i m getting warning "QPrintDialog: Cannot be used on non-native printers " and not able to printing the pdf file.I think we can't use QTextedit for prinitng of pdf file in QT(as i got info from google).If possible could you please guide me on how to perform printing of pdf file through printer on windows environment. I am beginner and i don't have any more idea about this things but am tring my best.Thanks for your reply. regards,Kirima
@user671112 Hm..you are right regarding the dialog, I tested on Linux, I have no Windows machine around right now. But you can print without print dialog if you want. Just comment the QPrintDialog related lines out and call only parent.print(&printer);Impignorate
Dear Lukas, I have commented QPrintDialog and just keep parent.print(&printer);and then ran the program it's running successfully but nothing is printed.I think there is no contents available on textedit named editor that's why no file is printed.Request you to please help me to get out of this issue.Kirima
Ah, I was mistaken by your question and now I get it. You want to print PDF file using Qt, you don't want to print into PDF, right? Qt does not have support for loading and display PDF as far as I know.Impignorate
Dear Lukas, Yes , I want to print pdf file from printer machine using QT.So is it possible through QT or not?.I did google about this and got some article.They are talking about poppler/third part library but i didn't get that how to implement this library.Kirima
@user671112: It's not possible through Qt "out of the box". You need third party library poppler.Impignorate
Here's an updated link to the Poplar library: people.freedesktop.org/~aacid/docs/qt5Ameeameer
#16738045Cassy
P
0

For a long time now, Qt itself supports reading PDF files, using the QtPdf module, which is a wrapper around PDFium.

A PDF document can be loaded using QPdfDocument::load

A print method doesn't exist, but you can:

Panfish answered 12/5, 2024 at 13:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.