php-phantomjs icon indicating copy to clipboard operation
php-phantomjs copied to clipboard

Output file is not writeable by PhantomJs

Open Gummet opened this issue 7 years ago • 5 comments

I'm getting the following when trying to generate a screenshot. The script runs as bitnami, also bitnami is the owner for everything under /htdocs. I've even tested setting all files/folders to 777 just to give it a quick test and see if the file will generate but no luck.

Could I be doing something wrong here that I am not relising?

[Thu Jul 26 16:51:50.456619 2018] [proxy_fcgi:error] [pid 1808:tid 140458139199232] [client 141.101.98.151:22227] AH01071: Got error 'PHP message: PHP Fatal error:  Uncaught JonnyW\\PhantomJs\\Exception\\NotWritableException: Output file is not writeable by PhantomJs: /opt/bitnami/apache2/htdocs/test.jpg in /opt/bitnami/apache2/htdocs/vendor/jonnyw/php-phantomjs/src/JonnyW/PhantomJs/Http/CaptureRequest.php:199

Stack trace:
#0 /opt/bitnami/apache2/htdocs/myClasses/MakeChart.php(35): JonnyW\\PhantomJs\\Http\\CaptureRequest->setOutputFile('/opt/bitnami/ap...')
#1 /opt/bitnami/apache2/htdocs/Commands/ChartCommand.php(23): MakeChart->takeScreenshot()
#2 /opt/bitnami/apache2/htdocs/vendor/longman/telegram-bot/src/Commands/Command.php(173): Longman\\TelegramBot\\Commands\\UserCommands\\ChartCommand->execute()
#3 /opt/bitnami/apache2/htdocs/vendor/longman/telegram-bot/src/Telegram.php(537): Longman\\TelegramBot\\Commands\\Command->preExecute()
#4 /opt/bitnami/apache2/htdocs/vendor/longman/telegram-bot/src/Telegram.php(505): Longman\\TelegramBot\\Telegram->executeCommand('chart')
#5 /opt/bitnami/apache2/htdocs/vendor/longman/telegram-bot/src/Telegram.php(423):...'

Here is the class/function that is called to generate the screenshot if it helps at all:

<?php

require __DIR__ . '/../vendor/autoload.php';
use JonnyW\PhantomJs\Client;

class MakeChart
{
    public function takeScreenshot()
    {
		$client = Client::getInstance();

		// isLazy() is useful when we want to wait for all resources on the page to load.
		$client->isLazy();

		// Some options
		$client->getEngine()->addOption('--load-images=true');
		$client->getEngine()->addOption('--ignore-ssl-errors=true');
		
		$vWidth  = 2560;
		$vHeight = 1440;
		
		$width  = 1910;
		$height = 670;
		$top    = 95;
		$left   = 15;
		
		// Here specify the page to take the screenshot
		$request  = $client->getMessageFactory()->createCaptureRequest('https://google.com');
		
		$request->setCaptureDimensions($width, $height, $top, $left);
		$request->setViewportSize($vWidth, $vHeight);
		$request->setBodyStyles(['backgroundColor' => '#ffffff']);
		
		$file = getcwd() . '/test.jpg';
		$request->setOutputFile($file);

		$response = $client->getMessageFactory()->createResponse();
		
		// Send the request
		$client->send($request, $response);
    }
}

Gummet avatar Jul 26 '18 17:07 Gummet

Getting the same issue as well. I'm not sure how to fix it currently.

cdknight avatar Aug 05 '18 01:08 cdknight

/vendor/jonnyw/php-phantomjs/src/JonnyW/PhantomJs/Http/CaptureRequest.php

/** * Set file to save output. * * @param string $file * * @throws \JonnyW\PhantomJs\Exception\NotWritableException * * @return \JonnyW\PhantomJs\Http\CaptureRequest */ public function setOutputFile($file) { // if (!is_writable(dirname($file))) { // throw new NotWritableException(sprintf('Output file is not writeable by PhantomJs: %s', $file)); // }

    $this->outputFile = $file;

    return $this;
}

nicklike avatar Aug 17 '18 10:08 nicklike

Same problem. +1

    private function screenshot($url, $output, $type = "pc"){
        $client = Client::getInstance();
        $client->getEngine()->setPath(APPPATH."../../bin/phantomjs.exe");
        $client->getEngine()->addOption('--ignore-ssl-errors=true');
        $client->isLazy();
        $client->getEngine()->debug(true);

        $width  = 1920;
        $height = 1080;
        $top    = 0;
        $left   = 0;

        switch($type){
            case "mobile":
                $width  = 375;
                $height = 667;
            break;

            default:
                $width  = 1920;
                $height = 1080;
            break;
        }

        $request  = $client->getMessageFactory()->createCaptureRequest($url);
        $request->setOutputFile(APPPATH.'tmp'.DIRECTORY_SEPARATOR.$output);
        $request->setTimeout(5000);
        $request->setViewportSize($width, $height);
        $request->setCaptureDimensions($width, $height, $top, $left);

        $response = $client->getMessageFactory()->createResponse();

        $client->send($request, $response)->getContent();
    }

50l3r avatar Dec 26 '18 17:12 50l3r

I know this issue is an old one but I solved the problem by first creating an empty file named file.pdf(in my own case) and it worked fine.

Koredeoluwafemi avatar Feb 05 '19 12:02 Koredeoluwafemi

I solved disable the sellinux on Centos 7.

pablosouza10 avatar Dec 04 '19 12:12 pablosouza10