I'm trying to use Puppeteer and BrowserShot on an Ubuntu machine to make and screenshot of a static HTML.
The thing is that every time I try to make and screenshot:
Screenshot::loadView('monthly_resume.resume')
->waitUntilNetworkIdle(true)
->windowSize(1200, 630)
->noSandbox()
->storeAs($this->pathFor($user), 'image.php');
It returns an error. It seems that the HTML it's stored properly under a folder inside /tmp, in this case tmp/441150534-0230735001662479158/index.html.
But when node tries to find it says that the file it's not found even when it's there. Any idea why node can't find it?
Symfony\Component\Process\Exception\ProcessFailedException
The command "PATH=$PATH:/usr/local/bin:/opt/homebrew/bin NODE_PATH=`npm root -g` node '/home/forge/monse.app/vendor/spatie/browsershot/src/../bin/browser.js' '{"url":"file:\/\/\/tmp\/441150534-0230735001662479158\/index.html","action":"screenshot","options":{"type":"png","path":"\/tmp\/1562301876-0230606001662479158\/BrowsershotOutput1662479158hbgfB.png","args":["--no-sandbox"],"viewport":{"width":1200,"height":630},"displayHeaderFooter":false,"waitUntil":"networkidle0"}}'" failed.
Exit Code: 1(General error)
Working directory: /home/forge/monse.app
Output:
================
Error Output:
================
Error: net::ERR_FILE_NOT_FOUND at file:///tmp/441150534-0230735001662479158/index.html
at navigate (/home/forge/node_modules/puppeteer/lib/cjs/puppeteer/common/Frame.js:225:23)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Frame.goto (/home/forge/node_modules/puppeteer/lib/cjs/puppeteer/common/Frame.js:195:21)
at async Page.goto (/home/forge/node_modules/puppeteer/lib/cjs/puppeteer/common/Page.js:1143:16)
at async callChrome (/home/forge/monse.app/vendor/spatie/browsershot/bin/browser.js:249:26)
I encountered exact error message that you got. But then I just change temp path and its working fine. I'm using spatie/browsershot by the way
To change temp path at spatie/browsershot:
Browsershot::html('some html string')
->setCustomTempPath('/home/your/other/path')
->save('filename.jpg')
Related
I am developing a web application using the Laravel framework. I need to generate the web page into pdf/ image in my application. I am using PHP browser shot spatie package for it, https://github.com/spatie/browsershot. I created a test function to generate the image using it with the following code.
\Spatie\Browsershot\Browsershot::url("https://www.google.com/")->save(public_path() . '/example.png');
When I run the function, I got the following error.
The command "PATH=$PATH:/usr/local/bin NODE_PATH=`npm root -g` node '/var/www/vendor/spatie/browsershot/src/../bin/browser.js' '{"url":"https:\/\/www.google.com\/","action":"screenshot","options":{"type":"png","path":"\/var\/www\/public\/example.png","args":[],"viewport":{"width":800,"height":600}}}'" failed. Exit Code: 127(Command not found) Working directory: /var/www/public Output: ================ Error Output: ================ sh: 1: npm: not found sh: 1: node: not found
I am using Docker and Docker Compose for my project. What is wrong with my code?
You need to tell BrowserShot to point to the installation binary of node and npm.
Modify your code this way
\Spatie\Browsershot\Browsershot::url("https://www.google.com/")
->setNodeBinary('/usr/bin/node')
->setNpmBinary('/usr/bin/npm')
->save(public_path() . '/example.png');
Make sure to modify '/usr/bin/node' and '/usr/bin/npm' accordingly.
Few weeks ago I set up a Message system with Symfony Messenger and it worked great.
Today I wanted to create new object through message, so I went to my server and type the command to consume message
First I had this result
$ bin/console messenger:consume-messages amqp_notifications
/usr/bin/env: ‘php\r’: No such file or directory
It never happened before with my files, and I never changed the line ending or encoding of my file sin PHPstorm.
I tried to use $ php bin/console messenger:consume-messages amqp_notifications
but then I had this error.
Attempted to load class "AMQPConnection" from the global namespace.
Did you forget a "use" statement?
Pretty weird, because I have have the php-amqp ext installed as you can see on the screenshot of my phpinfo
I didn't change anything in my Message class or Handler.
Also, I tried to call new AMQPConnection() on a random action, just to try, and I didn't get the error.
I'm completely lost with this error this time, as everything is installed.
I use PHP 7.3.1 and symfony Messenger 4.2.2
It seems your second issue was already solved by ccKep on his comment.
The first one is that the specific shebang line #!/usr/bin/env php executes the first php found in the $PATH. So if you already have uninstalled it, which seems the case, or it has a symbolic link to another php version, you can get a wrong result.
Tries to check what is inside the $PATH and replace the PHP path for the correct one. You might get the place running which php.
I'm sorry about my bad english.
I have encountered this error after installing the lamp server with this code. Previously, my same file system was working properly.
Lamp Server İnstall Command: "sudo tasksel install lamp-server"
Config File : define("URL", "http://localhost/test/");
Code: header("location:".URL."login");
Result: http://localhost/test/templates/URLlogin
I have no idea what the source of the problem is. Although I searched the web, I couldn't get any results. Thank you in advance for your help.
Respects
If you are getting URL printed instead of your constant, its because it is not defined in your current scope.
Is your config file being included or required at all anywhere? I would suggest it is not.
The default PHP behavior is to use the constant name as a string if it is not defined.
This would also trigger a warning in your logs.
Eg if you
define('URL', 'your url);
In one file, but do not have access/scope in another then using URL will give it to you as a string.
Eg
header('Location: ' . URL);
Will try to header over to 'Location: URL' instead of 'Location: your url'
I am trying to create pdf with Laravel snappy.
The problem is when I try to run wkhtmltopdf command in the console for example:
wkhtmltopdf http://google.com google.pdf
it works fine and generates pdf.
But when I try to generate pdf in my controller, then it throws this exception:
RuntimeException
The file
'/home/alemil/Projects/test/storage/app/annual_reports/Pdf test.pdf' was not created
(command: /usr/local/bin/wkhtmltopdf --lowquality --orientation
'landscape' --page-size 'a3'
'/tmp/knp_snappy5bb49cd6bfcf14.16728595.html'
'/home/alemil/Projects/test/storage/app/annual_reports/Pdf test.pdf').
I tried to copy command above and execute directly in console. This is what I get:
Loading pages (1/6)
Error: Failed to load http://tmp/knp_snappy5bb49cd6bfcf14.16728595.html, with network status code 3 and http status code 0 - Host tmp not found
Error: Failed loading page http:///tmp/knp_snappy5bb49cd6bfcf14.16728595.html (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1 due to network error: HostNotFoundError
As I can see, it tries to fetch local file as it is remote. I couldn't find this issue so I am asking if someone can solve this or has any idea.
My environment is Ubuntu 18.04 PHP and PHP 7.2.10.
This is my code:
// app.php aliases
'PDF' => Barryvdh\Snappy\Facades\SnappyPdf::class,
// controller code
PDF::loadView('admin.reports.annual.pdf_template', $data)
->setPaper('a3', 'landscape')
->save(
storage_path('app/annual_reports/') . $report->pdf,
true // when there is file with same name it throws file already exists so I had to set owerwrite to true
);
Also, one more thing to point out, I was using barryvdh/laravel-dompdf and everything was working fine except it cannot render charts so I'm now trying to set up snappy.
In my case the problem was that I passed null in $html to Knp\Snappy\Pdf::getOutputFromHtml($html). Under the hood it cheks if $html !== null and creates a temporary file for further PDF generating. If temporary file doesn't exist wkhtmltopdf throws error like described in question.
hmm
I think (not sure) that there are a a blocking in your application that blocks your controller from getting to the link. Or at the creating of the PDF.
It can also be your code syntax.
you can try this for html :
$pdf = new Pdf('/usr/local/bin/wkhtmltopdf');
$pdf->generateFromHtml('<h1>test</h1><p>test from test</p>', '/tmp/test-123.pdf');
When I try to use ftp_delete() I get the error: Warning: ftp_delete(): File not found. My ftp server should be working fine, I can upload files fine to the same directory without a problem. Here is the PHP:
$fileSource = 'http://localhost/user/images/dfdf.png';
$ftpCon = ftp_connect('localhost');
ftp_login($ftpCon,'---','---');
ftp_delete($ftpCon,$fileSource);
Also when I look at the server logs I can see I get the message: 550 File not found
The url for $fileSource is the file's exact path, I went into localhost and copy/pasted it into the code, still for some reason it can't be found.
The ftp_delete function acccepts a path to the file. You are providing http://localhost/user/images/dfdf.png which contains the host (http://localhost/). I think you mean to provide /user/images/dfdf.png, which is just the path.
Example:
ftp_delete($ftpCon, '/user/images/dfdf.png');
When, for example, your FTP root is /user/, it will mean you need to provide ftp_delete with /images/dfdf.png. Thanks to #maremp.