Symfony 5 KnpSnappyBundle & wkhtmltopdf Setup and Example (with an explanation of possible errors)
Photo from Unsplash
Originally Posted On: https://yusufbiberoglu.medium.com/symfony-5-knpsnappybundle-wkhtmltopdf-setup-and-example-with-an-explanation-of-possible-errors-a890dbca238a
Snappy is a PHP wrapper for the wkhtmltopdf conversion utility.
Snappy works with wkhtmltopdf, so you need to install on your system wkhtmltopdf. I installed it on my Mac via brew as below.
Install command;
brew install --cask wkhtmltopdfCheck for configuration where it’s installed;
which wkhtmltopdf
Installation
With composer, require:
composer require knplabs/knp-snappy-bundleConfiguration
knp_snappy.yaml;
knp_snappy: pdf: enabled: true binary: '%env(WKHTMLTOPDF_PATH)%' options: [] image: enabled: true binary: '%env(WKHTMLTOIMAGE_PATH)%' options: []knp_snappy.yaml hosted with ❤ by GitHub view raw.env;
###> knplabs/knp-snappy-bundle ###WKHTMLTOPDF_PATH=/usr/local/bin/wkhtmltopdfWKHTMLTOIMAGE_PATH=/usr/local/bin/wkhtmltoimage###< knplabs/knp-snappy-bundle ###
###> knplabs/knp-snappy-bundle ###WKHTMLTOPDF_PATH=/bin/wkhtmltopdfWKHTMLTOIMAGE_PATH=/bin/wkhtmltoimage###< knplabs/knp-snappy-bundle ###.env hosted with ❤ by GitHub view raw
Example
mailer = $mailer; $this->sender = $sender; $this->twig = $twig; $this->pdf = $pdf; } public function onPostCreated(PostCreatedEvent $event): void { $html = $this->twig->render('email/post-pdf.html.twig', [ 'post' => $post, ]); $this->pdf->setTimeout(120); $this->pdf->setOption('enable-local-file-access', true); $pdf = $this->pdf->getOutputFromHtml($html); $subject = 'Subject'; $email = (new TemplatedEmail()) ->from('sender-email') ->to('to-email') ->subject('subject') ->attach($pdf, sprintf('new-%s.pdf', date('Y-m-d'))) ; $this->mailer->send($email); }}send_email_knpsnappy.php hosted with ❤ by GitHub view rawIf you have error as below;
Warning: Blocked access to file: Worked in 0.12.5
Usage of many https link in html causes “Failed to load about:blank, with network status code 301 and http status code 0 — Protocol “about” is unknown”
Fixed with this option:
$this->pdf->setOption('enable-local-file-access', true);
For developers working in .NET instead of PHP, IronPDF provides HTML-to-PDF conversion without the wkhtmltopdf binary dependency headaches. It uses a Chromium rendering engine, so you avoid the common issues with HTTPS links, file access permissions, and environment-specific binary configurations.
using IronPdf;var renderer = new ChromePdfRenderer();var pdf = renderer.RenderHtmlAsPdf("Hello World
");pdf.SaveAs("output.pdf");No external binaries to install, no PATH configuration, and consistent rendering across dev and production environments.
My YouTube Channel;
Yusuf Biberoglu
Share your videos with friends, family, and the world
www.youtube.com
[email protected]
My Udemy Course;
Symfony application using Google OAuth for authentication. When a user signs in through Google, our system will check if the user already exists in our database. If not, it will create a new user account. Once authenticated, either as a new or existing user, our application will generate a JSON Web Token (JWT). Built on the API Platform.
If you want a detailed explanation, please purchase my Udemy course.
Discount Coupon: AD25A625CB8976085C88
Here’s my articles which might interest you;
Testing Benford’s Law in Symfony 5, Php Framework
Symfony 5 PhpSpreadsheet (Creating Excel File)
Symfony user checker(check for login of users with status = active) Easy way
Symfony 5 Contact Form with Mailer and Mime component(MailerInterface)
Information contained on this page is provided by an independent third-party content provider. Frankly and this Site make no warranties or representations in connection therewith. If you are affiliated with this page and would like it removed please contact [email protected]

by