如何打印pdf文件在Qt [英] How to print pdf file in Qt

查看:845
本文介绍了如何打印pdf文件在Qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


推荐答案

I不明白你的问题,但现在我明白了。你想使用Qt打印PDF文件,你不想打印成PDF,是吗?

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。
对于Qt中的PDF支持,您需要外部库poppler。检查这篇文章

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

Poppler允许您将PDF文件转换为QImage,您可以轻松打印QImage
like this

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

这里是如何将文本打印成PDF文件。

Here is how do you print text into PDF file.

我试图编辑你的代码,它为我工作,你能检查吗?
也许尝试检查 QPrinter :: isValid()在您的环境中返回 true 。 >

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

这篇关于如何打印pdf文件在Qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆