[Interest] PDF to QByteArray

Rainer Wiesenfarth Rainer_Wiesenfarth at trimble.com
Wed Feb 26 08:02:36 CET 2014


Am 26.02.2014 04:02, schrieb Marcelo Estanislau Geyer:
> It's possible load pdf file to QByteArray? The simple code below is not
> work:
>
> 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 (þÿ

As you did not tell what you expected, I guess the character encoding 
differs, and the strings for /Creator, /Producer, ... should look different.

You might give this snippet a try:

     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;
     QTextStream ts(&file);
     ts.setCodec ("UTF-8");
     QString s_pdf = ts.readAll();
     qDebug() << s_pdf;

     file.close();

The "UTF-8" might need to be replaced by something else, depending on 
the encoding your file uses.

Best Regards / Mit freundlichen Grüßen
Rainer Wiesenfarth

-- 
Software Engineer | Trimble Imaging Division
Rotebühlstraße 81 | 70178 Stuttgart | Germany
Office +49 711 22881 0 | Fax +49 711 22881 11
http://www.trimble.com/imaging/ | http://www.inpho.de/

Trimble Germany GmbH, Am Prime Parc 11, 65479 Raunheim
Eingetragen beim Amtsgericht Darmstadt unter HRB 83893,
Geschäftsführer: Dr. Frank Heimberg, Hans-Jürgen Gebauer

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4282 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140226/be81bcca/attachment.bin>


More information about the Interest mailing list