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 getTemplateEditForm(Application $app, Request $request, $templateId) { $error = $request->query->get('error'); $repo = $app->getTemplateRepository(); $add = false; $template = $repo->getById($templateId); if ($template === null) { $defaults = null; $add = true; } else { $defaults = ['name' => $template->getName(), 'template' => $template->getTemplate(), 'description' => $template->getDescription()]; } $form = $app['form.factory']->createBuilder('form', $defaults)->add('name', 'text')->add('template', 'textarea')->add('description', 'textarea', array('required' => false))->getForm(); // handle form submission $form->handleRequest($request); if ($form->isValid()) { $data = $form->getData(); if ($add) { $template = new Template(); } $template->setTemplate($data['template'])->setName($data['name'])->setDescription($data['description']); if ($add) { if (!$repo->add($template)) { return $app->redirect($app['url_generator']->generate('templates_add', array('error' => 'Failed adding template'))); } } else { $repo->update($template); } return $app->redirect($app['url_generator']->generate('templates_index')); } return new Response($app['twig']->render('templates/edit.html.twig', ['form' => $form->createView(), 'template' => $template, 'error' => $error])); }