1

I have this code for printing my data. But I couldn't add html string to css. I need to add pdf document to css. Because html string so close to right side of paper.

 QString html=
           "<!DOCTYPE html><html><body><b>"+json_map["Sample"].toString()+": </b><a>"+ui->sampleText->text()+"</a><br><b>"+json_map["Operator"].toString()+" : </b> <a>"+ui->operatorText->text()+"</a><br><b>"+json_map["Evalutaion"].toString()+" : </b> <a>"+ui->evalText->text()+" </a><br>"+
           "<b>"+json_map["Date"].toString()+" : </b> <a>"+ui->date->text()+"</a><br><br>"+
           "<b>"+json_map["Mixer"].toString()+" : </b> <a>300 </a>"+
           "<b>"+json_map["Moisture"].toString()+ ": </b> <a>"+ui->moistureText->text()+"</a><br>"+
           "<a>"+json_map["Consistency"].toString()+ ": </a><b></b><a>"+json_map["With"].toString()+" </a><a>"+ui->absorptionText->text()+"</a><br><br>"+
           "<b>"+json_map["Water500Fu"].toString()+" : </b> <a></a><br>"+
           "<b>"+json_map["Water14%"].toString()+" : </b> <a></a><br>"+
           "<b>"+json_map["DevelopmentTime"].toString()+" : </b> <a></a><br>"+
           "<b>"+json_map["Stability"].toString()+" : </b> <a></a><br>"+
           "<b>"+json_map["Softening10min"].toString()+" </b> <a></a><br>"+
           "<b>"+json_map["Softening12min"].toString()+" : </b><a></a><br>"+
           "<b>"+json_map["FQN"].toString()+" : </b> <a></a><br>"+
           "<b>"+json_map["Remarks"].toString()+" : </b><a>"+ui->remarkText->text()+"</a><br>"+
           "</body>"+
           "</html>";

   QTextDocument parent;
   parent.setHtml(html);
   QPrinter printer(QPrinter::HighResolution);
   printer.setOutputFormat(QPrinter::PdfFormat);
   printer.setColorMode(QPrinter::Color);
   printer.setOutputFileName(QDir::homePath()+"/Desktop/"+ui->operatorText->text());
   QPrintDialog *dlg = new QPrintDialog(&printer, this);
   dlg->setWindowTitle(QObject::tr("Bastak Test"));

   if(dlg->exec() == QDialog::Accepted) {

       QPainter painter(&printer);
       painter.drawPixmap(500, 5500, QPixmap(ui->qcustom->toPixmap(800,500,10.0)));
       parent.setDefaultFont(QFont("Times", 200));
       parent.drawContents(&painter);
       painter.end();
       //formula.savejson(printer.outputFileName());

    }
   delete dlg;

1 Answer 1

1

I have some difficulties to understand your question, so I make a few assumptions and wait for your reply:

  • You want to generate a PDF from HTML,
  • You need to apply CSS to the HTML

So to test it, I did the following minimum example:

#include <memory>
#include <QApplication>
#include <QPrinter>
#include <QString>
#include <QDebug>
#include <QTextDocument>
#include <QDir>
#include <QPrintDialog>
#include <QPainter>

int main( int argn, char **argc)
{
    QApplication app(argn, argc);
    QString html="<!DOCTYPE html><html><body style='color: blue;'>Hello World!</body></html>";

    QTextDocument parent;
    parent.setHtml(html);
    QPrinter printer(QPrinter::HighResolution);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setColorMode(QPrinter::Color);
    printer.setOutputFileName("test.pdf");

    QPainter painter(&printer);
    parent.setDefaultFont(QFont("Times", 200));
    parent.drawContents(&painter);
    painter.end();

   return 0;
}

I get the following result:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I got my answer thank you for helping. sorry for my lang. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.