Results 1 to 7 of 7

Thread: How to display PDF in Qt?

  1. #1
    Join Date
    May 2008
    Posts
    2
    Platforms
    MacOS X Windows

    Default How to display PDF in Qt?

    Hi!

    is there any ways to display PDF in an Qt application?

    Thanks!
    stelio
    Last edited by stelio; 18th May 2008 at 19:35. Reason: reformatted to look better

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to display PDF in Qt?

    You could use QDesktopServices::openUrl() to show it in the default application.
    J-P Nurmi

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display PDF in Qt?

    The only way is to use a 3rd party library to give you page rasters.
    For example xpdf does that very well. You can build pixmaps from those rasters and display them in a widget.
    You will have a bit of overhead from converting xpdf pixel data to QPixmap, but that will depend on the actual page size.

  4. #4
    Join Date
    Aug 2006
    Posts
    250
    Thanks
    19
    Thanked 49 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to display PDF in Qt?

    If you need embedded pdf you probably want to look at either mupdf or poppler. They're both used in the free PDF viewer Sumatra http://blog.kowalczyk.info/software/...f/develop.html which is opensource and therefore is probably a good example for how to use them (at least on windows.)

  5. #5
    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: How to display PDF in Qt?

    Quote Originally Posted by stelio View Post
    Hi!

    is there any ways to display PDF in an Qt application?

    Thanks!
    stelio
    QT3 DTP Scribus programm import pdf as image .. need only ghostscript an run on mac,win, linux

    this is part of code from QT 4.4 MiniScribus
    http://www.qt-apps.org/content/show....?content=80234

    Qt Code:
    1. extern inline QPixmap LoadPDF(QString fn, int Page, int w )
    2. {
    3. QString tmp, cmd1, cmd2;
    4. const QString pdfFile = PathConvert(fn);
    5. const QString tmpFile = PathConvert(QDir::homePath()+"/sctodaytmps.png");
    6. const QString qttmpFile = QDir::homePath()+"/sctodaytmps.png";
    7. QPixmap pm;
    8. tmp.setNum(Page);
    9. int ret = -1;
    10. tmp.setNum(Page);
    11. args.append("-sDEVICE=png16m");
    12. args.append("-r72");
    13. args.append("-dGraphicsAlphaBits=4");
    14. args.append("-o");
    15. args.append(tmpFile);
    16. args.append("-dFirstPage="+tmp);
    17. args.append("-dLastPage="+tmp);
    18. args.append(pdfFile);
    19. ret = callGS(args);
    20. ////////qDebug() << "### ret " << ret;
    21. if (ret == 0)
    22. {
    23. QPixmap tmpimage(qttmpFile);
    24. QPixmap penna = tmpimage.scaledToWidth(w);
    25. tmpimage.detach();
    26. QFile lastaction(qttmpFile);
    27. lastaction.remove();
    28. p.begin(&penna);
    29. p.setBrush(Qt::NoBrush);
    30. p.setPen(QPen(QBrush(Qt::black),2,Qt::SolidLine));
    31. p.drawRect(0, 0, penna.width(), penna.height());
    32. p.end();
    33. return penna;
    34. }
    35. return pm;
    36. }
    37.  
    38. extern inline int callGS( const QStringList args )
    39. {
    40. const QString startnow = QDir::currentPath();
    41. const QString GhostScriptPath = getGSDefaultExeName();
    42. QDir::setCurrent(_GSCACHE_);
    43. QString cmd1 = GhostScriptPath + " ";
    44. cmd1 += args.join(" ");
    45. int fax = -1;
    46. #if defined Q_WS_MAC
    47. fax = system(cmd1.toLocal8Bit());
    48. QDir::setCurrent(startnow);
    49. return fax;
    50. #endif
    51. QProcess *process = new QProcess(NULL);
    52. process->setReadChannelMode(QProcess::MergedChannels);
    53. process->start( GhostScriptPath , args , QIODevice::ReadOnly );
    54. if (!process->waitForFinished()) {
    55. fax = -1;
    56. } else {
    57. QString ghostcomment = process->readAll().trimmed();
    58. //////qDebug() << "ghostcomment-> " << ghostcomment;
    59. fax = 0;
    60. }
    61.  
    62. QDir::setCurrent(startnow);
    63. return fax;
    64. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2008
    Posts
    2
    Platforms
    MacOS X Windows

    Default Re: How to display PDF in Qt?

    Thanks for all those answers!

    I will investigate xpdf, mupdf and poppler to better understand.

    MiniScribus is very interesting, but as I understand, it convert it to a png. I need to provide graphical zoom feature in the PDF without loosing the vectorial display.

    Is converting PDF to SVG an option?

    stelio

  7. #7
    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: How to display PDF in Qt?

    The best option is a poppler fork

    svn co http://poppler-kjk.googlecode.com/svn/trunk/ Fpoppler

    it can run on linux mac e win
    on window you must install automake autoconf
    like this http://www.venge.net/mtn-wiki/BuildingOnWindows

    info on http://blog.kowalczyk.info/software/sumatrapdf/ &&
    Sumatra PDF source on ...
    http://blog.kowalczyk.info/software/.../download.html

Similar Threads

  1. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  2. Custom widget
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 28th January 2008, 14:06
  3. Replies: 3
    Last Post: 17th October 2007, 12:52
  4. transparent background of the main widget
    By nagpalma in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2007, 17:52
  5. [Qt 4.1.0] Split a widget on demand
    By Townk in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2006, 14:16

Tags for this Thread

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.