public function rootAction(Application $app, Request $request) { $data = $this->prepareInput(); if ($data === null) { return new JsonResponse(['error' => 'no json data found'], 400); } $templateName = isset($data['template']) ? $data['template'] : null; $templateData = isset($data['data']) ? $data['data'] : null; if (!$templateName || !$templateData) { return new JsonResponse(['error' => 'template and data must be set'], 400); } $repo = $app->getTemplateRepository(); $template = $repo->getByName($templateName); if (!$template) { return new JsonResponse(['error' => "template {$templateName} not found"], 404); } $twig = new \Twig_Environment(new \Twig_Loader_String()); $html = $twig->render($template->getTemplate(), $templateData); $file = new File(); $file->setId(Uuid::uuid4()->toString()); $file->setCreatedAt(date('Y-m-d H:i:s')); $file->setPath($this->getFilePath($file)); $snappy = new Pdf(); if (substr(php_uname(), 0, 7) == "Windows") { $snappy->setBinary('vendor\\bin\\wkhtmltopdf.exe.bat'); } else { $snappy->setBinary('vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64'); } $snappy->generateFromHtml($html, $file->getPath()); $repo = $app->getFileRepository(); $repo->add($file); return new JsonResponse(['id' => $file->getId()], 201); }
private function rowToObject($row) { if (!$row) { return null; } $obj = new File(); $obj->setId($row['id'])->setPath($row['path'])->setCreatedAt($row['created_at']); return $obj; }