Error running wkhtmltopdf knp snappy bundle - symfony

I am using knp snappy bundle in symfony 2 ,when I tried this code below i get the error "The exit status code '127' says something went wrong:
stderr: "sh: 1: /usr/local/bin/wkhtmltopdf: not found
"
stdout: ""
command: /usr/local/bin/wkhtmltopdf --lowquality '/tmp/knp_snappy5730ffc6454157.04526188.html' '/tmp/knp_snappy5730ffc6454549.48294109.pdf'."
public function showtestAction()
{
$html = $this->renderView('CondorsTnMallBundle:SimpleVisitor:test.html.twig');
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
}

The error message is quite clear: /usr/local/bin/wkhtmltopdf does not exist
First of all you need to know where wkhtmltopdf is in your filesystem. Then, you can add to your config.yml file these lines:
knp_snappy:
pdf:
enabled: true
binary: /real/path/to/your/file/wkhtmltopdf
options:
print-media-type: true

This works for me, just rename wkhtmltopdf-amd64 to wkhtmltopdf. Test older versions too.
knp_snappy:
pdf:
enabled: true
binary: /real/path/to/your/file/wkhtmltopdf
options:
print-media-type: true

Related

Deploy Symfony 4 application but composer has "cache:clear error"

I try to deploy my Symfony application and when I use composer to install dependencies, I get an error.
I saw that there were people who had the same problem but I didn' t find a solution. Like change in .env APP_ENV=dev to APP_ENV=prod.
Here the logs :
Executing script cache:clear [KO]
[KO]
Script cache:clear returned with error code 255
!! PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "DoctrineFixturesBundle" from namespace "Doctrine\Bundle\FixturesBundle".
!! Did you forget a "use" statement for another namespace? in /tmp/build.YZSXu7Ir2b/src/Kernel.php:23
I use DoctrineFixturesBundle only in dev environment :
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.1",
"fzaninotto/faker": "^1.8",
"symfony/profiler-pack": "^1.0"
}
Here my config/bundles.php :
...
Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle::class => ['dev' => true, 'test' => true],
...
And here Line 23 of Kernel.php :
...
public function registerBundles(): iterable
{
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if ($envs[$this->environment] ?? $envs['all'] ?? false) {
yield new $class(); // line 23
}
}
}
...
if I understand your problem, you just have Composer Install, but the best is to do that composer install --no-dev --optimize-autoloader it's explained Here. Otherwise, I do not know
Please check if directory "vendor/doctrine/doctrine-fixtures-bundle" exists and if there are files inside - specifically the "vendor/doctrine/doctrine-fixtures-bundle/DoctrineFixturesBundle.php"
If they are not there then it seems you didnt executed "composer update" to add the package. In such case please try:
COMPOSER_MEMORY_LIMIT=-1 composer update doctrine/doctrine-fixtures-bundle
You should see something like:
- Installing doctrine/data-fixtures (v1.3.1): Loading from cache
- Installing doctrine/doctrine-fixtures-bundle (3.2.2): Loading from cache

Drush 9 sql:sync error: no database record could be found for source #local

I just installed a new Drupal 8 and it comes with Drush 9, and I was trying to set up everything to use the command sql:sync or config:pull to have my local and staging syncronized but everytime I run the command 'drush sql:sync #local #stage' I get the following error
In SqlSyncCommands.php line 86:
[Exception] Error: no database record could be found for source
#local
Here's my self.site.yml
local:
host: localhost
user: root
root: /var/www/html/new-drupal
uri: http://new-drupal.local
stage:
host: XX.XX.XX.XX
user: root
root: /var/www/html/new-drupal
uri: http://new-drupal.my-staging.com
Searching around I found old versions of the aliases file that included this option:
'databases' =>
array (
'default' =>
array (
'default' =>
array (
'driver' => 'mysql',
'username' => 'sqlusername',
'password' => 'sqlpassword',
'port' => '',
'host' => 'localhost',
'database' => 'sqldbname',
),
),
),
But checking the example file of the 9 version there's nothing about it: https://github.com/drush-ops/drush/blob/master/examples/example.site.yml
Did someone had the same problem and found a solution?
It's been a while since my conversion. I had similar issues and went with a least-configuration approach.
First, compare the results of the following commands to see if it exposes any problems:
drush site:alias
drush site:alias #self
drush site:alias #local
I don't have a self.site.yml file. I've named mine drush/sites/remote.site.yml and only have remote aliases in it. I do not define a #local alias and use the built-in #self alias instead.
In drush/drush.yml I have:
drush:
paths:
config:
- ../docroot/sites/default/local.drush.yml
In docroot/sites/default/local.drush.yml I only have
options:
uri: 'http://localhost:port'
With this configuration the results of drush site:alias #self is
'#self':
root: /var/www/docroot
uri: 'http://localhost:port'
And assuming I have a valid #stage alias in drush/sites/remote.site.yml I would just run
drush sql:sync #self #stage
If you want a #local alias maybe you should move it over to the local drush config instead.

Option footer-html returning error when used within KnpSnappyBundle

I'm trying to build invoice pdf with wkhtmltopdf library and a symfony2 wrapper KnpSnappyBundle.
I want to add a footer on every page with the wkhtmltopdf option footer-html. In command-line, everything works fine, my pdf is built and a footer is repeated on every page. But when I use the bundle service :
$snappy = $this->get('knp_snappy.pdf');
$snappy->setOption('footer-html', 'http://www.google.com');
$content = $snappy->getOutputFromHtml($html);
return new Response(
$content,
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
it returns an error :
The exit status code '1' says something went wrong:
stderr: "Loading pages (1/6)
[> ] 0%
[======> ] 10%
Error: Failed loading page http://www.google.com (sometimes it will work just to ignore this error with --load-error-handling ignore)
Exit with code 1 due to network error: HostNotFoundError
"
stdout: ""
command: C:\wkhtmltopdf\bin\wkhtmltopdf.exe --lowquality --dpi "300" --margin-bottom "0" --margin-left "0" --margin-right "0" --margin-top "0" --page-size "A4" --background --encoding "utf-8" --footer-html "http://www.google.com" "http://www.google.com" "test.pdf".
Did any already succeed in using footer-html option with KnpSnappyBundle ?
I think the problem is with this line:
$snappy->setOption('footer-html', 'http://www.google.com');
The second argument in the setOption() method is looking for a html file on the local filesystem. As it is written currently it would try to include the Google homepage as a footer on every page of your pdf. Try something like this instead:
$snappy->setOption('footer-html', 'path/to/footer.html');
$snappy->getOutput('https://google.com');
Be aware that the .html is important, non-standard extension names like .phtml will cause errors.

Symfony2 command run cache:clear -e prod

I am trying to write symfony2 command that runs few other command.
But when I run
$this->getApplication()->find('cache:clear')->run(new ArrayInput(array(
'command' => 'cache:clear',
'-e' => 'prod'
)), $output);
$output->writeln('Done clearing cache prod!');
it it ignores '-e' => 'prod' parameters giving:
Clearing the cache for the dev environment with debug true
instead of:
Clearing the cache for the prod environment with debug false
Try removing the 'command' => 'cache:clear', since you are already running on that Command you have found from the Application, so you don't need to specify it again.
$this->getApplication()->find('cache:clear')->run(new ArrayInput(array(
'-e' => 'prod'
)), $output);

Knp Snappy Bundle Options

I'm using KnpSnappyBundle for my Symfony project and I'm trying to define some options in my config file. However if I set, for example:
knp_snappy:
pdf:
enabled: true
binary: /usr/local/bin/wkhtmltopdf
options: [ 'no-outline' ]
I get an error:
The option '0' does not exist.
How do I define options for the bundle?
Well, that was kinda dumb of me. YAML to the answer:
knp_snappy:
pdf:
enabled: true
binary: /usr/local/bin/wkhtmltopdf
options:
no-outline: true
The answer is:
knp_snappy:
pdf:
enabled: true
binary: /path/to/wkhtmltopdf
options:
- { name: 'page-size', value: 'A4' }

Resources