Results 1 to 5 of 5

Thread: Sql Server and PDF files

  1. #1
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Sql Server and PDF files

    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.

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sql Server and PDF files

    Quote Originally Posted by kroenecker
    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

    Qt Code:
    1. QString Sqlitedb::encodeBase64( QString xml )
    2. {
    3. QByteArray text;
    4. text.append(xml);
    5. return text.toBase64();
    6. }
    7.  
    8. QString Sqlitedb::decodeBase64( QString xml )
    9. {
    10. QByteArray xcode("");;
    11. xcode.append(xml);
    12. QByteArray precode(QByteArray::fromBase64(xcode));
    13. QString notetxt = precode.data();
    14. return notetxt;
    15. }
    To copy to clipboard, switch view to plain text mode 

    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....

  3. #3
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sql Server and PDF files

    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?

  4. #4
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sql Server and PDF files

    Quote Originally Posted by kroenecker
    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...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 :

    Qt Code:
    1. QString readfilepdf( QString filename )
    2. {
    3. QString inside = "";
    4. QFile file(filename);
    5. if (file.exists()) {
    6. if (file.open(QFile::ReadOnly | QFile::Text)) {
    7. inside = encodeBase64(QString::fromUtf8(file.readAll()));
    8. /* || inside = encodeBase64(file.readAll()); */
    9. file.close();
    10. }
    11. }
    12. return inside;
    13. }
    To copy to clipboard, switch view to plain text mode 

    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 ....

  5. #5
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sql Server and PDF files

    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:

    Qt Code:
    1. //addfile.filepath contains the full path+filename IE: c:\folder\test.jpg
    2. bool ProgramMain::addFile() {
    3. //convert file into QByteArray:
    4. QByteArray filedata = "";
    5. QString filename = "";
    6. QString base = "";
    7. QString ext = "";
    8.  
    9. QFile file(addfile.filepath); //use the file that we selected;
    10. QFileInfo fileinfo(addfile.filepath);
    11.  
    12. if (file.exists()) {
    13. if (file.open(QFile::ReadOnly)) {
    14.  
    15.  
    16. filedata = file.readAll();
    17. filename = fileinfo.fileName(); //read the name of the file that we are looking for
    18. base = fileinfo.baseName(); //filename
    19. ext = fileinfo.completeSuffix();//extension
    20.  
    21. file.close();
    22. }
    23. } else {
    24. //alert the user that we selected a file that does not exist for upload
    25. return false;
    26. }
    27. /////////////////////////////////////////////
    28. // here you use QSqlQuery and bindvalue() with your filedata and other info that you collected
    29. ///////////////////////////////////////////////
    30. return true;
    31.  
    32.  
    33. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. bool ProgramMain::getFile() {
    2. /////////////////////////////////////////////
    3. // here you use QSqlQuery and query.value(int).toString() for filename,
    4. // and query.value(int).toByteArray() for filedata (the binary data) to pull
    5. // the file out then write it to disk (maybe as a temp file for opening on client
    6. // machine)
    7. ///////////////////////////////////////////////
    8.  
    9.  
    10. QFile file(filename);
    11.  
    12.  
    13.  
    14. if (file.open(QFile::WriteOnly)) {
    15. file.write(filedata);
    16. file.close();
    17.  
    18. }
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 26th June 2007 at 21:01. Reason: wrapped too long lines

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.