C++ 如何在Qt中打印pdf文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8296021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:19:51  来源:igfitidea点击:

How to print pdf file in Qt

c++qtpdfprinting

提问by lekhraj

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.

我试图编写一些代码来使用 Qt 打印 pdf 文件,但不知何故它不起作用。如果有人对解决此问题有任何想法,请提供您的提示。

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; 
    } 
}

回答by LukasT

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打印PDF文件,你不想打印成PDF,对吧?

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

Qt 不支持加载和显示 PDF。对于 Qt 中的 PDF 支持,您需要外部库 poppler。检查这篇文章

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

Poppler 允许您将 PDF 文件渲染到 QImage 中,您可以像这样轻松打印 QImage 。

Here is how do you print text into PDF file.

以下是如何将文本打印成 PDF 文件。

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 truein your environment.

我试图编辑你的代码,以便我可以测试一下它对我有用,你能检查一下吗?也许尝试检查是否在您的环境中QPrinter::isValid()返回true

#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();
}