PDA

View Full Version : Decode base64 into PDF file



nhocjerry
30th July 2013, 12:32
I have a PDF file which has been encoded to base64. Now I want to convert these base64 script and write to a file to make a PDF file, I have try as following code:



QByteArray data = QByteArray::fromBase64(ui->txtData->toPlainText().toAscii());
QFile file("output.pdf");
file.open(QIODevice::WriteOnly | QIODevice::Text);
QTextStream out(&file);
out << data;
file.close();


It can create the PDF file but it can not be read, the Foxit Reader say that "format error". I check the data has been decoded and I found that some special character become "?". How could I get all the character out? Could anyone can give me a solution?

wysota
30th July 2013, 19:40
QTextStream is for streaming textual data. PDF is binary data so it is not suited for QTextStream. Use QFile::write() instead.

nhocjerry
31st July 2013, 02:55
QTextStream is for streaming textual data. PDF is binary data so it is not suited for QTextStream. Use QFile::write() instead.

I use QFile::write() but it still error and I try QDataStream instead, it works fine now. Thank you!

ChrisW67
31st July 2013, 05:08
... then you are doing it wrong. If QDataStream::writeRawData() works then QIODevice::write() must work: the former being a thin wrapper over the latter.