Create a table and convert it to PDF
-
@Negar_mg
Hi
Like anything else. you print the docQTextDocument doc; ...fill the doc... QPrinter printer(QPrinter::HighResolution); printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName(filename); // where pdf will be stored doc.print(&printer);
-
hi,
I wanted to know is it possible to create a chart with html code, as I created the table?
I wrote the following code but it does not create any chart.
Would you please guide me in this regard as well?
//..........................................................................................................................
// Your HTML codeQString html; html = "<html>" "<body>" "<canvas id='myChart' style='width:100%;max-width:600px'></canvas>" "<script>" "var xValues = [50,60,70,80,90,100,110,120,130,140,150];" "var yValues = [7,8,8,9,9,9,10,11,14,14,15];" "new Chart('myChart', {type: 'line',data: {labels: xValues,datasets: [{fill: false,lineTension: 0,backgroundColor: 'rgba(0,0,255,1.0)',borderColor: 'rgba(0,0,255,0.1)',data: yValues }]}," " options: {legend: {display: false},scales: { yAxes: [{ticks: {min: 6, max:16}}],}}});" "</script>" "</body>" "</html>"; QTextDocument doc ; QTextCursor cursor(&doc); cursor.insertHtml(html); QTextDocumentWriter writer("htmlchart.odt", "ODF"); writer.write(&doc); QPrinter printer(QPrinter::HighResolution); printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName("htmlchart.pdf"); doc.print(&printer);
//..............................................................................................................................................
I also used the "append" command but no graph was drawn.html.append("<html>"); html.append("<body>"); html.append("<canvas id='myChart' style='width:100%;max-width:600px'></canvas>"); html.append("<script>"); html.append("var xValues = [50,60,70,80,90,100,110,120,130,140,150];"); html.append("var yValues = [7,8,8,9,9,9,10,11,14,14,15];"); html.append("new Chart('myChart', {type: 'line',data: {labels: xValues,datasets: [{fill: false,lineTension: 0,backgroundColor: 'rgba(0,0,255,1.0)',borderColor: 'rgba(0,0,255,0.1)',data: yValues }]}"); html.append(",options: {legend: {display: false},scales: { yAxes: [{ticks: {min: 6, max:16}}],}}});"); html.append("</script>"); html.append("</body>"); html.append("</html>");
-
Hi
I don't think it can work that way a normally graphs are drawn by javascript or similar.What kind of graph do you need ? bar or what type?
Im asking as to get a ok graph, i think the easiest is to use QtChart or QCustomplot and render it to an image
and print that too.
However, if you just need simply bar one, we could just hand paint it. -
@Negar_mg QTextDocument only renders static HTML4, in your code you are using chart.js that executes js. One possible solution is to use QWebEngineView:
#include <QApplication> #include <QWebEnginePage> #include <QWebEngineView> int main(int argc, char *argv[]) { QApplication a(argc, argv); QString html = R"HTML(<html> <body> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <canvas id="myChart" style="width: 100%; max-width: 600px"></canvas> <script> var xValues = [50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150]; var yValues = [7, 8, 8, 9, 9, 9, 10, 11, 14, 14, 15]; new Chart("myChart", { type: "line", data: { labels: xValues, datasets: [ { fill: false, lineTension: 0, backgroundColor: "rgba(0,0,255,1.0)", borderColor: "rgba(0,0,255,0.1)", data: yValues, }, ], }, options: { legend: { display: false }, scales: { yAxes: [{ ticks: { min: 6, max: 16 } }] }, }, }); </script> </body> </html>)HTML"; QWebEngineView view; view.resize(640, 480); view.setAttribute(Qt::WA_DontShowOnScreen, true); view.show(); QObject::connect(view.page(), &QWebEnginePage::pdfPrintingFinished, &QCoreApplication::quit); QObject::connect(view.page(), &QWebEnginePage::loadFinished, [&view](bool ok){ qDebug() << ok; if(ok) view.page()->printToPdf(QString("htmlchart.pdf")); else QCoreApplication::exit(-1); }); view.setHtml(html); return a.exec(); }
-
thanks,
I want to prepare a report that includes text, table, photo and chart.
With what was said here, I was able to create text, image, and table using
<QTextCursor>
<QTextDocument>
<QTextTableFormat>
<QTextDocumentWriter>
<QPrinter>
and Convert the created file to pdf.
Now I have to add the chart to that doc, do you have any suggestions for adding the chart to it?