How to control html code styling in Qprinter



  • Hello every one

    i have a problem with styling html code for printing

    i have html file with css code

    i read this file like so @void Vente::print_Facture()
    {
    QString html;
    QFile reportfile("E:/apprendreQt/gestionstock6/includes/reports/facture.html");

    if ( ! reportfile.open(QIODevice::ReadOnly | QIODevice::Text) ) {
        return;
    }
    html = reportfile.readAll();
    replaceHtml(html);
    

    }@

    then replace html function replace some words in html ( witch mean i'm using the file as template and fill it with data from database )

    @void Vente::replaceHtml(QString &html)
    {
    // get vente id
    int num_bl = ui->numeroBLlineEdit->text().toInt() ;
    QString requette = "SELECT produits.designation,produits_en_ventes.qte_out,produits_en_ventes.prix,(produits_en_ventes.qte_out * produits_en_ventes.prix) as montant from ventes left join produits_en_ventes on ventes.vente_id = produits_en_ventes.vente_id left join produits on produits_en_ventes.produit_id = produits.produit_id where ventes.vente_id = :vente_id ";

    if(!m_db->isOpen())
        QMessageBox::critical(this,tr("Inventoria Solution"),m_db->lastError().text()) ;
    else{
        m_query->clear();
        m_query->prepare(requette);
        m_query->bindValue(":vente_id",num_bl);
    
        if(!m_query->exec())
            QMessageBox::critical(this,tr("Inventoria Solution"),m_query->lastError().text()) ;
        else{
            html = html.replace("%entrepriseNumRegCommerce%","Numéro de registre de commece ")
                    .replace("%entrepriseNumIdenFiscale%","Numéro d'identifiant fiscale")
                    .replace("%numFac%","num facture")
                    .replace("%nomClient%","Nom de client")
                    .replace("%numRegCommerce%","client numéro de registre de commerce")
                    .replace("%numIdenFiscale%","client identifiant fiscale");
    
            while(m_query->next())
            {
                html +=  "<tr>"
    
                                 "<td>"+ m_query->value(0).toString() +"</td>"
                                 "<td>"+ QString::number(m_query->value(1).toInt())+"</td>"
                                 "<td>"+ QString::number(m_query->value(2).toInt())+"</td>"
                                 "<td>"+ QString::number(m_query->value(2).toInt())+"</td>"
                                 "</tr>" ;
            }
    
            html += "</tbody>"
                    "</table>"
                    "&lt;!-- footer of facture --&gt;"
                    "</div>"
                    "&lt;/body&gt;"
                    "&lt;/html&gt;";
                    print_Html(html);
    
        }
    
    }
    

    }@

    then i print html

    @
    void Vente::print_Html( QString &html)
    {
    QPrintDialog printDialog(m_printer, this);
    if (printDialog.exec()) {
    QTextDocument textDocument;
    textDocument.setHtml(html);
    textDocument.print(m_printer);
    }

    }
    @

    the problem is when the print dialog appears and print the html code the styling not works at all

    how i can control the postion of html element as well as styling them


  • Moderators

    i assume the opening element of the table is the end in your template html file? Meaning is the HTML you want to print at the end valid?

    An advice: save yourself from some pain and don't use QTextDocument when you need HTML features :P
    I would suggest you to use QWebView instead and use it's "print()":http://qt-project.org/doc/qt-5.0/qtwebkit/qwebview.html#print method.



  • Ok, thank's for advices , but what library should i include to my .pro file

    because i'm getting an error

    E:\apprendreQt\gestionstock6\vente.cpp:595: error: undefined reference to `_imp___ZNK8QWebView5printEP8QPrinter'


  • Moderators

    QtWebkit module



  • ok, it works a little bit better then before but i still can't controle the position of element in the page

    i have coded the report page as template it works in firefox every element in the place where i want it , but when i click print button , the position of elements changed , plz tell me witch is the correct way to print using html and Qwebview or other garantate way



  • i have a porblem like this guy

    http://qt-project.org/forums/viewthread/32643/



  • Plz any body help me



  • I need a printing solution for my project !!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.