Files
html/api/wevia-filegen.php
2026-04-12 22:57:03 +02:00

91 lines
4.5 KiB
PHP

<?php
header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");
$action = $_GET["action"] ?? $_POST["action"] ?? "";
$title = $_GET["title"] ?? $_POST["title"] ?? "WEVAL Report";
$content = $_GET["content"] ?? $_POST["content"] ?? "";
$input = json_decode(file_get_contents("php://input"), true) ?: [];
$title = $input["title"] ?? $title;
$content = $input["content"] ?? $content;
$ts = date("Ymd-His");
switch ($action) {
case "pdf":
$html = "<html><head><style>body{font-family:Arial;margin:40px}h1{color:#1a237e}h2{color:#0d47a1}table{border-collapse:collapse;width:100%}td,th{border:1px solid #ddd;padding:8px}th{background:#1a237e;color:white}.header{text-align:center;padding:20px;border-bottom:3px solid #1a237e}.footer{text-align:center;font-size:10px;color:#666;margin-top:40px}</style></head><body>";
$html .= "<div class='header'><h1>" . htmlspecialchars($title) . "</h1><p>WEVAL Consulting - " . date("d/m/Y") . "</p></div>";
$html .= "<div>" . nl2br(htmlspecialchars($content ?: "Rapport genere par WEVIA Engine")) . "</div>";
$html .= "<div class='footer'>WEVAL Consulting | Casablanca - Paris | weval-consulting.com</div></body></html>";
$tmpHtml = "/tmp/wevia-pdf-{$ts}.html";
$tmpPdf = "/var/www/weval/wevia-ia/downloads/wevia-report-{$ts}.pdf";
@mkdir("/var/www/html/uploads", 0755, true);
file_put_contents($tmpHtml, $html);
exec("wkhtmltopdf --quiet {$tmpHtml} {$tmpPdf} 2>&1", $out, $ret);
if (file_exists($tmpPdf)) {
echo json_encode(["ok"=>true, "url"=>"/uploads/wevia-report-{$ts}.pdf", "size"=>filesize($tmpPdf)]);
} else { echo json_encode(["error"=>"PDF generation failed"]); }
break;
case "excel":
$script = "import openpyxl,json,sys
data=json.loads(sys.argv[1])
wb=openpyxl.Workbook()
ws=wb.active
ws.title=data.get('title','Sheet1')[:31]
for row in data.get('rows',[]):
ws.append(row)
f='/var/www/weval/wevia-ia/downloads/wevia-report-{$ts}.xlsx'
wb.save(f)
print(json.dumps({'ok':True,'url':'/wevia-ia/downloads/wevia-report-{$ts}.xlsx'}))";
$data = json_encode(["title"=>$title, "rows"=>$input["rows"] ?? [["Col1","Col2"],["data1","data2"]]]);
@mkdir("/var/www/html/uploads", 0755, true);
$escaped = escapeshellarg($data);
exec("python3 -c " . escapeshellarg($script) . " " . $escaped . " 2>&1", $out);
echo implode("", $out) ?: json_encode(["error"=>"Excel failed"]);
break;
case "chart":
$script = "import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import json,sys
d=json.loads(sys.argv[1])
plt.figure(figsize=(10,6))
plt.bar(d.get('labels',[]),d.get('values',[]),color=d.get('colors',['#1a237e','#0d47a1','#1565c0','#1976d2','#1e88e5']))
plt.title(d.get('title',''))
plt.tight_layout()
f='/var/www/weval/wevia-ia/downloads/wevia-chart-{$ts}.png'
plt.savefig(f,dpi=150)
print(json.dumps({'ok':True,'url':'/wevia-ia/downloads/wevia-chart-{$ts}.png'}))";
$data = json_encode(["title"=>$title, "labels"=>$input["labels"]??["SAP","Cloud","Cyber","Data","IA"],
"values"=>$input["values"]??[95,88,92,85,97]]);
@mkdir("/var/www/html/uploads", 0755, true);
exec("python3 -c " . escapeshellarg($script) . " " . escapeshellarg($data) . " 2>&1", $out);
echo implode("", $out) ?: json_encode(["error"=>"Chart failed"]);
break;
case "pptx":
$script = "from pptx import Presentation
from pptx.util import Inches,Pt
import json,sys
d=json.loads(sys.argv[1])
p=Presentation()
for s in d.get('slides',[{'title':'WEVAL','content':'Consulting'}]):
sl=p.slides.add_slide(p.slide_layouts[1])
sl.shapes.title.text=s.get('title','')
sl.placeholders[1].text=s.get('content','')
f='/var/www/weval/wevia-ia/downloads/wevia-deck-{$ts}.pptx'
p.save(f)
print(json.dumps({'ok':True,'url':'/wevia-ia/downloads/wevia-deck-{$ts}.pptx'}))";
$data = json_encode(["slides"=>$input["slides"]??[
["title"=>"WEVAL Consulting","content"=>"IA Souverain - Cloud - SAP - Cyber"],
["title"=>$title,"content"=>$content ?: "Genere par WEVIA Engine"]
]]);
@mkdir("/var/www/html/uploads", 0755, true);
exec("python3 -c " . escapeshellarg($script) . " " . escapeshellarg($data) . " 2>&1", $out);
echo implode("", $out) ?: json_encode(["error"=>"PPTX failed"]);
break;
default:
echo json_encode(["actions"=>["pdf","excel","chart","pptx"],"usage"=>"POST with title+content or rows/labels/values/slides"]);
}