Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
add css selector option
Browse files Browse the repository at this point in the history
  • Loading branch information
PaperBag42 committed Apr 11, 2019
1 parent 58b0963 commit 6c53981
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/image/imagearguments.cc
Expand Up @@ -38,6 +38,7 @@ ImageCommandLineParser::ImageCommandLineParser(wkhtmltopdf::settings::ImageGloba
// addarg("scale-w",0,"Set width for resizing", new IntSetter(s.scale.width,"int"));
// addarg("scale-h",0,"Set height for resizing", new IntSetter(s.scale.height,"int"));

addarg("selector", 0, "Set a CSS2 selector that points to an element to crop on", new QStrSetter(s.selector, "selector"));
addarg("crop-x",0,"Set x coordinate for cropping", new IntSetter(s.crop.left,"int"));
addarg("crop-y",0,"Set y coordinate for cropping", new IntSetter(s.crop.top,"int"));
addarg("crop-w",0,"Set width for cropping", new IntSetter(s.crop.width,"int"));
Expand Down
28 changes: 22 additions & 6 deletions src/lib/imageconverter.cc
Expand Up @@ -166,12 +166,28 @@ void ImageConverterPrivate::pagesLoaded(bool ok) {
fail();
}

if (settings.crop.left < 0) settings.crop.left = 0;
if (settings.crop.top < 0) settings.crop.top = 0;
if (settings.crop.width < 0) settings.crop.width = 1000000;
if (settings.crop.height < 0) settings.crop.height = 1000000;
QRect rect = QRect(QPoint(0,0), loaderObject->page.viewportSize()).intersected(
QRect(settings.crop.left,settings.crop.top,settings.crop.width,settings.crop.height));
QRect rect;
if (settings.selector != "") {
QWebElement elem = frame->findFirstElement(settings.selector);
if (elem.isNull()) {
emit out.error("No element matched the selector");
fail();
return;
}
rect = elem.geometry().adjusted(
settings.crop.left == settings::CropSettings::DEFAULT ? 0 : settings.crop.left,
settings.crop.top == settings::CropSettings::DEFAULT ? 0 : settings.crop.top,
settings.crop.width == settings::CropSettings::DEFAULT ? 0 : settings.crop.width,
settings.crop.height == settings::CropSettings::DEFAULT ? 0 : settings.crop.height);

} else {
if (settings.crop.left < 0) settings.crop.left = 0;
if (settings.crop.top < 0) settings.crop.top = 0;
if (settings.crop.width < 0) settings.crop.width = 1000000;
if (settings.crop.height < 0) settings.crop.height = 1000000;
rect = QRect(QPoint(0,0), loaderObject->page.viewportSize()).intersected(
QRect(settings.crop.left,settings.crop.top,settings.crop.width,settings.crop.height));
}
if (rect.width() == 0 || rect.height() == 0) {
emit out.error("Will not output an empty image");
fail();
Expand Down
2 changes: 2 additions & 0 deletions src/lib/imagesettings.cc
Expand Up @@ -48,6 +48,7 @@ struct DLL_LOCAL ReflectImpl<ImageGlobal>: public ReflectClass {
WKHTMLTOPDF_REFLECT(in);
WKHTMLTOPDF_REFLECT(out);
WKHTMLTOPDF_REFLECT(fmt);
WKHTMLTOPDF_REFLECT(selector);
WKHTMLTOPDF_REFLECT(quality);
WKHTMLTOPDF_REFLECT(loadGlobal);
WKHTMLTOPDF_REFLECT(loadPage);
Expand All @@ -69,6 +70,7 @@ ImageGlobal::ImageGlobal():
in(""),
out(""),
fmt(""),
selector(""),
screenWidth(1024),
screenHeight(0),
quality(94),
Expand Down
3 changes: 3 additions & 0 deletions src/lib/imagesettings.hh
Expand Up @@ -77,6 +77,9 @@ struct DLL_PUBLIC ImageGlobal {
QString out;
//! The output format
QString fmt;

//! Set the CSS selector of an element to crop on
QString selector;

//! Set the screen width
int screenWidth;
Expand Down

0 comments on commit 6c53981

Please sign in to comment.