Search code examples
phpfilepdf-generationsavedompdf

how to save DOMPDF generated content to file?


I am using Dompdf to create PDF file but I don't know why it doesn't save the created PDF to server.

Any ideas?

require_once("./pdf/dompdf_config.inc.php");
    $html =
      '<html><body>'.
      '<p>Put your html here, or generate it with your favourite '.
      'templating system.</p>'.
      '</body></html>';

    $dompdf = new DOMPDF();
    $dompdf->load_html($html);
    $dompdf->render();
    file_put_contents('Brochure.pdf', $dompdf->output());

Solution

  • I have just used dompdf and the code was a little different but it worked.

    Here it is:

    require_once("./pdf/dompdf_config.inc.php");
    $files = glob("./pdf/include/*.php");
    foreach($files as $file) include_once($file);
    
    $html =
          '<html><body>'.
          '<p>Put your html here, or generate it with your favourite '.
          'templating system.</p>'.
          '</body></html>';
    
        $dompdf = new DOMPDF();
        $dompdf->load_html($html);
        $dompdf->render();
        $output = $dompdf->output();
        file_put_contents('Brochure.pdf', $output);
    

    Only difference here is that all of the files in the include directory are included.

    Other than that my only suggestion would be to specify a full directory path for writing the file rather than just the filename.