public function generateScreenshotAction()
{
$success = false;
if ($this->getParam("id")) {
$doc = Document::getById($this->getParam("id"));
$url = Tool::getHostUrl() . $doc->getRealFullPath();
$config = \Pimcore\Config::getSystemConfig();
if ($config->general->http_auth) {
$username = $config->general->http_auth->username;
$password = $config->general->http_auth->password;
if ($username && $password) {
$url = str_replace("://", "://" . $username . ":" . $password . "@", $url);
}
}
$tmpFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . "/screenshot_tmp_" . $doc->getId() . ".png";
$file = PIMCORE_TEMPORARY_DIRECTORY . "/document-page-previews/document-page-screenshot-" . $doc->getId() . ".jpg";
$dir = dirname($file);
if (!is_dir($dir)) {
File::mkdir($dir);
}
try {
if (\Pimcore\Image\HtmlToImage::convert($url, $tmpFile)) {
$im = \Pimcore\Image::getInstance();
$im->load($tmpFile);
$im->scaleByWidth(400);
$im->save($file, "jpeg", 85);
unlink($tmpFile);
$success = true;
}
} catch (\Exception $e) {
Logger::error($e);
}
}
$this->_helper->json(["success" => $success]);
}