Trying to created pdf - Failed to load PDF document

trying to create a simple pdf, but keeps returning an error when viewing “Failed to load PDF document.” code copied from phpM export function. options and other items not used.

some function() {
    $Options = [];
    $PdfBackend = "";
    $FileExtension = "pdf";
    $PageSize = "a4";
    $PageOrientation = "portrait";
    
    $options = new \Dompdf\Options($Options);
    $options->set("pdfBackend", $PdfBackend);
    $options->set("isRemoteEnabled", true); // Support remote images such as S3
    $options->setDefaultFont('Courier');
    
    $chroot = $options->getChroot();
    $chroot[] = UploadTempPathRoot();
    $chroot[] = UploadTempPath();
    $chroot[] = dirname(CssFile(Config("PDF_STYLESHEET_FILENAME"), false));

    $options->setChroot($chroot);
    $campaignid = 5;
    $employeeid = 5;
    
    $fileName = "test.pdf";        
    $file = "html/test.html";
    
    $html = file_get_contents($file);

    $dompdf = new \Dompdf\Dompdf();
    $dompdf->loadHtml($html);
    $dompdf->setPaper($PageSize, $PageOrientation);

    $dompdf->render();
    $dompdf->stream('5.pdf', array("Attachment" => true));    
}
<html lang="en">
<body>
hello world
</body>
</html>

Where did you put your code? Is it a Custom File with “Include common files” enabled? Or a standalone script? Make sure the relative path (relative to the script with your code) of the HTML file ("html/test.html") is correct. You may debug by die($html); first, make sure the HTML file is correctly loaded.

You also did not use your $options, you may read dompdf’s docs on Setting Options.

the function resides in userfn.php.

the create pdf function is used called within another function that sends a message to the user, we need to create a pdf letter if noted in the users record, we would than create the pdf file, save it to the the uploads/specific folder then attach it to the message…

as well, prefixing the file name to the required folder to save, does not work, it always saves to the downloads folder.

we just want to create the file save it to the specified directory and that’s it, not needed to display or download.

not sure if we should generate to a variable then write it out to a file ?

issue resolve hopefully…

    if ($save) { // Save to folder
        // *** REMOVED *** $dompdf->stream($fileName, array("Attachment" => true)); *** ADDED LINES BELOW ***
        $output = $dompdf->output();
        file_put_contents("$fileName", $output);        
    }