diff --git a/src/image/imagearguments.cc b/src/image/imagearguments.cc index 41e05e362..f4392db3b 100644 --- a/src/image/imagearguments.cc +++ b/src/image/imagearguments.cc @@ -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")); diff --git a/src/image/imagedocparts.cc b/src/image/imagedocparts.cc index 12727460c..f8e754f89 100644 --- a/src/image/imagedocparts.cc +++ b/src/image/imagedocparts.cc @@ -118,5 +118,9 @@ void ImageCommandLineParser::outputExamples(Outputter * o) const { o->verbatim("wkhtmltoimage http://www.google.com google.png\n"); o->paragraph("To convert a local HTML file to PNG:"); o->verbatim("wkhtmltoimage my.html my.png\n"); + o->paragraph("To crop on a specific HTML element:"); + o->verbatim("wkhtmltoimage --selector '#hplogo' https://google.com google.png\n"); + o->paragraph("To crop a part of the element by an offset (use a negative offset for a margin):"); + o->verbatim("wkhtmltoimage --selector '#hplogo' --crop-x 70 https://google.com google.png\n"); o->endSection(); } diff --git a/src/lib/imageconverter.cc b/src/lib/imageconverter.cc index 09e3b2397..63e70b816 100644 --- a/src/lib/imageconverter.cc +++ b/src/lib/imageconverter.cc @@ -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(); diff --git a/src/lib/imagesettings.cc b/src/lib/imagesettings.cc index a120b948c..455ace9b5 100644 --- a/src/lib/imagesettings.cc +++ b/src/lib/imagesettings.cc @@ -48,17 +48,20 @@ struct DLL_LOCAL ReflectImpl: public ReflectClass { WKHTMLTOPDF_REFLECT(in); WKHTMLTOPDF_REFLECT(out); WKHTMLTOPDF_REFLECT(fmt); + WKHTMLTOPDF_REFLECT(selector); WKHTMLTOPDF_REFLECT(quality); WKHTMLTOPDF_REFLECT(loadGlobal); WKHTMLTOPDF_REFLECT(loadPage); } }; +const int CropSettings::DEFAULT = std::numeric_limits::min(); + CropSettings::CropSettings(): - left(-1), - top(-1), - width(-1), - height(-1) {} + left(DEFAULT), + top(DEFAULT), + width(DEFAULT), + height(DEFAULT) {} ImageGlobal::ImageGlobal(): logLevel(Info), @@ -67,6 +70,7 @@ ImageGlobal::ImageGlobal(): in(""), out(""), fmt(""), + selector(""), screenWidth(1024), screenHeight(0), quality(94), diff --git a/src/lib/imagesettings.hh b/src/lib/imagesettings.hh index be3ccc254..c8092b511 100644 --- a/src/lib/imagesettings.hh +++ b/src/lib/imagesettings.hh @@ -25,6 +25,7 @@ #include #include #include +#include #include namespace wkhtmltopdf { @@ -41,6 +42,8 @@ struct DLL_PUBLIC CropSettings { int width; //! Cropping height/h dime int height; + + static const int DEFAULT; }; /*! \brief Class holding all user settings. @@ -74,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;