PDA

View Full Version : Sql Server and PDF files



kroenecker
26th June 2006, 22:24
I'm using the generic driver to access a microsoft sql server db. I was wondering could someone point me to a resource that explains how to store a pdf file into a database? I want to be able to save and retreive the file of course.

Thanks.

patrik08
27th June 2006, 00:57
I'm using the generic driver to access a microsoft sql server db. I was wondering could someone point me to a resource that explains how to store a pdf file into a database? I want to be able to save and retreive the file of course.

Thanks.


debend how Your build pdf?

My modern method....
All image ist on server http://domainx.bla/pics/***
I compose xml and i xslt-fo this file http://en.wikipedia.org/wiki/XSL_Formatting_Objects
i put this fo file to sqlite2 on encodeBase64(readall) same as mail attachment... flat file on line on sql insert......
on request the pdf programm on server fop this file ( http://xmlgraphics.apache.org/fop/ )
take image from http://localhost and on 0.8 sec... i have a fresh pdf whit the new date of day....

My projekt http://sourceforge.net/projects/qtexcel-xslt/ make pdf from table ... landscape or portrait depend of nummer max column... on fop method




QString Sqlitedb::encodeBase64( QString xml )
{
QByteArray text;
text.append(xml);
return text.toBase64();
}

QString Sqlitedb::decodeBase64( QString xml )
{
QByteArray xcode("");;
xcode.append(xml);
QByteArray precode(QByteArray::fromBase64(xcode));
QString notetxt = precode.data();
return notetxt;
}



If you direkt read binary/text mode the pdf and encodeBase64 on same way as email attachment .... i am sure the output decodeBase64 stay a acceptable pdf on just mime....

kroenecker
29th June 2006, 15:28
patrik,

I appreciate your reply. First of all I'm not generating the pdf. I already have pdf's that I simply need to store. So I guess my question is: do I dump the pdf file into a byte array and then insert that into the database?

Is that the basic idea? And since I'm using the open source version for windows, the best way to handle opening the pdf is to dump the contents of that database entry to the desktop?

patrik08
30th June 2006, 10:36
patrik,

I appreciate your reply. First of all I'm not generating the pdf. I already have pdf's that I simply need to store. So I guess my question is: do I dump the pdf file into a byte array and then insert that into the database?

Is that the basic idea? And since I'm using the open source version for windows, the best way to handle opening the pdf is to dump the contents of that database entry to the desktop?

"microsoft sql server" is not a db for my work i preferred mysql or the new imb db2 opensource database but you can test...
http://www.devarticles.com/c/a/MySQL/Blobbing-Data-With-PHP-and-MySQL/
method or read dir or single pdf as mail attachment and store blob data to db
Qfileinfo mime filename & times...

db table like so...
id | mime | filename | blob | lastupdate | filetime |

test byte array to encodeBase64(xx.data) or direct :


QString readfilepdf( QString filename )
{
QString inside = "";
QFile file(filename);
if (file.exists()) {
if (file.open(QFile::ReadOnly | QFile::Text)) {
inside = encodeBase64(QString::fromUtf8(file.readAll()));
/* || inside = encodeBase64(file.readAll()); */
file.close();
}
}
return inside;
}

and so QString not destroi nothing .... i observer qt to dual convert utf8 ...

output...

QTextCodec *codecutf8 = QTextCodec::codecForMib(106);
QTextStream in(&data);
in.setAutoDetectUnicode(false);
in.setCodec(codecutf8);


blob saved as encodeBase64 is sure to non destroy binary information of pdf file ...
IMO
some programm wo produce pdf ... the source is like xml,html plain text other make a binary result ....

tpf80
26th June 2007, 11:50
I couldnt get the "encoding" method that was described here to work, but heres what I was able to do that worked fine for mysql. I read the file into QByteArray and then used bindvalue on the insert/select queries for the array so it wouldnt be ruined:



//addfile.filepath contains the full path+filename IE: c:\folder\test.jpg
bool ProgramMain::addFile() {
//convert file into QByteArray:
QByteArray filedata = "";
QString filename = "";
QString base = "";
QString ext = "";

QFile file(addfile.filepath); //use the file that we selected;
QFileInfo fileinfo(addfile.filepath);

if (file.exists()) {
if (file.open(QFile::ReadOnly)) {


filedata = file.readAll();
filename = fileinfo.fileName(); //read the name of the file that we are looking for
base = fileinfo.baseName(); //filename
ext = fileinfo.completeSuffix();//extension

file.close();
}
} else {
//alert the user that we selected a file that does not exist for upload
return false;
}
/////////////////////////////////////////////
// here you use QSqlQuery and bindvalue() with your filedata and other info that you collected
///////////////////////////////////////////////
return true;


}


then to pull out the data and do something with it:



bool ProgramMain::getFile() {
/////////////////////////////////////////////
// here you use QSqlQuery and query.value(int).toString() for filename,
// and query.value(int).toByteArray() for filedata (the binary data) to pull
// the file out then write it to disk (maybe as a temp file for opening on client
// machine)
///////////////////////////////////////////////


QFile file(filename);



if (file.open(QFile::WriteOnly)) {
file.write(filedata);
file.close();

}

}