Chromedriver ExecuteCustomcommand Get Base64, codiert aus PDF -Ausgang mit PAGEDJS RELENDED HTMLPhp

PHP-Programmierer chatten hier
Anonymous
 Chromedriver ExecuteCustomcommand Get Base64, codiert aus PDF -Ausgang mit PAGEDJS RELENDED HTML

Post by Anonymous »

Ich verwende PageDJs, um meine Rechnungsprobe zu rendern (mit Tabelle, TR, TD, Th usw.). unten: < /p>
// 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;
}
< /code>
Meine $ Antwort ist immer null. Irgendwelche? < /P>
Ich habe mit GPT und derselben Ausgabe überprüft. Ich glaube, ich mache ein paar Schritte falsch.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post