简体   繁体   中英

Symfony Console Commands v6 and Dompdf --- file_put_contents - Failed to open stream: Permission denied (windows)

I'm new to consoles stuff. My goal is to create a platform independent command line (windows, mac, linux) to convert an hmtl file to a pdf. I'm using to create the command Symfony Console Commands Version 6 (so I can easily run it on all operating systems) and to convert the HTML i am trying to use Dompdf.

I tried saving my HTML to a file and it worked.

$myhtmlfile = fopen($newDirectory . DIRECTORY_SEPARATOR . $fileName . ".html", "w") or die("Unable to create file!");
fwrite($myhtmlfile, $myhtml);

Now i want to save the html as PDF file with Dompdf.

$myhtml ='<html><body>'. '<p>I wand a PDF with this TEXT!</p>'. '</body></html>'; 

$dompdf = new DOMPDF();
$dompdf->loadhtml($myhtml);
$dompdf->render();
$output = $dompdf->output();
file_put_contents($newDirectory . DIRECTORY_SEPARATOR . $fileName. ".pdf" , $output);

But I suddenly get an error message "Permission denied"... Why can I save the HTML file but not this PDF? I'm trying to save them in the same directory created a few lines above the line where I create the HTML file.

// call command in terminal
C:\Users\...\app>php documentGenerator.php doc:GenerateDocu
    
// Result
PHP Warning:  file_put_contents(C:\Users\...\generatedDocs\generated-documentation-2022-06-11-09-29-54\generated-documentation-2022-06-11-09-29-54.pdf): Failed to open stream: Permission denied in C:\Users\...\app\resources\generatePdf.php on line 68

Is there a (simple) solution for this that works on Windows, Mac and Linux?

There is a good checklist for these permission issues on these widely used functionalities here:

PHP - Failed to open stream: No such file or directory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM