jakubkulhan / chrome-devtools-protocol

Chrome Devtools Protocol client for PHP
MIT License
170 stars 49 forks source link

Fail on intialization #26

Open pauloacosta opened 4 years ago

pauloacosta commented 4 years ago

Fatal error: Uncaught ChromeDevtoolsProtocol\Exception\RuntimeException: Executable [chrome] not found. in C:\xampp\htdocs\vendor\jakubkulhan\chrome-devtools-protocol\src\ChromeDevtoolsProtocol\Instance\Launcher.php:139 Stack trace: #0 C:\xampp\htdocs\index.php(17): ChromeDevtoolsProtocol\Instance\Launcher->launch(Object(ChromeDevtoolsProtocol\Context)) #1 {main} thrown in C:\xampp\htdocs\vendor\jakubkulhan\chrome-devtools-protocol\src\ChromeDevtoolsProtocol\Instance\Launcher.php on line 139 

Code:

`<?php

require "./vendor/autoload.php";

use ChromeDevtoolsProtocol\Context; use ChromeDevtoolsProtocol\Instance\Launcher; use ChromeDevtoolsProtocol\Model\Page\PrintToPDFRequest; use ChromeDevtoolsProtocol\Model\Page\NavigateRequest;

@unlink(DIR . '/test.pdf');

// context creates deadline for operations $ctx = Context::withTimeout(Context::background(), 30 / seconds /);

// launcher starts chrome process ($instance) $launcher = new Launcher(); $instance = $launcher->launch($ctx);

try { // work with new tab $tab = $instance->open($ctx); $tab->activate($ctx);

$devtools = $tab->devtools();
try {
    $devtools->page()->enable($ctx);
    $devtools->page()->navigate($ctx, NavigateRequest::builder()->setUrl("https://www.google.com/")->build());
    $devtools->page()->awaitLoadEventFired($ctx);
    $data = $devtools->page()->printToPDF($ctx, PrintToPDFRequest::fromJson((object) [
        'displayHeaderFooter' => false
    ]))->data;
    file_put_contents(__DIR__ . '/../test.pdf', base64_decode($data));
} finally {
    // devtools client needs to be closed
    $devtools->close();
}

} finally { // process needs to be killed $instance->close(); }`

Any idea what could be going on? Is there a place where I set the Chrome address? On my system it is installed at the default address.

official-ymail-com commented 4 years ago

You need to install Chrome

For example, I am using ubuntu 18 $ wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb $ sudo apt install ./google-chrome-stable_current_amd64.deb

pauloacosta commented 4 years ago

I am using Windows 10 and Chrome is installed at default folder: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

Is there a file where I set up the Chrome address?

official-ymail-com commented 3 years ago

You can specific Chrome path like this:

$launcher = new Launcher();
$launcher->setExecutable('/usr/bin/google-chrome-stable');
$instance = $launcher->launch();
pauloacosta commented 3 years ago

Thank You! Worked like a charm!

I am a Windows user, then my code was:

// launcher starts chrome process ($instance)
$launcher = new Launcher();
$launcher->setExecutable('C:\Program Files (x86)\Google\Chrome\Application');
$instance = $launcher->launch($ctx);