67 lines
1.7 KiB
PHP
67 lines
1.7 KiB
PHP
<?php
|
|
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
|
|
use Dompdf\Dompdf;
|
|
use Dompdf\Options;
|
|
|
|
$router = new \Bramus\Router\Router();
|
|
|
|
$router->set404('/api(/.*)?', function () {
|
|
header('HTTP/1.1 404 Not Found');
|
|
header('Content-Type: application/json');
|
|
|
|
$jsonArray = array();
|
|
$jsonArray['status'] = "404";
|
|
$jsonArray['status_text'] = "route not defined";
|
|
|
|
echo json_encode($jsonArray);
|
|
});
|
|
|
|
$router->post('/api/html2pdf', function () {
|
|
|
|
$json_data = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (isset($json_data['html'])) {
|
|
|
|
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
|
|
|
|
$html = $json_data['html'];
|
|
$md5 = md5($html);
|
|
|
|
$attachment = sprintf("/tmp/%s.pdf", $md5);
|
|
|
|
if (!file_exists($attachment)) {
|
|
|
|
$options = new Options();
|
|
$options->setIsRemoteEnabled(true);
|
|
|
|
$dompdf = new Dompdf($options);
|
|
$dompdf->loadHtml($html);
|
|
$dompdf->setPaper('A5');
|
|
$dompdf->render();
|
|
$output = $dompdf->output();
|
|
file_put_contents($attachment, $output);
|
|
|
|
header("X-Local-Cache: miss");
|
|
} else {
|
|
header("X-Local-Cache: hit");
|
|
}
|
|
|
|
header("Cache-Control: public");
|
|
header("Content-Type: application/pdf");
|
|
header("Content-Transfer-Encoding: Binary");
|
|
header("Content-Length:" . filesize($attachment));
|
|
header("Content-Disposition: attachment; filename=export.pdf");
|
|
readfile($attachment);
|
|
} else {
|
|
header("HTTP/1.1 422 Unprocessable Entity");
|
|
header("Content-Type: application/json");
|
|
echo json_encode(array("status_text" => "missing parameters"));
|
|
}
|
|
|
|
});
|
|
|
|
$router->run();
|
|
|