0

I'm using the basic code from bundle to generate pdf and the browser-output works. (Controller)

public function fileAction($name, Request $request)
{
  $format = $this->get('request')->get('_format');      
  $em = $this->getDoctrine()->getManager();
  $entity = $em->getRepository('TestBundle:Kunden')->find($name);

  return $this->render(sprintf('TestBundle:Kunden:file.%s.twig', $format), array(
    'name' => $name,
    'entity' => $entity,
  ));

I want to save the file in upload folder, so I tried the code I found in documentation or other posts. (Controller)

 public function pdfAction($name)
{
  $format = $this->get('request')->get('_format');                  
  $em = $this->getDoctrine()->getManager();
  $entity = $em->getRepository('TestBundle:Kunden')->find($name);
  $facade = $this->get('ps_pdf.facade');
  $response = new Response();                           

  $this->render(sprintf('TestBundle:Kunden:file.%s.twig', $format), array('entity' => $entity,'name' => $name,), $response);

  $xml = $response->getContent();
  $content = $facade->render($xml);

  return new Response($content, 200, array('content-type' => 'application/pdf'));
}

I know that I missed something, but I don't know where I can define the path or filename. How could I save the generated pdf from fileAction() in upload folder?

1 Answer 1

1

What you are doing there is just generating a pdf file and returning it as a response / download response. Saving a file to an upload directory is another task. I'd recommend you using a file abstraction layer for that, e.g. "Gaufrette" or "Flysystem" Can't post a whole example here because it would be too long including all the config stuff, but in the end you would have something like:

 $filesystem->write('myFile', $content);

Hope this helps, ++.

Sign up to request clarification or add additional context in comments.

3 Comments

I used Gaufrette to save my pdf and it works. I generate the new file with the html-Template of the pdf-file and the pdf-preview with the pdf-Template. Not exactly what I want, because I wanted to generate the file from the pdf-Template. But thanks a lot - it's the best solution up to now.
Glad I could help. What's the difference between your html-Template and pdf-Template ? Aren't they both twig templates ? I don't know the bundle you're using, maybe I am missing something.
they are both twig templates, for the preview-pdf-Template the markup is like this: <pdf><dynamic-page></dynamic-page></pdf>. I use github.com/psliwa/PdfBundle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.