Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in command line options to set the page size & margin, and to control remote web requests #5

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 94 additions & 6 deletions CutyCapt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include <QApplication>
#include <QtWebKit>
#include <QtGui>
#ifndef QT_NO_SVG
#include <QSvgGenerator>
#endif

#if QT_VERSION < 0x050000
#include <QPrinter>
Expand All @@ -46,7 +48,9 @@
Q_IMPORT_PLUGIN(qjpeg)
Q_IMPORT_PLUGIN(qgif)
Q_IMPORT_PLUGIN(qtiff)
#ifndef QT_NO_SVG
Q_IMPORT_PLUGIN(qsvg)
#endif
Q_IMPORT_PLUGIN(qmng)
Q_IMPORT_PLUGIN(qico)
#endif
Expand All @@ -56,7 +60,9 @@ static struct _CutyExtMap {
const char* extension;
const char* identifier;
} const CutyExtMap[] = {
#ifndef QT_NO_SVG
{ CutyCapt::SvgFormat, ".svg", "svg" },
#endif
{ CutyCapt::PdfFormat, ".pdf", "pdf" },
{ CutyCapt::PsFormat, ".ps", "ps" },
{ CutyCapt::InnerTextFormat, ".txt", "itext" },
Expand All @@ -76,6 +82,24 @@ static struct _CutyExtMap {
{ CutyCapt::OtherFormat, "", "" }
};

CutyNetworkAccessManager::CutyNetworkAccessManager() {
mAllowRemoteResources = true;
}

void CutyNetworkAccessManager::setAllowRemoteResources(bool allowRemoteResources) {
mAllowRemoteResources = allowRemoteResources;
}

QNetworkReply * CutyNetworkAccessManager::createRequest(Operation op, const QNetworkRequest & req, QIODevice * outgoingData) {
if (req.url().scheme() != "file" && req.url().scheme() != "data" && !mAllowRemoteResources) {
QNetworkRequest adjusted = req;
adjusted.setUrl(QUrl("data:,"));
return QNetworkAccessManager::createRequest(op, adjusted, outgoingData);
}

return QNetworkAccessManager::createRequest(op, req, outgoingData);
}

QString
CutyPage::chooseFile(QWebFrame* /*frame*/, const QString& /*suggestedFile*/) {
return QString::null;
Expand Down Expand Up @@ -162,12 +186,15 @@ CutyPage::setAttribute(QWebSettings::WebAttribute option,

CutyCapt::CutyCapt(CutyPage* page, const QString& output, int delay, OutputFormat format,
const QString& scriptProp, const QString& scriptCode, bool insecure,
bool smooth) {
bool smooth, int pageWidth, int pageHeight, QRectF margins) {
mPage = page;
mOutput = output;
mDelay = delay;
mInsecure = insecure;
mSmooth = smooth;
mPageWidth = pageWidth;
mPageHeight = pageHeight;
mMargins = margins;
mSawInitialLayout = false;
mSawDocumentComplete = false;
mFormat = format;
Expand Down Expand Up @@ -271,6 +298,7 @@ CutyCapt::saveSnapshot() {
mPage->setViewportSize( mainFrame->contentsSize() );

switch (mFormat) {
#ifndef QT_NO_SVG
case SvgFormat: {
QSvgGenerator svg;
svg.setFileName(mOutput);
Expand All @@ -280,11 +308,25 @@ CutyCapt::saveSnapshot() {
painter.end();
break;
}
#endif
case PdfFormat:
case PsFormat: {
QPrinter printer;
printer.setPageSize(QPrinter::A4);
if (mPageWidth != 0 && mPageHeight != 0) {
printer.setPaperSize(QSizeF(mPageWidth, mPageHeight), QPrinter::Point);
} else {
printer.setPageSize(QPrinter::A4);
}
if (mMargins.left() >= 0 && mMargins.top() >= 0 && mMargins.right() >= 0 && mMargins.bottom() >= 0) {
printer.setPageMargins(mMargins.left(), mMargins.top(), mMargins.right(), mMargins.bottom(), QPrinter::Point);
}
printer.setOutputFileName(mOutput);
#ifdef Q_WS_MACX
if (mFormat == PdfFormat) {
// Set the output format to native on Mac OSX otherwise the PDF text isn't selectable
printer.setOutputFormat(QPrinter::NativeFormat);
}
#endif
// TODO: change quality here?
mainFrame->print(&printer);
break;
Expand Down Expand Up @@ -359,8 +401,15 @@ CaptHelp(void) {
" --plugins=<on|off> Plugin execution (default: unknown) \n"
" --private-browsing=<on|off> Private browsing (default: unknown) \n"
" --auto-load-images=<on|off> Automatic image loading (default: on) \n"
" --allow-remote-resources=<on|off> Allow loading remote resources (default: on)n"
" --js-can-open-windows=<on|off> Script can open windows? (default: unknown) \n"
" --js-can-access-clipboard=<on|off> Script clipboard privs (default: unknown)\n"
" --page-width=<pts> Sets the page width in points (default: A4 width)\n"
" --page-height=<pts> Sets the page height in points (default: A4 height)\n"
" --margin-left=<pts> Sets the left margin in points (default: unknown)\n"
" --margin-top=<pts> Sets the top margin in points (default: unknown)\n"
" --margin-bottom=<pts Sets the right margin in points (default: unknown)\n"
" --margin-right=<pts> Sets the bottom margin in points (default: unknown)\n"
#if QT_VERSION >= 0x040500
" --print-backgrounds=<on|off> Backgrounds in PDF/PS output (default: off) \n"
" --zoom-factor=<float> Page zoom factor (default: no zooming) \n"
Expand Down Expand Up @@ -407,6 +456,12 @@ main(int argc, char *argv[]) {
int argMaxWait = 90000;
int argVerbosity = 0;
int argSmooth = 0;
int argPageWidth = 0;
int argPageHeight = 0;
int argMarginLeft = -1;
int argMarginTop = -1;
int argMarginRight = -1;
int argMarginBottom = -1;

const char* argUrl = NULL;
const char* argUserStyle = NULL;
Expand All @@ -421,12 +476,14 @@ main(int argc, char *argv[]) {

QApplication app(argc, argv, true);
CutyPage page;

QNetworkAccessManager::Operation method =
QNetworkAccessManager::GetOperation;
QByteArray body;
QNetworkRequest req;
QNetworkAccessManager manager;
CutyNetworkAccessManager manager;

page.setNetworkAccessManager(&manager);

// Parse command line parameters
for (int ax = 1; ax < argc; ++ax) {
Expand Down Expand Up @@ -495,6 +552,30 @@ main(int argc, char *argv[]) {
// TODO: see above
argMaxWait = (unsigned int)atoi(value);

} else if (strncmp("--page-width", s, nlen) == 0) {
// TODO: see above
argPageWidth = (unsigned int)atoi(value);

} else if (strncmp("--page-height", s, nlen) == 0) {
// TODO: see above
argPageHeight = (unsigned int)atoi(value);

} else if (strncmp("--margin-left", s, nlen) == 0) {
// TODO: see above
argMarginLeft = (unsigned int)atoi(value);

} else if (strncmp("--margin-top", s, nlen) == 0) {
// TODO: see above
argMarginTop = (unsigned int)atoi(value);

} else if (strncmp("--margin-right", s, nlen) == 0) {
// TODO: see above
argMarginRight = (unsigned int)atoi(value);

} else if (strncmp("--margin-bottom", s, nlen) == 0) {
// TODO: see above
argMarginBottom = (unsigned int)atoi(value);

} else if (strncmp("--out", s, nlen) == 0) {
argOut = value;

Expand All @@ -519,6 +600,13 @@ main(int argc, char *argv[]) {
} else if (strncmp("--auto-load-images", s, nlen) == 0) {
page.setAttribute(QWebSettings::AutoLoadImages, value);

} else if (strncmp("--allow-remote-resources", s, nlen) == 0) {
if (strcmp("on", value) == 0) {
manager.setAllowRemoteResources(true);
} else if (strcmp("off", value) == 0) {
manager.setAllowRemoteResources(false);
}

} else if (strncmp("--javascript", s, nlen) == 0) {
page.setAttribute(QWebSettings::JavascriptEnabled, value);

Expand Down Expand Up @@ -558,7 +646,6 @@ main(int argc, char *argv[]) {
QNetworkProxy proxy = QNetworkProxy(QNetworkProxy::HttpProxy,
p.host(), p.port(80), p.userName(), p.password());
manager.setProxy(proxy);
page.setNetworkAccessManager(&manager);
#endif

#if CUTYCAPT_SCRIPT
Expand Down Expand Up @@ -651,7 +738,8 @@ main(int argc, char *argv[]) {
}

CutyCapt main(&page, argOut, argDelay, format, scriptProp, scriptCode,
!!argInsecure, !!argSmooth);
!!argInsecure, !!argSmooth, argPageWidth, argPageHeight,
QRectF(argMarginLeft, argMarginTop, argMarginRight, argMarginBottom));

app.connect(&page,
SIGNAL(loadFinished(bool)),
Expand Down
21 changes: 20 additions & 1 deletion CutyCapt.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
#include <QtWebKit>
#include <QRectF>

#if QT_VERSION >= 0x050000
#include <QtWebKitWidgets>
#endif

class CutyNetworkAccessManager : public QNetworkAccessManager {
Q_OBJECT

public:
CutyNetworkAccessManager();
void setAllowRemoteResources(bool allowRemoteResources);
QNetworkReply * createRequest(Operation op, const QNetworkRequest & req, QIODevice * outgoingData = 0);

protected:
bool mAllowRemoteResources;
};

class CutyCapt;
class CutyPage : public QWebPage {
Q_OBJECT
Expand Down Expand Up @@ -46,7 +59,10 @@ class CutyCapt : public QObject {
const QString& scriptProp,
const QString& scriptCode,
bool insecure,
bool smooth);
bool smooth,
int pageWidth,
int pageHeight,
QRectF margins);

private slots:
void DocumentComplete(bool ok);
Expand All @@ -72,4 +88,7 @@ private slots:
QString mScriptCode;
bool mInsecure;
bool mSmooth;
int mPageWidth;
int mPageHeight;
QRectF mMargins;
};
5 changes: 5 additions & 0 deletions CutyCapt.pro
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ contains(CONFIG, static): {
DEFINES += STATIC_PLUGINS
}

mac {
QMAKE_CXXFLAGS += -fvisibility=hidden
QMAKE_LFLAGS += '-sectcreate __TEXT __info_plist Info.plist'
CONFIG -= app_bundle
}
12 changes: 12 additions & 0 deletions Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd">
<plist version="0.9">
<dict>
<key>CFBundleExecutable</key>
<string>CutyCapt</string>
<key>CFBundleIdentifier</key>
<string>net.sf.cutycapt</string>
<key>LSUIElement</key>
<string>1</string>
</dict>
</plist>