Search code examples
phpwkhtmltopdfsnappy

snappy wkhtmltopdf wrapper send generated html file to browser


I googled like a mole, but can´t find the right way to go.

I´m creating a PDF with the WKHTMLTOPDF Wrapper Snappy.

How can I send the pdf generated with the method generateFromHtml straight to the Browser? Thats what I´m trying to do:

header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $snappy->generateFromHtml($contents);

Solution

  • You want to use the getOutput/getOutputFromHtml methods to return the PDF as a string, generate/generateFromHtml will save the PDF to a file and not return anything.

    header('Content-Type: application/pdf');
    // Remove the next line to let the browser display the PDF
    header('Content-Disposition: attachment; filename="file.pdf"');
    echo $snappy->getOutputFromHtml($contents);
    

    Snappy GeneratorInterface and documentation on GitHub

    • Save PDF of URL $input to file $output
      generate($input, $output, array $options = array(), $overwrite = false)

    • Save PDF of HTML $html to file $output
      generateFromHtml($html, $output, array $options = array(), $overwrite = false)

    • Return PDF of URL $input as string
      getOutput($input, array $options = array())

    • Return PDF of HTML $html as string
      getOutputFromHtml($html, array $options = array())