@JonB Yep, I've just found that I need to wait for loading page before printing and the solution is connect loadFinished signal to method I need, that do printToPdf() in it. I thought that WebEngineView does not need to wait for the local html file to load, but it was mistaken.
UPD: Also, I've found a way to print multiple pdfs. You need to make a QEventLoop obj, connect loadFinished signal to QEventLoop::quit and after setHtml just call loop.exec() and you can guarantee that file you need will load and prints correctly only after loading of the previous page code.
Example:
// Header
class MyClass : public QObject
{
Q_OBJECT
private:
QEventLoop loop;
QWebEngineView *webView;
public:
void saveCardPdf();
// Cpp
MyClass::MyClass(QObject *parent ): QObject(parent)
{
webView = new QWebEngineView();
connect(webView, &QWebEngineView::loadFinished, &loop, &QEventLoop::quit);
}
...
void MyClass::saveCardPdf()
{
...
// Load your html code..
webView->setHtml(html); // html - string of one page
loop.exec();
webView->page()->printToPdf(path);
// Modify name for another file
path.replace(".pdf", "_1.pdf");
webView->setHtml(html1); // html1 - string of another page
loop.exec();
webView->page()->printToPdf(path);
}
It's not perfect solution, but I think you understand the idea