mstdmstd's avatar

How to save on disk file generated with spatie/browsershot

Hello, In my laravel 5.8/blade/jQuery v3.3.1 app with https://github.com/spatie/browsershot plugin I managed in my kubuntu 18 to upload my file with path

     $save_to_file= '/home/username/Downloads/file.pdf';
     Browsershot::html(htmlspecialchars_decode($pdf_content))
         ->showBackground()
         ->save($save_to_file);

But to run it without error I had to run in my console :

    sudo chmod -R 777 /home/username/Downloads/

otherwize I got error access permission

  1. I wonder if there is a way to escape this error on client's OS, as I can not run
    sudo chmod -R 777 /home/username/Downloads/

on client's site. I search OS independently decision

  1. Can you advice some Javascript/Jquery script to open "SaveAs" dialog for choosing of saving location? And maybe somehow to check if app has not write access to info user?

Thanks!

0 likes
5 replies
bobbybouwmann's avatar

You shouldn't save the file outside the project directory. The best place to store is probably the storage directory

$save_to_file = storage_path('pdfs/file.pdf');

Browsershot::html(htmlspecialchars_decode($pdf_content))
    >showBackground()
    ->save($save_to_file);

This shouldn't give you any other problems AFAIK

2 likes
mstdmstd's avatar

I tried to make as :

            $save_to_file = storage_path( 'generate_profile_card_' . Session::getId() . '/' . $filename_to_save );
            // it has value /mnt/_work_sdb8/wwwroot/lar/votes/storage/generate_profile_card/5/filename.pdf

            Browsershot::html(htmlspecialchars_decode($pdf_content))
                ->showBackground()
                ->save($save_to_file);

and got error :

Symfony \ Component \ Process \ Exception \ ProcessFailedException
The command "PATH=$PATH:/usr/local/bin NODE_PATH=`npm root -g` node '/mnt/_work_sdb8/wwwroot/lar/votes/vendor/spatie/browsershot/src/../bin/browser.js' '{"url":"file:\/\/\/tmp\/0712247001562138587\/index.html","action":"pdf","options":{"path":"\/mnt\/_work_sdb8\/wwwroot\/lar\/votes\/storage\/generate_profile_card_Qwr94TQ4MqSagLRDf7jWP0ygk0rpcdbtCBWXz3az\/filename.pdf","args":[],"viewport":{"width":800,"height":600},"displayHeaderFooter":false,"printBackground":true}}'" failed. Exit Code: 1(General error) Working directory: /mnt/_work_sdb8/wwwroot/lar/votes/public Output: ================ Error Output: ================ ┌──────────────────────────────────────────────────────┐ │ npm update check failed │ │ Try running with sudo or get access │ │ to the local update config store via │ │ sudo chown -R $USER:$(id -gn $USER) /var/www/.config │ └──────────────────────────────────────────────────────┘ { Error: ENOENT: no such file or directory, open '/mnt/_work_sdb8/wwwroot/lar/votes/storage/generate_profile_card_Qwr94TQ4MqSagLRDf7jWP0ygk0rpcdbtCBWXz3az/filename.pdf' -- ASYNC -- at Page.<anonymous> (/mnt/_work_sdb8/wwwroot/lar/votes/node_modules/puppeteer/lib/helper.js:110:27) at getOutput (/mnt/_work_sdb8/wwwroot/lar/votes/vendor/spatie/browsershot/bin/browser.js:26:40) at callChrome (/mnt/_work_sdb8/wwwroot/lar/votes/vendor/spatie/browsershot/bin/browser.js:148:24) at process._tickCallback (internal/process/next_tick.js:68:7) errno: -2, code: 'ENOENT', syscall: 'open', path: '/mnt/_work_sdb8/wwwroot/lar/votes/storage/generate_profile_card_Qwr94TQ4MqSagLRDf7jWP0ygk0rpcdbtCBWXz3az/filename.pdf' }

/mnt/_work_sdb8/wwwroot/lar/votes/ - root of the app OR
/_wwwroot/lar/votes                - with linked path 

I tried to run a command in error message above:

$ sudo chown -R $USER:$(id -gn $USER) /var/www/.config
chown: cannot access '/var/www/.config': No such file or directory

?

arifulhb's avatar

Did anyone got any solution? I am having the same issue.

Please or to participate in this conversation.