how to save DOMPDF generated content to file?

119,378

Solution 1

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.

Solution 2

I did test your code and the only problem I could see was the lack of permission given to the directory you try to write the file in to.

Give "write" permission to the directory you need to put the file. In your case it is the current directory.

Use "chmod" in linux.

Add "Everyone" with "write" enabled to the security tab of the directory if you are in Windows.

Share:
119,378
TomTom
Author by

TomTom

Analyst with programming passion with Python and PHP

Updated on July 08, 2022

Comments

  • TomTom
    TomTom almost 2 years

    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());