Advertisement
Guest User

Федор лалка

a guest
Jan 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <QApplication>
  2. #include <QCommandLineParser>
  3. #include <QFile>
  4. #include <QTextStream>
  5. #include <QWebEnginePage>
  6.  
  7. #include <functional>
  8.  
  9. using namespace std;
  10. using namespace std::placeholders;
  11.  
  12. class Html2PdfConverter : public QObject
  13. {
  14.     Q_OBJECT
  15. public:
  16.     explicit Html2PdfConverter(QString inputPath, QString outputPath);
  17.     int run();
  18.  
  19. private slots:
  20.     void loadFinished(bool ok);
  21.     void pdfPrintingFinished(const QString &filePath, bool success);
  22.  
  23. private:
  24.     QString m_inputPath;
  25.     QString m_outputPath;
  26.     QScopedPointer<QWebEnginePage> m_page;
  27. };
  28.  
  29. Html2PdfConverter::Html2PdfConverter(QString inputPath, QString outputPath)
  30.     : m_inputPath(move(inputPath))
  31.     , m_outputPath(move(outputPath))
  32.     , m_page(new QWebEnginePage)
  33. {
  34.     connect(m_page.data(), &QWebEnginePage::loadFinished,
  35.             this, &Html2PdfConverter::loadFinished);
  36.     connect(m_page.data(), &QWebEnginePage::pdfPrintingFinished,
  37.             this, &Html2PdfConverter::pdfPrintingFinished);
  38. }
  39.  
  40. int Html2PdfConverter::run()
  41. {
  42.     m_page->load(QUrl::fromUserInput(m_inputPath));
  43.     return QApplication::exec();
  44. }
  45.  
  46. void Html2PdfConverter::loadFinished(bool ok)
  47. {
  48.     if (!ok) {
  49.         QTextStream(stderr)
  50.             << tr("failed to load URL '%1'").arg(m_inputPath) << "\n";
  51.         QCoreApplication::exit(1);
  52.         return;
  53.     }
  54.  
  55.     m_page->printToPdf(m_outputPath);
  56. }
  57.  
  58. void Html2PdfConverter::pdfPrintingFinished(const QString &filePath, bool success)
  59. {
  60.     if (!success) {
  61.         QTextStream(stderr)
  62.             << tr("failed to print to output file '%1'").arg(filePath) << "\n";
  63.         QCoreApplication::exit(1);
  64.     } else {
  65.         QCoreApplication::quit();
  66.     }
  67. }
  68.  
  69. int main(int argc, char *argv[])
  70. {
  71.     QApplication app(argc, argv);
  72.     QCoreApplication::setApplicationName("html2pdf");
  73.     QCoreApplication::setApplicationVersion(QT_VERSION_STR);
  74.  
  75.     QCommandLineParser parser;
  76.     parser.setApplicationDescription(
  77.         QCoreApplication::translate("main", "Converts the web page INPUT into the PDF file OUTPUT."));
  78.     parser.addHelpOption();
  79.     parser.addVersionOption();
  80.     parser.addPositionalArgument(
  81.         QCoreApplication::translate("main", "INPUT"),
  82.         QCoreApplication::translate("main", "Input URL for PDF conversion."));
  83.     parser.addPositionalArgument(
  84.         QCoreApplication::translate("main", "OUTPUT"),
  85.         QCoreApplication::translate("main", "Output file name for PDF conversion."));
  86.  
  87.     parser.process(QCoreApplication::arguments());
  88.  
  89.     const QStringList requiredArguments = parser.positionalArguments();
  90.     if (requiredArguments.size() != 2)
  91.         parser.showHelp(1);
  92.  
  93.     Html2PdfConverter converter(requiredArguments.at(0), requiredArguments.at(1));
  94.     return converter.run();
  95. }
  96.  
  97. #include "html2pdf.moc"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement