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. QPrinter PDF to QByteArray
Qt 6.11 is out! See what's new in the release blog

QPrinter PDF to QByteArray

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 6.8k Views 1 Watching
  • 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.
  • E Offline
    E Offline
    estanisgeyer
    wrote on last edited by
    #1

    I'm trying to generate a pdf file and save it in a QByteArray, later to attach and send via email. I already do this with other types of files, there is the possibility of generating a pdf with QPrinter and store in a QByteArray?

    Marcelo Estanislau Geyer
    Standard Net Tecnologia / Brazil

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      It is unfortunate that QPrinter does not have a file output option that takes a QIODevice* instead of a file name because that would enable using QBuffer. AFAICT the only option is to write to a file, perhaps a QTemporaryFile for neatness, and then read that into a QByteArray.

      @
      QByteArray pdf;
      QPrinter printer;
      printer.setOrientation(QPrinter::Portrait);
      printer.setOutputFormat(QPrinter::PdfFormat);
      printer.setPaperSize(QPrinter::A4);

      QTemporaryFile temp;
      if (temp.open()) // causes file creation
      temp.close();
      printer.setOutputFileName(temp.fileName());
      // paint
      if (temp.open()) {
      pdf = temp.readAll();
      }
      @

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        If you are using Qt 5, you can try "QPdfWriter":http://qt-project.org/doc/qt-5/qpdfwriter.html with a "QBuffer":http://qt-project.org/doc/qt-5/qbuffer.html

        Hope it helps

        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
        • E Offline
          E Offline
          estanisgeyer
          wrote on last edited by
          #4

          Thanks,

          Marcelo Estanislau Geyer
          Standard Net Tecnologia / Brazil

          1 Reply Last reply
          0
          • E Offline
            E Offline
            estanisgeyer
            wrote on last edited by
            #5

            I did a test using the idea of ​​ChrisW67 and strangely corrupted PDF content is the object (pdf) that stores

            @
            QByteArray pdf;
            QPrinter printer;
            printer.setOrientation(QPrinter::Portrait);
            printer.setOutputFormat(QPrinter::PdfFormat);
            printer.setPaperSize(QPrinter::A4);

            QTemporaryFile temp;
            if (temp.open())
            temp.close();

            printer.setOutputFileName(temp.fileName());

            if (temp.open()) {
            pdf = temp.readAll();
            }
            @

            I changed the format to postscript and to my surprise it worked perfectly. Is a bug of Qt 4.8.X with PDF files?

            @
            QByteArray ps;
            QPrinter printer;
            printer.setOrientation(QPrinter::Portrait);
            printer.setOutputFormat(QPrinter::PostScriptFormat);
            printer.setPaperSize(QPrinter::A4);

            QTemporaryFile temp;
            if (temp.open())
            temp.close();

            printer.setOutputFileName(temp.fileName());

            if (temp.open()) {
            ps = temp.readAll();
            }
            @

            Marcelo Estanislau Geyer
            Standard Net Tecnologia / Brazil

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ChrisW67
              wrote on last edited by
              #6

              Are you painting anything between lines 11 and. 13? I do not think you will even get a blank page unless you at least create a QPainter on the printer.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                estanisgeyer
                wrote on last edited by
                #7

                Yes, I have a class that does the painter. As to this no problem, I just did not mention this code snippet. I opened the temporary file that is created and is correct.

                Marcelo Estanislau Geyer
                Standard Net Tecnologia / Brazil

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ChrisW67
                  wrote on last edited by
                  #8

                  If the temporary file contains a valid PDF the byte array should contain exactly the same bytes.

                  If you later manipulate the byte array as if it is a C-string you will break the data at the first zero byte.

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    estanisgeyer
                    wrote on last edited by
                    #9

                    Hi,

                    I did a simple test that can be played with the code below, and content of QByteArray is not the same file. Any way around this problem? My platform is GNU/Linux and Qt 4.8.5

                    @
                    QFile file("/home/marcelo/Documents/test.pdf");
                    if (!file.open(QIODevice::ReadOnly)) {
                    qDebug() << "file open error";
                    return;
                    }

                    QByteArray ba_pdf = file.readAll();
                    qDebug() << ba_pdf;
                    file.close();
                    @

                    The output is:
                    @
                    "%PDF-1.4
                    1 0 obj
                    <<
                    /Title (þÿ)
                    /Creator (þÿ)
                    /Producer (þÿ
                    @

                    Marcelo Estanislau Geyer
                    Standard Net Tecnologia / Brazil

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      estanisgeyer
                      wrote on last edited by
                      #10

                      It's possible to read pdf file to QBytearray?

                      Marcelo Estanislau Geyer
                      Standard Net Tecnologia / Brazil

                      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