Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove header from pdf file #19

Closed
artworkad opened this issue Jan 20, 2013 · 7 comments
Closed

Remove header from pdf file #19

artworkad opened this issue Jan 20, 2013 · 7 comments

Comments

@artworkad
Copy link

Hey folks,

when I generate a pdf it always has this

HTTP/1.0 200 OK Cache-Control: no-cache Date: Sun, 20 Jan 2013 11:17:25 GMT

at the top. How can I avoid that?

@galan83
Copy link

galan83 commented Jan 24, 2013

If you are using render method for the html generation, you should change to renderView.

@pilot
Copy link
Contributor

pilot commented Feb 19, 2013

put here how you response a pdf file

@artworkad
Copy link
Author

   $html = $this->render('MyMainBundle:CRM:pdf.html.twig', array(
        'request' => $cr_request
   ));

    return new Response(
                    $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
                    200,
                    array(
                        'Content-Type' => 'application/pdf',
                        'Content-Disposition' => 'attachment; filename="Customer-Request.pdf"'
                    )
    );

@stloyd
Copy link
Contributor

stloyd commented Feb 22, 2013

You have those headers because you call it via Symfony2 Response. Is this a real problem that you have this header? Because this is standard header for HTTP responses (just sometimes "cache" is omitted). Or you want different? Then you can manipulate headers of response in Symfony2... Here is with simple echo.

$html = $this->render('MyMainBundle:CRM:pdf.html.twig', array(
    'request' => $cr_request
));

header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $this->get('knp_snappy.pdf')->getOutputFromHtml($html);

Closing this as this is not an issue.

@stloyd stloyd closed this as completed Feb 22, 2013
@slucas-nwx
Copy link

getContent() will keep you from grabbing the headers.

$html = $this->render('MyMainBundle:CRM:pdf.html.twig', array(
        'request' => $cr_request
   ))->getContent();


    return new Response(
                    $this->get('knp_snappy.pdf')->getOutputFromHtml($html),
                    200,
                    array(
                        'Content-Type' => 'application/pdf',
                        'Content-Disposition' => 'attachment; filename="Customer-Request.pdf"'
                    )
    );

@Tofandel
Copy link

Tofandel commented Jul 31, 2017

Or just use the renderView method instead of the render method as said by @galan83

$html = $this->renderView('pdf.html.twig', array(/* vars to pass to the twig template */) );

return new Response(
	$this->get('knp_snappy.pdf')->getOutputFromHtml($html),
	200,
	array(
		'Content-Type'          => 'application/pdf',
		'Content-Disposition'   => 'inline; filename="file.pdf"'
		//inline = display in browser, attachment = download
	)
);

@stloyd you misunderstood the issue, the op meant that the headers were actually displayed as text in the pdf

@odisleysi
Copy link

The last part of the previous solutions did not work for me, but this way it work for me.

        return new PdfResponse(
            $this->get('knp_snappy.pdf')->getOutputFromHtml(
                $this->renderView('GeneralBundle:pdf:pdf.html.twig', array( /*vars*/ ))
            ),
            $filename . '.pdf'
        );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants