Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Auto generate PDF's

Auto generate PDF's

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 6 Posters 765 Views
  • 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 KriseS Offline
    Scott KriseS Offline
    Scott Krise
    wrote on last edited by
    #1

    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.HilkJ 1 Reply Last reply
    0
    • Scott KriseS Scott Krise

      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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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


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

      1 Reply Last reply
      2
      • Scott KriseS Offline
        Scott KriseS Offline
        Scott Krise
        wrote on last edited by
        #3

        Thats great!! Thanks so much!

        1 Reply Last reply
        0
        • Scott KriseS Offline
          Scott KriseS Offline
          Scott Krise
          wrote on last edited by
          #4

          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. RoginaP JonBJ 2 Replies Last reply
          0
          • Scott KriseS Scott Krise

            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. RoginaP Offline
            Pablo J. RoginaP Offline
            Pablo J. Rogina
            wrote on last edited by
            #5

            @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
            0
            • Scott KriseS Scott Krise

              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?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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
              0
              • P Offline
                P Offline
                PSI_lbc
                wrote on last edited by
                #7

                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
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved