I'm using pagedjs to render my invoice sample (using table, tr, td, th, etc...) I use chromedriver v 135.0.7049.84 to open a headless browser and having export pdf from my html using polyfill pagedjs script, it opens very well and show my invoice in pdf properly. But when I tries chromedriver like below:
// Generate PDF with Chrome Headless try { // Set Chrome options $options = new \Facebook\WebDriver\Chrome\ChromeOptions(); $options->addArguments([ '--headless', '--disable-gpu', '--no-sandbox', ]); // Set the Chrome preference to specify the download directory $options->setExperimentalOption('prefs', [ 'download.default_directory' => $uploadPath, // Specify the directory for saving PDF 'download.prompt_for_download' => false, // Avoid the prompt for file download ]); // Create WebDriver instance $capabilities = \Facebook\WebDriver\Remote\DesiredCapabilities::chrome(); $capabilities->setCapability(\Facebook\WebDriver\Chrome\ChromeOptions::CAPABILITY, $options); $driver = \Facebook\WebDriver\Remote\RemoteWebDriver::create($this->chromedriverhost, $capabilities); // Load the HTML content (using file:// URL for local HTML file) $driver->get('file://' . $outputHtml); // Execute the printToPDF command via Chrome DevTools Protocol $response = $driver->executeCustomCommand( '/session/' . $driver->getSessionID() . '/chromium/send_command', // URL path for the custom command 'POST', // HTTP method [ 'cmd' => 'Page.printToPDF', // Custom command to execute (this could be any supported DevTools command) 'params' => [ // Parameters required for the command 'printBackground' => true, 'scale' => 0.8, ] ] ); // Decode and save PDF if data is returned if (isset($response['value']['data']) && !empty($response['value']['data'])) { // Base64 decode the PDF data $pdfData = base64_decode($response['value']['data']); $pdfFilePath = $outputPdf; // Specify the filename // Save the PDF to the specified path file_put_contents($pdfFilePath, $pdfData); } else { echo 'Error: Failed to generate PDF or received null response'; return $this; } // Quit the driver $driver->quit(); } catch (\Exception $e) { echo 'Error: ' . $e->getMessage(); // Handle any errors return $this; }
My $response is always null. Any though?
I checked with GPT and same output. I think I'm doing some steps wrong.