Chapters
31 Chapters
|
3:19:40
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
16.
Lets Generate a PDF!
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial is built on Symfony 4.3, but will work well with Symfony 4.4 or 5.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"aws/aws-sdk-php": "^3.87", // 3.110.11
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.1
"doctrine/doctrine-bundle": "^1.6.10", // 1.11.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.1
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-snappy-bundle": "^1.6", // v1.6.0
"knplabs/knp-time-bundle": "^1.8", // v1.9.1
"league/flysystem-aws-s3-v3": "^1.0", // 1.0.23
"league/flysystem-cached-adapter": "^1.0", // 1.0.9
"league/html-to-markdown": "^4.8", // 4.8.2
"liip/imagine-bundle": "^2.1", // 2.1.0
"nexylan/slack-bundle": "^2.1,<2.2.0", // v2.1.0
"oneup/flysystem-bundle": "^3.0", // 3.1.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.1
"sensio/framework-extra-bundle": "^5.1", // v5.4.1
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.3.4
"symfony/console": "^4.0", // v4.3.4
"symfony/flex": "^1.9", // v1.21.6
"symfony/form": "^4.0", // v4.3.4
"symfony/framework-bundle": "^4.0", // v4.3.4
"symfony/mailer": "4.3.*", // v4.3.4
"symfony/messenger": "4.3.*", // v4.3.4
"symfony/property-access": "4.3.*", // v4.3.4
"symfony/property-info": "4.3.*", // v4.3.4
"symfony/security-bundle": "^4.0", // v4.3.4
"symfony/sendgrid-mailer": "4.3.*", // v4.3.4
"symfony/serializer": "4.3.*", // v4.3.4
"symfony/twig-bundle": "^4.0", // v4.3.4
"symfony/validator": "^4.0", // v4.3.4
"symfony/web-server-bundle": "^4.0", // v4.3.4
"symfony/webpack-encore-bundle": "^1.4", // v1.6.2
"symfony/yaml": "^4.0", // v4.3.4
"twig/cssinliner-extra": "^2.12", // v2.12.0
"twig/extensions": "^1.5", // v1.5.4
"twig/extra-bundle": "^2.12|^3.0", // v2.12.1
"twig/inky-extra": "^2.12", // v2.12.0
"twig/twig": "^2.12|^3.0" // v2.13.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.2.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/browser-kit": "4.3.*", // v4.3.5
"symfony/debug-bundle": "^3.3|^4.0", // v4.3.4
"symfony/dotenv": "^4.0", // v4.3.4
"symfony/maker-bundle": "^1.0", // v1.13.0
"symfony/monolog-bundle": "^3.0", // v3.4.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.3.4
"symfony/stopwatch": "4.3.*", // v4.3.4
"symfony/var-dumper": "^3.3|^4.0", // v4.3.4
"symfony/web-profiler-bundle": "4.3.*" // v4.3.4
}
}
25 Comments
When running the console command to send the PDFs, I was getting the error: "Failed to load about:blank, with network status code 301 and http status code 0 - Protocol "about" is unknown". It seems to be related to https links being accessed during the rendering of the PDF (my local site uses https).
I found that adding:
$this->pdf->setOption("enable-local-file-access", true);right before the line that contains:
$pdf = $this->pdf->getOutputFromHtml($html);solved the problem!
I hope this helps anyone else that runs in the same problem. And a huge thanks to Ryan and Victor for these great tutorials!!
Thanks a lot! You solved my problem.
Hey Mark,
Hm, not sure about this, we also use Wkhtmltopdf on SymfonyCasts that works only by HTTPS, and we don't have any problems with generating PDFs.
I noticed 301 status code in the error you mentioned, probably somewhere you're referencing to HTTP but your server/website redirects to HTTPS? I'd recommend you to fix all your links to be HTTPS, then no redirects will be set and PDF generation should work fine even wihtout "enable-local-file-access" I think.
I hope this helps. Anyway, I'm glad you found a working solution for yourself. And thank you for sharing the solution with others!
Cheers!
Figured out my problem
Hey Brandon
I'm glad to hear it. If you don't mind could you please tell us how you fixed your problem so others can benefit from your answer?
Cheers!
Hi there.
If you are using the Dompdf library (like me), and it outputs the following message:
No block-level parent found. Not good.at the end of the foreach (inside its body), you should write the following lines of code:
Hey AbelardoLG,
Hm, this sounds like a workaround: you kill the old instance and create a new one. Sounds like a bug? Anyway, thank you for sharing this tip with others.
Cheers!
I think so.
My pleasure!
Brs.
Hello,
I was trying the exact same but have the following error:
In
DefinitionErrorExceptionPass.phpline 54:I have installed "composer require knplabs/knp-snappy"
But errors remain the same.
when i try to see "debug:autowiring pdf"
It does not show Knp\Snappy\PDF. What is the solution?
I am using contao 4.12 (which is with symfony 5)
Help will be appreciated!
Hey Rana,
I believe you installed the wrong library, you need to install the bundle as mentioned in this chapter https://symfonycasts.com/screencast/mailer/snappy-wkhtmltopdf
Cheers!
To me
{{ encore_entry_link_tags('app') }}
in
author-weekly-report-pdf.html.twig
generated an error when calling php bin/console app:author-weekly-report:send :
exit with code 1 due to network error: Protocolunknownerror .
I found , after lot of search and testings , that the pb was from twig file and I changed it to
{% for path in encore_entry_css_files('app') %}
<link rel="stylesheet" href="{{ absolute_url(path) }}">
{% endfor %}
Now email are sending correctly and without error.
Maybe can be usefull to someone this information.
Hey Stefan,
Hm, that encore_entry_link_tags() function should be valid, that's weird. But I'm happy you were able to find a workaround by manually iterating and render CSS files with encore_entry_css_files(), well done! And thanks for sharing it with others!
Most probably that problem is coming from the Wkhtmkltopdf - it might require absolute URL instead of just a path without the host, probably might depend on its version.
Cheers!
Hi,
Do you know any working Wkhtmltopdf alternative? Such as Weasyprint?
My sysadmin that uses the gentoo linux distribution told me that Wkhtmltopdf is being deprecated because of the qtwebkit dependency, which seems to not be maintained anymore ( https://bugs.gentoo.org/692532 ).
Would be very thankful if someone can share some knowledge about this matter.
Thanks a lot!
Hey Samuel,
We do use Wkhtmltopdf for generating PDFs on SymfonyCasts and it works good for us. Not sure about the deprecated dependency, but it's an issue that should be workaround by Wkhtmltopdf I suppose. Anyway, I don't see any information that the Wkhtmltopdf tool is deprecated, the latest stable release was 0.12.6 on June 11, 2020.
I personally do not have any experience with alternatives to Wkhtmltopdf tools so can't help with this, but it should be easy to find some similar projects on GitHub I suppose. From my side, I can say that Weasyprint sounds interesting, also you could take a look at some others:
- https://github.com/dompdf/d...
- https://github.com/MrRio/jsPDF
- https://github.com/RelaxedJ...
2 latest seems JS solutions, I hope this helps!
Cheers!
Hi Victor,
Sorry for the late response.
We finally used Weasyprint, was easy to implement and can generate pdfs on the fly without creating temporary files.
We are thinking about creating a small vendor bundle wrapping a few command line parameters. If so, I'll let you know for your info.
Thanks a lot!
Hey Samuel,
No problem! Thank you for letting us know what you chose. Fairly speaking, I haven't worked with Weasyprint yet, but that sounds really interesting! And yeah, having a bundle that will make the integration even smoother is awesome, good luck with it! If you could share the link to the bundle later - it would be useful I think ;)
Cheers!
Hi,
I'm using Docker and, for some reason, I get a blank pdf.
I made a public repo should anyone would be so nice to look into it.
https://bitbucket.org/j_bon...
To get the project running, you could fire run.sh on the root directory or alternatively running the commands inside that script manually.
I would really like to get this working.
Best,
- J
Hey Julien,
I'd recommend you to "ssh" into that container, i.e. where you have Wkhtmltopdf installed, and run it manually. You can try to run the default command recommended by Wkhtmltopdf to see if it works as expected:
wkhtmltopdf http://google.com google.pdf
Then try to look into that google.pdf - do you see a Google start page PDF-ed? Also, look closer to the output of that command, it may contain some tips or errors if something is missing.
I hope this helps!
Cheers!
Hi Victor,
I figured it out, I was missing xvbf package. I added it and voilĂ !
Thanks
Hey Julien,
Nice! Thanks for sharing your solution with others!
Cheers!
I get an error:
Notice: Object of class Symfony\Component\HttpFoundation\Response could not be converted to number
$html = $this->twig-$this->render('daily_report/showdailypdf.html.twig', [
'daily' => $dailyreport <-----ERROR HERE
]);
$pdf = $this->pdf->getOutputFromHtml($html);
Any thoughts?
Hey Brandon!
Oh wow That's an interesting error :). I think I see the error - and it's SO subtle!
See the difference? The "+" line is the one I've corrected - you were missing a tiny
>, which made it look like you were trying to do subtraction (hence the "converted to number" error).Let me know if this fixes it :).
Cheers!
For Windows 10 Users (if there are others besides me :D):
I had to move the installation of the wkhtmltopdf from Program Files to C:/ so there are not any spaces in the path eg: "Program FIles"
My .env config:
###> knplabs/knp-snappy-bundle ###<br />WKHTMLTOPDF_PATH='C:\wkhtmltopdf\bin\wkhtmltopdf.exe'<br />WKHTMLTOIMAGE_PATH='C:\wkhtmltopdf\bin\wkhtmltoimage.exe'<br />###< knplabs/knp-snappy-bundle ###It's not needed, you can achieve this by treating C:\Program Files directory in the old MS-DOS wa, where there was 8 character limit per folder/file
so C:\Program Files becomes "C:\Progra~1"
Here's the source (i've checked it on my windows 10 and it works)
https://superuser.com/quest...
```
Note for Windows users on 64-bit systems
Progra~1 = 'Program Files'
Progra~2 = 'Program Files(x86)'
```
Oh boy, Windows been Windows... Thanks for sharing it!
"Houston: no signs of life"
Start the conversation!