Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Forum Updated on Feb 6th

    Unsolved Auto generate PDF's

    General and Desktop
    6
    8
    296
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Scott Krise
      Scott Krise last edited by

      Hello Everyone,

      I need to auto-generate a PDF within my program, then send it via email. I have a 3rd party software I can use to send the emails, but to use it, I first need to create PDF without leaving my CPP program.

      Currently, we do most of our report writing through Crystal...so I'm not sure quite where to start on this.

      Thanks,

      Scott

      J.Hilk 1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @Scott Krise last edited by

        @scott-krise
        hi,
        Qt has the https://doc.qt.io/qt-5/qpdfwriter.html class,
        that should have everything you need to create across platforms PDF's

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply Reply Quote 2
        • Scott Krise
          Scott Krise last edited by

          Thats great!! Thanks so much!

          1 Reply Last reply Reply Quote 0
          • Scott Krise
            Scott Krise last edited by

            Looks like that is only available in qt-5 and up? We are currently in 4.4.3. I know we need to update...just haven't yet. What are my options with what we currently have?

            Pablo J. Rogina JonB 2 Replies Last reply Reply Quote 0
            • Pablo J. Rogina
              Pablo J. Rogina @Scott Krise last edited by

              @scott-krise I was going to suggest using Poppler library, but it looks like it's also tied to Qt 5...

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply Reply Quote 0
              • JonB
                JonB @Scott Krise last edited by JonB

                @scott-krise
                For Qt 4, the only way I know might work is: does the QPrinter support print-to-PDF-and-save-to-file? In code I upgraded from 4 to 5 I think that's how they were doing save PDF from Qt.

                1 Reply Last reply Reply Quote 0
                • P
                  PSI_lbc last edited by

                  Pretty sure Qt4.7 had the PDF support in the Print Dialog..

                  0_1567114723159_qt-PrintDialog-wPDF.png

                  Or you could try..

                  void AppData::ConvertToPDF( QString thefile )
                  {
                    // load a place holder image and output in .pdf format
                    //
                    // input:  filename of what to convert
                    // output: input file converted to PDF
                    //
                    // NOTE: you may need to experiment with output canvas dimensions
                    //       tested with .jpg and .png
                    //
                    #if DEBUG_PDF
                    qDebug() << "*AppData::ConvertToPDF:";
                    #endif
                  
                    // initialize a source and destination path..different on iOS, Android, and Desktop
                    //QString docsPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
                    //if ( ! docsPath.endsWith( "/" ) ) docsPath += "/";
                    //QPixmap canvas( docsPath + "yourfile.png", nullptr, Qt::AutoColor );
                    QString docsPath = ur->docpath;
                  
                    // read the graphics file into your "image canvas"
                    //QPixmap canvas( docsPath + thefile, nullptr, Qt::AutoColor );
                    QImage canvas( docsPath + thefile );
                  
                    // change image size to something else ??
                    // NOTE: 2400 assumes ~8.5x11 @ 300dpi with margins
                    QImage scaled = canvas.scaledToWidth( 2400 );
                  
                    // do the printer setup
                    QPrinter printer;
                    QPainter painter;
                  
                    // arbitrary output file name and page format
                    printer.setOutputFileName( docsPath + "test.PDF" );
                    printer.setOutputFormat( QPrinter::PdfFormat );
                    printer.setResolution( 300 );
                    //printer.setPageMargins( 0.1, 0.1, 0.1, 0.1, QPrinter::Inch );
                    //printer.setPageMargins( 2.54, 2.54, 2.54, 2.54, QPrinter::Millimeter);
                    printer.setPaperSize(QPrinter::A4);
                    //printer.setPaperSize( QPrinter::Letter );
                    //printer.setFullPage( true );
                  
                    // output it to the printer
                    painter.begin( &printer );
                    //painter.drawPixmap( 0, 0, canvas );
                    //painter.drawImage( 0, 0, canvas );
                    painter.drawImage( 0, 0, scaled );
                    painter.end();
                  }
                  
                  
                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    Hi,

                    That's because you are on macOS. The system natively supports printing in a PDF.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post