PDA

View Full Version : How to display PDF in Qt?



stelio
18th May 2008, 19:33
Hi!

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

Thanks!
stelio

jpn
18th May 2008, 19:57
You could use QDesktopServices::openUrl() to show it in the default application.

marcel
18th May 2008, 20:20
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.

pherthyl
19th May 2008, 00:57
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/sumatrapdf/develop.html which is opensource and therefore is probably a good example for how to use them (at least on windows.)

patrik08
19th May 2008, 06:52
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.php/GraphicsViewEdit+Layer?content=80234



extern inline QPixmap LoadPDF(QString fn, int Page, int w )
{
QString tmp, cmd1, cmd2;
const QString pdfFile = PathConvert(fn);
const QString tmpFile = PathConvert(QDir::homePath()+"/sctodaytmps.png");
const QString qttmpFile = QDir::homePath()+"/sctodaytmps.png";
QPixmap pm;
tmp.setNum(Page);
int ret = -1;
tmp.setNum(Page);
QStringList args;
args.append("-sDEVICE=png16m");
args.append("-r72");
args.append("-dGraphicsAlphaBits=4");
args.append("-o");
args.append(tmpFile);
args.append("-dFirstPage="+tmp);
args.append("-dLastPage="+tmp);
args.append(pdfFile);
ret = callGS(args);
////////qDebug() << "### ret " << ret;
if (ret == 0)
{
QPixmap tmpimage(qttmpFile);
QPixmap penna = tmpimage.scaledToWidth(w);
tmpimage.detach();
QFile lastaction(qttmpFile);
lastaction.remove();
QPainter p;
p.begin(&penna);
p.setBrush(Qt::NoBrush);
p.setPen(QPen(QBrush(Qt::black),2,Qt::SolidLine));
p.drawRect(0, 0, penna.width(), penna.height());
p.end();
return penna;
}
return pm;
}

extern inline int callGS( const QStringList args )
{
const QString startnow = QDir::currentPath();
const QString GhostScriptPath = getGSDefaultExeName();
QDir::setCurrent(_GSCACHE_);
QString cmd1 = GhostScriptPath + " ";
cmd1 += args.join(" ");
int fax = -1;
#if defined Q_WS_MAC
fax = system(cmd1.toLocal8Bit());
QDir::setCurrent(startnow);
return fax;
#endif
QProcess *process = new QProcess(NULL);
process->setReadChannelMode(QProcess::MergedChannels);
process->start( GhostScriptPath , args , QIODevice::ReadOnly );
if (!process->waitForFinished()) {
fax = -1;
} else {
QString ghostcomment = process->readAll().trimmed();
//////qDebug() << "ghostcomment-> " << ghostcomment;
fax = 0;
}

QDir::setCurrent(startnow);
return fax;
}

stelio
19th May 2008, 09:15
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

patrik08
19th May 2008, 09:44
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/sumatrapdf/download.html