PDA

View Full Version : Upload pdf file in the MySQL database



sujan.dasmahapatra
3rd October 2011, 20:17
Dear Friends

I am working with MySQL database using Qt. I want to store a pdf file in the database. and show the contents of the pdf when user wants. How can I upload a pdf file in the database and open the file when needed from the database. Please give me some idea.

I know only two types INT and VARCHAR to store int and some string. But how can i upload the pdf file. Please help me if you know anyone. Thanks a lot. sujan

boudie
3rd October 2011, 21:09
You can store it in the database as a BLOB (Binary Large Object).
To show the pdf you can use the poppler library.

ChrisW67
4th October 2011, 01:00
You can store it in the database as a BLOB (Binary Large Object).
This is handled as a QByteArray inside your Qt code. Reading a PDF file into a QByteArray is trivial. You will need to use a QSqlQuery::prepare()-ed query and QSqlQuery::bindValue() to insert it into the database.

To show the pdf you can use the poppler library.
Qt does not have any built-in PDF viewing capability: Poppler is an external library that could be use to add a in-program viewer. You could also extract the BLOB to a temporary file and use QDesktopServices::openUrl() to launch the user's PDF viewer of choice.

sujan.dasmahapatra
4th October 2011, 06:14
where can i find the poppler library do i need to install it.

boudie
4th October 2011, 13:46
Forget poppler (for now...)

As ChrisW67 suggests:

...You could also extract the BLOB to a temporary file and use QDesktopServices::openUrl() to launch the user's PDF viewer of choice.

It's much simpler to implement.

sujan.dasmahapatra
10th October 2011, 20:32
Please check this code snippet ui.lineEdit_APPLICATION is the control fetching the filename..........With this my table is not getting updated with the value resume........My table is blank........Whats wrong please tell me .


if(!ui.lineEdit_APPLICATION->text().isEmpty()) {
QByteArray inside;
QString filename = ui.lineEdit_APPLICATION->text();
QStringList list = filename.split(QRegExp("/"), QString::SkipEmptyParts);
QFile file(filename);
if (file.exists()) {
if (file.open(QFile::ReadOnly)) {
inside = file.readAll();
file.close();
}
}
query4.bindValue(":applicationpdf",list.last());
query4.bindValue(":resume",inside);
list.clear();
}
else {
query4.bindValue(":applicationpdf", "");
query4.bindValue(":resume", "");
}

wysota
10th October 2011, 21:00
It pays off to execute the query once it is prepared (if at all).

sujan.dasmahapatra
11th October 2011, 05:42
If I load a pdf or .doc file my table is not updated. But if I load a jpeg file then its uploading in the database. Please tell me why its happening. why pdf or doc file data is not uploading as QByteArray. Thanks Sujan

Added after 24 minutes:

If I give LONGBLOB instead of BLOB then its storing something. Can u tell me how can I fetch the data from database check if this is correct


void CDisplayEntry::openPDF()
{

QSqlQuery query(console->mainwindow->db);
QString queryString = "SELECT * FROM Tbl_StudentLink";
query.exec(queryString);

int field1 = query.record().indexOf("resume");
if(query.seek(index,false))
{
QUrl url = QUrl::fromEncoded(query.value(field1).toByteArray( ));
QTextEdit *viewPDF = new QTextEdit(url.toString());
viewPDF->show();
}

}

ChrisW67
11th October 2011, 07:27
Here's what you are doing:

You store the actual bytes of the PDF file into the database
You retrieve the actual bytes of the PDF file and try to interpret those bytes as a URL
You use a QTextEdit to display a string made from the URL and expect to see the PDF file.

Does this sound correct to you?

Go back and read the earlier responses about your options.

sujan.dasmahapatra
24th October 2011, 22:22
PDF is not working only txt files are working. Poppler library is linux based I need windows. Give me some suggestion how can i view pdf document.

check my snippet.

// To read the document and store in the database


QByteArray inside="";
QFile file(filename);
if (file.exists()) {
if (file.open(QFile::ReadOnly)) {
inside = file.readAll();
file.close();
}
query4.bindValue(":resume",inside);


Retrieving from the database and viewing in TextEdit


QSqlQuery query(console->mainwindow->db);
QString queryString = "SELECT * FROM Tbl_StudentLink";
query.exec(queryString);

int field1 = query.record().indexOf("resume");
if(query.seek(index,false))
{
// QUrl url = QUrl::fromEncoded(query.value(field1).toByteArray( ));
QByteArray pdf = query.value(field1).toByteArray();
QTextEdit *viewPDF = new QTextEdit(QString(pdf));
viewPDF->show();
}

stampede
24th October 2011, 23:15
QByteArray pdf = query.value(field1).toByteArray();
QTextEdit *viewPDF = new QTextEdit(QString(pdf));
What do you expect to see in text edit when you convert the bytes of the file to a string ? A rendered pdf document ?

Anyway, why don't you just write the "pdf" byte array to a file and open it with the system default pdf viewer ?

ChrisW67
25th October 2011, 07:49
PDF is not working only txt files are working.
Surprising given the mangling you put the content rhough in your code snippets.

Poppler library is linux based I need windows.
Poppler can be built in Windows but it is an effort. From the discussion to date I suspect that you do not have the requisite understanding to build it. For example: Compiling Poppler on Windows (http://laconsigna.wordpress.com/2011/07/14/compiling-poppler-on-windows/). MuPDF is another option under GPL3.

Give me some suggestion how can i view pdf document.
We already have, several times. Here (http://www.qtcentre.org/threads/44992-Upload-pdf-file-in-the-MySQL-database?p=204611#post204611) for example.


check my snippet.

Retrieving from the database and viewing in TextEdit


QSqlQuery query(console->mainwindow->db);
QString queryString = "SELECT * FROM Tbl_StudentLink";
query.exec(queryString);

int field1 = query.record().indexOf("resume");
if(query.seek(index,false))
{
// QUrl url = QUrl::fromEncoded(query.value(field1).toByteArray( ));
QByteArray pdf = query.value(field1).toByteArray();
QTextEdit *viewPDF = new QTextEdit(QString(pdf));
viewPDF->show();
}

Uh huh! You have tweaked this since the last time but you still have not grasped that a PDF in a binary data stream and that displaying the raw bytes in a text edit is wasting your time. On the upside you are no longer trying to mangle the binary data by treating it as a URL.

Write the data to a temporary file (use QDesktopServices to find where this should go), build a URL pointing at the temporary file, and use openUrl() to launch the user's PDF reader. This is about as simple as it gets.