Backport from 4.2 of commit 897000 to fix Print to File for PDF, DVI and DJVU.

svn path=/branches/KDE/4.1/kdegraphics/okular/; revision=897005
remotes/origin/KDE/4.1
John Layt 18 years ago
parent 3f694b03d8
commit dd7005833c
  1. 87
      core/fileprinter.cpp
  2. 12
      core/fileprinter.h
  3. 3
      generators/djvu/generator_djvu.cpp
  4. 2
      generators/dvi/generator_dvi.cpp
  5. 2
      generators/poppler/generator_pdf.cpp

@ -15,9 +15,9 @@
#include <QtGui/QPrinter>
#include <QPrintEngine>
#include <QStringList>
#include <QFile>
#include <QSize>
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtGui/QLabel>
#include <QtGui/QShowEvent>
#include <QtNetwork/QTcpSocket>
@ -49,22 +49,6 @@ int FilePrinter::printFiles( QPrinter &printer, const QStringList &fileList, Fil
int FilePrinter::doPrintFiles( QPrinter &printer, QStringList fileList, FileDeletePolicy fileDeletePolicy,
PageSelectPolicy pageSelectPolicy, const QString &pageRange )
{
//Decide what executable to use to print with, need the CUPS version of lpr if available
//Some distros name the CUPS version of lpr as lpr-cups or lpr.cups so try those first
//before default to lpr, or failing that to lp
QString exe;
if ( !KStandardDirs::findExe("lpr-cups").isEmpty() ) {
exe = "lpr-cups";
} else if ( !KStandardDirs::findExe("lpr.cups").isEmpty() ) {
exe = "lpr.cups";
} else if ( !KStandardDirs::findExe("lpr").isEmpty() ) {
exe = "lpr";
} else if ( !KStandardDirs::findExe("lp").isEmpty() ) {
exe = "lp";
} else {
return -9;
}
if ( fileList.size() < 1 ) {
return -8;
@ -80,6 +64,10 @@ int FilePrinter::doPrintFiles( QPrinter &printer, QStringList fileList, FileDele
return -6;
}
QString exe;
QStringList argList;
int ret;
// Print to File if a filename set, assumes there must be only 1 file
if ( !printer.outputFileName().isEmpty() ) {
@ -87,33 +75,56 @@ int FilePrinter::doPrintFiles( QPrinter &printer, QStringList fileList, FileDele
QFile::remove( printer.outputFileName() );
}
int res = QFile::copy( fileList[0], printer.outputFileName() );
QFileInfo inputFileInfo = QFileInfo( fileList[0] );
QFileInfo outputFileInfo = QFileInfo( printer.outputFileName() );
if ( inputFileInfo.suffix() == outputFileInfo.suffix() ) {
int res = QFile::copy( fileList[0], printer.outputFileName() );
if ( res ) ret = 0;
} else if ( inputFileInfo.suffix() == "ps" && outputFileInfo.suffix() == "pdf" && ps2pdfAvailable() ) {
exe = "ps2pdf";
argList << fileList[0] << printer.outputFileName();
kDebug(OkularDebug) << "Executing" << exe << "with arguments" << argList;
ret = KProcess::execute( exe, argList );
} else if ( inputFileInfo.suffix() == "pdf" && outputFileInfo.suffix() == "ps" && pdf2psAvailable() ) {
exe = "pdf2ps";
argList << fileList[0] << printer.outputFileName();
kDebug(OkularDebug) << "Executing" << exe << "with arguments" << argList;
ret = KProcess::execute( exe, argList );
} else {
ret = -5;
}
if ( fileDeletePolicy == FilePrinter::SystemDeletesFiles ) {
QFile::remove( fileList[0] );
}
if ( res ) return 0;
}
bool useCupsOptions = cupsAvailable();
QStringList argList = printArguments( printer, fileDeletePolicy, pageSelectPolicy, useCupsOptions, pageRange, exe )
<< fileList;
kDebug(OkularDebug) << "Executing" << exe << "with arguments" << argList;
} else { // Print to a printer via lpr command
int ret = KProcess::execute( exe, argList );
//Decide what executable to use to print with, need the CUPS version of lpr if available
//Some distros name the CUPS version of lpr as lpr-cups or lpr.cups so try those first
//before default to lpr, or failing that to lp
// If we used the Cups options and the command failed, try again without them in case Cups lpr/lp not installed.
// I'm not convinced this will work, I don't think KProcess returns the result of the command, but rather
// that it was able to be executed?
if ( useCupsOptions && ret < 0 ) {
if ( !KStandardDirs::findExe("lpr-cups").isEmpty() ) {
exe = "lpr-cups";
} else if ( !KStandardDirs::findExe("lpr.cups").isEmpty() ) {
exe = "lpr.cups";
} else if ( !KStandardDirs::findExe("lpr").isEmpty() ) {
exe = "lpr";
} else if ( !KStandardDirs::findExe("lp").isEmpty() ) {
exe = "lp";
} else {
return -9;
}
argList = printArguments( printer, fileDeletePolicy, pageSelectPolicy, useCupsOptions, pageRange, exe )
<< fileList;
kDebug(OkularDebug) << "Retrying" << exe << "without CUPS arguments" << argList;
bool useCupsOptions = cupsAvailable();
argList = printArguments( printer, fileDeletePolicy, pageSelectPolicy,
useCupsOptions, pageRange, exe ) << fileList;
kDebug(OkularDebug) << "Executing" << exe << "with arguments" << argList;
ret = KProcess::execute( exe, argList );
}
return ret;
@ -188,6 +199,16 @@ QString FilePrinter::pageListToPageRange( const QList<int> &pageList )
return pageRange;
}
bool FilePrinter::ps2pdfAvailable()
{
return ( !KStandardDirs::findExe("ps2pdf").isEmpty() );
}
bool FilePrinter::pdf2psAvailable()
{
return ( !KStandardDirs::findExe("pdf2ps").isEmpty() );
}
bool FilePrinter::cupsAvailable()
{
FilePrinter fp;

@ -120,6 +120,18 @@ public:
*/
static QString pageListToPageRange( const QList<int> &pageList );
/** Return if Ghostscript ps2pdf is available on this system
*
* @returns Returns true if Ghostscript ps2pdf available
*/
static bool ps2pdfAvailable();
/** Return if Ghostscript pdf2ps is available on this system
*
* @returns Returns true if Ghostscript pdf2ps available
*/
static bool pdf2psAvailable();
/** Return if CUPS Print System is available on this system
*
* @returns Returns true if CUPS available

@ -83,7 +83,8 @@ DjVuGenerator::DjVuGenerator( QObject *parent, const QVariantList &args )
setFeature( TextExtraction );
setFeature( Threaded );
setFeature( PrintPostscript );
if ( Okular::FilePrinter::ps2pdfAvailable() )
setFeature( PrintToFile );
m_djvu = new KDjVu();
}

@ -57,6 +57,8 @@ DviGenerator::DviGenerator( QObject *parent, const QVariantList &args ) : Okular
{
setFeature( TextExtraction );
setFeature( PrintPostscript );
if ( Okular::FilePrinter::ps2pdfAvailable() )
setFeature( PrintToFile );
}
bool DviGenerator::loadDocument( const QString & fileName, QVector< Okular::Page * > &pagesVector )

@ -260,6 +260,8 @@ PDFGenerator::PDFGenerator( QObject *parent, const QVariantList &args )
setFeature( TextExtraction );
setFeature( FontInfo );
setFeature( PrintPostscript );
if ( Okular::FilePrinter::ps2pdfAvailable() )
setFeature( PrintToFile );
setFeature( ReadRawData );
// generate the pixmapGeneratorThread
generatorThread = new PDFPixmapGeneratorThread( this );

Loading…
Cancel
Save