void ItemSplitter::printpdf(const QString& fileName){

    QPrinter printer_html(QPrinter::ScreenResolution);
    printer_html.setPageSize(QPrinter::A4);
    printer_html.setOutputFormat(QPrinter::PdfFormat);
    printer_html.setOutputFileName(fileName);
    QPainter painter( &printer_html );

    QSizeF pageSize(printer_html.pageRect().width(),printer_html.pageRect().height());

    TreeItem * treeItem = rootItem;
    QString logo = ("");
    QString date = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");

    QString htmltable[2],htmlbody[2],htmlfront[2],htmltext;

    htmlbody[0] +="<html>";
    htmlbody[0] +="<body>";
    htmlbody[1] +="</body>";
    htmlbody[1] +="</html>";

    htmltable[0] +="<div><table width=\"85%\" align=\"center\">";
    htmltable[1] +="</table></div>";

    htmlfront[0] +=htmltable[0];
    htmlfront[0] +="<tr><td><h1>"+QString("Parameter Report") + "</h1></td><td align=\"right\"><h1>"+ logo +"</h1></td> </tr>";
    htmlfront[0] +="<tr><td><h3>"+ date + "     Page ";
    htmlfront[1] +="</h3></td><td align=\"right\" ><img src = \"logoprint.png\"></td></tr>";
    htmlfront[1] +="<tr><td colspan = \"2\"><hr/></td></tr>";
    htmlfront[1] +=htmltable[1];

    int pagenum = 0;
    int counter = 0 ;
    htmltext.clear();
    htmltext+=htmlfront[0];
    htmltext+=QString::number(++pagenum);
    htmltext+=htmlfront[1];
    htmltext += htmlbody[0];
    htmltext += htmltable[0];
    for( int i = 0 ; i < treeItem->childCount() ; ++i)
    {
         TreeItem* it1 = treeItem->child(i);
         htmltext +="<tr>";
         htmltext += "<td colspan = \"4\">";
         htmltext += "<strong>";
         htmltext+= "Collumn " + it1->data(E_COORDINATE).toString();
         htmltext+= ": " + it1->data(E_NAME).toString();
         htmltext += "</strong>";
         htmltext += "</td>";
         htmltext +="</tr>";
         counter += 3;
         for( int i = 0 ; i < it1->childCount() ; ++i ,++counter)
         {
              TreeItem* it2 = it1->child(i);
              htmltext +="<tr>";
              htmltext +="<td>";
              htmltext+= it2->data(E_COORDINATE).toString();
              htmltext +="</td>";

              htmltext +="<td>";
              htmltext+= it2->data(E_NAME).toString();
              htmltext +="</td>";

              htmltext +="<td>";
              htmltext+= it2->data(E_VALUE).toString();
              htmltext +="</td>";

              htmltext +="<td align=\"right\">";
              htmltext+= it2->data(E_UNIT).toString();
              htmltext +="</td>";
              htmltext +="</tr>";
         }
         htmltext +="<tr>";
         htmltext += "<td colspan = \"4\">";
         htmltext += "</td>";
         htmltext +="</tr>";
         int nextcount;
         if(i + 1 < treeItem->childCount()){
            it1 = treeItem->child(i + 1);
            nextcount = it1->childCount();
         }
         else
         {
            nextcount = 0;
         }
         if(counter + nextcount + 3 > 65 )
         {
            counter = 0 ;
            htmltext+=htmltable[1];
         // htmltext+="<div style=\"page-break-after:auto !important;\"></div>";
            QTextDocument text_document;
            text_document.setHtml(htmltext);
            text_document.setPageSize(pageSize);
            text_document.drawContents(&painter);
            printer_html.newPage();
            htmltext.clear();
            htmltext+=htmlfront[0];
            htmltext+=QString::number(++pagenum);
            htmltext+=htmlfront[1];
            htmltext +=htmltable[0];
        }
    }
   // qDebug() << html;
    if( counter > 0 )
    {
         htmltext += htmlbody[1];
         QTextDocument text_document;
         text_document.setHtml(htmltext);
         text_document.setPageSize(pageSize);
         text_document.drawContents(&painter);
         qDebug() <<" page count:" << text_document.pageCount();
    }

}

 

posted on 2018-12-05 16:25  静观海月  阅读(520)  评论(0编辑  收藏  举报