33 lines
1.4 KiB
PHP
33 lines
1.4 KiB
PHP
<?php
|
|
header("Content-Type: application/json");
|
|
$out = [];
|
|
// Find all chromium-like binaries
|
|
foreach (["/usr/bin/chromium-browser","/usr/bin/chromium","/usr/bin/google-chrome","/snap/bin/chromium","/usr/bin/chrome"] as $b) {
|
|
$out["binaries"][$b] = file_exists($b);
|
|
}
|
|
|
|
// Test headless
|
|
$test_html = "/tmp/test-chart.html";
|
|
file_put_contents($test_html, '<html><body><h1>test</h1><script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script><canvas id="c"></canvas><script>setTimeout(()=>{var ctx=document.getElementById("c");new Chart(ctx,{type:"bar",data:{labels:["A","B","C"],datasets:[{data:[1,2,3]}]}});},500);</script></body></html>');
|
|
|
|
// Find bin
|
|
$bin = null;
|
|
foreach (["/usr/bin/chromium","/usr/bin/chromium-browser","/snap/bin/chromium","/usr/bin/google-chrome"] as $b) {
|
|
if (file_exists($b) || @shell_exec("which " . basename($b) . " 2>/dev/null")) {
|
|
$bin = $b;
|
|
break;
|
|
}
|
|
}
|
|
$out["chosen_bin"] = $bin;
|
|
|
|
if ($bin) {
|
|
$cmd = "timeout 30 $bin --headless --disable-gpu --no-sandbox --virtual-time-budget=8000 --print-to-pdf=/tmp/test-chart.pdf --print-to-pdf-no-header file:///tmp/test-chart.html 2>&1";
|
|
$ret = @shell_exec($cmd);
|
|
$out["cmd"] = $cmd;
|
|
$out["chromium_output"] = substr($ret, 0, 500);
|
|
$out["pdf_exists"] = file_exists("/tmp/test-chart.pdf");
|
|
$out["pdf_size"] = $out["pdf_exists"] ? filesize("/tmp/test-chart.pdf") : 0;
|
|
}
|
|
|
|
echo json_encode($out, JSON_PRETTY_PRINT);
|