Thank you for sharing this relatively simple trick Nick. Here's how I do it whenever business requires pdfs: I just tell them to prepare the templates as excel or word documents and then proceed to use NPOI to populate the data, and after that use LibreOffice headless (sofice executable) to convert to pdf. It's not the fastest or most accurate (especially if you have intricate formatting documents), but hey...it's free and it also works on Linux.
I have been looking for this for a while. Mainly for toying around and make a POC for some things that my boss won't ok, until he has a POC. Chances are it will even be enough for what we want to do. So if I say "Hey, no library to pay for", he'll be happy. Although, if we do end up needing a library that isn't too expensive, I can probably convince him, with some good arguments added to the POC. Thank you so much for pointing me in a feasable direction.
I mean, the whole point of .NET Core was to break it up into a bunch of smaller packages instead of one giant library. Everything he installed was from Microsoft. It was pretty clear, as he kept mentioning IronPDF, that he meant third party libraries, and specifically ones that require a paid license.
Keep in mind this will be pretty memory intensive though. For real production environments you will need something light that can also scale well. Consider using a pure library for real world scenarios.
Since this and puppeteer load chromium it is memory intensive, but if you use it as a singleton and just instantiate playwrite once then it should be fine. But as soon as you want pdfs that are editable, contain fields or you need digital signature capability, yeah this is not gonna cut it at all, and that is when you will start paying a lot of money for a bigger library (syncfusion has one of the best licensing as far as I can tell since the community version gives you a free license until you hit like a 2million $ profit margin or something like that)
I use the built in PrintDocument() set its PrinterSettings object to use Microsoft Print to PDF driver (this does have the drawback of creating a temporary file though). Setting up the page is a bit tricker than using html converters, but easy enough and I have complete control of the whole process.
we're using puppeteer to call our frontend page by passing the access token in the query string. the advantage we have is the frontend team design the required document using the tools they already familiar with the disadvantages is the complex setup and depending on the server to have chromium and so on
All these "HTML To PDF" libraries suck. They all package the Blink/Chrome browser in the nuget package, so it's huge. They use the "print to pdf" implemented by blink. Needless to say, it is extremely memory and CPU intensive. Most of us will need to create thousands if not hundreds of thousands of PDFs -- This library will bring your entire server to a crawl, and you'll be paying out the you-know-what in CPU usage. Just buy a real PDF library. The cost will be a wash. Yes, this is a free alternative to these libraries, but that will be the least of your problems if you need to do real work.
At this point, just run a cmd comand to use msedge print-to-pdf functionality, just serve your html to it and that’s it. Msedge comes default with windows, so no other dependencies needed.
I was coming to say the same: at my work we had to evaluate different libs (both free and paid) and ended up deciding for QuestPdf because it was way simpler and much better performant than any of these BS html-to-pdf libs that have all the cons you mentioned. We evaluated Playwright and IronPdf as well and both ended up in the trash can. At some point companies have to decide to spend some money and in the context of big projects, it really is not worthy to cut down expenses on these things which can really have an impact.
@@CesarDemi81 This is almost the solution i ended up with - I use Xceed Words to generate a document (i also need to be able to save out a Word version of the PDF), use WebView2 (Edge but really Chromium under the hood) to preview the PDF in the (desktop) app - which you get for free, and then have a SaveAs word function - I have yet to find a nice solution to this - even with expensive paid libraries there always seems to be some 'hacking' involved to have a nice document output functionality
If you want good looking, easy to maintain PDFs, using HTML templates is an excellent approach. But you will always need a browser engine if you want to use the latest CSS possibilities.
Not sure I like using Chromium to do this, but using the Razor engine to render components/models into HTML strings is brilliant, I did not think of that. I'm already creating PDFs from manually constructed HTML strings using SelectPdf. I might incorporate the Razor engine for templating. Thanks!
Packaged the same approach in the BlazorReports library, but more for a separate report server. This approach works great as you can leverage Blazor and other Js libraries.
Do you have any experience how this approach deals with header/footer logic, especially one time headers followed by a different repeating header? Same for footers? Also tables across multiple pages with and without their own repeating header? Those were topics previous approaches I saw to PDF generation from HTML struggled with.
@@theCybertier It's definitely a limitation, it's not hard to include repeating headers or footers but when you need to add logic to them it gets harder. Will try to see if I can get a usable example on the repo if it's viable.
I used to use Office apps to print to a Postscript file, then had a script that would package the Postscripts as a PDF. It was free, in that my employer used Office for everything but printing to Postscripts isn't exclusive to Microsoft products and is completely free as long as you have a Postscript printer installed.
I had to develop a project with PDF reports, and what I did is, I created a TeX template with labels like __name__, __date__, etc. So those labels were replaced with code and the result was converted to PDF. I even included some graphs with TeX libraries. The final software had to bundle the TeX distribution (MiKTeX if I recall correctly).
If you need a proper publishing infrastructure (page layouts, footnotes, overflow options), then I’ve used XSLFO for years. Similar to HTML markup with CSS. I use a simple paid library called Ibex to render it.
Hello, very information video, I have one question. please answer my question I have a microservices architecture with 5 services, 2 of them send emails, so have different html dynamic templates in email, and other three have pdfs, .. So if i create a 6th service of blazor project, and then send requests from other services to blazor to create pdf or html (as you are showing on video), and then can download or send on email. is that a good practice? I mean at the same time blazor will be responsible for creating email template like password reset email or otp email, and also responsible as a service to send emails and print pdfs.
I/we did the same but instead of having that in the app, i did an API, you sent it a link (of the invoice) and the API returns the pdf. No overhead of installing playright/chromium on all servers, just the ones dedicated to this api
Having worked on creating pdf muliple times, the way we had done without pdf library was when the content was created in our appliction. If we have conver a content created by another application, that is challenging. And the PDF library based on browsers seems to be the only way. The main reason being bowsers allow lot of errors in html. The best way to extend the capability was to use a flow document to have the ability to fixed documet and then to pdf, which unfortunately Ms choose not to do. Another prospective way is to extend the print to pdf, which also can be done to a good extent, but the libraries that i explored had one issue or another.
I have used libraries that allow me to dynamically compile a template (int Razor syntax) that I had fetched from the database. I wanted to move over to the Razor components engine but since it lacks that support it is impossible. You kind of have to compile the templates into the project. Unless you compile components into a separate assembly, store it somewhere, and then load it when you generate the invoice.
I love things like this. Definitely saving this in case I need to create any PDFs. Getting my employer to shell out money for libraries is next to impossible :P
Some years ago I used PDFlib to generate PDFs based on a template document and dynamically populated field values. I think it was a fairly expensive library and probably overkill in many cases, but it worked flawlessly, was extremely scalable, and could do pretty much anything with PDF files.
I wish I knew this about 3 years ago 🤣 Next time I'll do it like this, but I will isolate it as a service in a container and send to an storage like s3
I really like this approach. I wonder if there is a way to generate the PDF using streams. For example, when there is a large amount of data that requires writing the file while the data arrives. Also, when there are many files queued to be generated.
We currently use the Syncfusion library. It does the job but I have been tempted to pivot to Playwright to save $$$. The thing I can’t see this approach doing is document assembly i.e. taking multiple PDF and merging them into one PDF with page numbering etc.
Hey Nick, I love your videos and I'd have a wish on a topic. Could you please make a video about best practices on pull-requests, focussing not on the self-explanatory branch policies, but more on the perspectives of author and reviewer? Reason: We had a couple of "fights" in our company because of comments on pull-requests. We had these issues on multiple products, involving multiple developers and I feel like it's a systematic issue that must be addressed, before things get out of hand. I highly doubt that we are the only ones struggling with this, since pull-requests are a devops essential. And I am pretty sure, that someone like you, with an own "code-cop" series, has some neat hints on how to improve as both, author and reviewer, to keep such things from happening. And even if not, it would be great if you could share your experiences with this, cause then this community will very likely be able to provide decent advice.
A lot of people are commenting that this would crush server performance and/or cost a ton of money. That assumes the reports are being generated on the server and that you are in the cloud. In some scenarios that is unavoidable. But many other scenarios this solution would work well. For Desktop apps, this would be great (can everything Nick did can be baked into the desktop app and just send the data to the client and have them render it?). With the web, well, you are already rendering to HTML, so you can just have the client export to pdf from his/her browser. So really the only scenario where this would be an issue is if you were a company offering a pdf-generating service to clients. Then you do have the possibility of millions of pdfs being generated in a short amount of time (if you have a large client base). But if you are a company generating invoices for your clients in-house, then this would absolutely be a viable solution.
The point of PDFs is that you can lock editing and sign it, which is crucial for invoices or other legal documents. You lose that when you ask client to export the html page to a pdf, them being free to edit any fields before doing so.
Nice, I like it. On desktop apps, I use WPF with Xaml in code to print to the Microsoft PDF driver. I've created some very complicated products with drawing and graphing with complete pagination.
Hi Nick, trying to convert a Html to pdf. When done via Chrome browser it's perfect, all the images and page breaks are perfect, I need to do it via azure function, please advise
Weird on the timing of this.... I was just adding save to PDF support in a WPF application I'm developing for a client. Unfortunately I can't see a way to integrate this solution into a WPF application.
In a WPF application I developed years ago, I created XPS files and called GhostScript to convert them to PDF. Has the advantage that, for creating XPS, I can re-use all WPF UI components.
@@coloresfelices7 ok. I'm sort of doing the same. Rendering WPF Visual element into XPS format, then using PDFSharp to convert that to PDF. The problem is the Visual element is a scroll viewer and I want to have it render the entire content not just what's visible. I need to do more research...
I use just html file with scriban for templating to replace the placeholders. Then IronPDF for the pdf generation. It works well, but I want to try this now.
seems like it would be easy for me to swap out IronPDF and try this since I already have a string for the html that I pass to IronPDF. I'll report back if it works. The html has chartjs-umd in it and some data points to make some charts. Should be interesting to see if that works with Playwright
Playwrite and gotenberg both look like they use headless chromium to create PDFs. You just containerize playwrite and have it basically be the same thing
How about PDFSharp & MigraDoc by empira Software GmbH, Germany It is open source under MIT licence Edit: they are still working on it, but basic functions didn't changed for years so you can AI simple PDF quickly
Same. I generate statements for a company using PuppeteerSharp and create and email almost 300 files in less than 4 minutes. Some of the querying and emailing also takes time so... do the math. Not a bad deal for me.
This is actually a very interesting approach to be able to do SSG with Blazor! Experimented with it before and although it's rudimentary; it does the trick. I did not think of the use case of creating pdfs though, very interesting!
This is a pretty cool solution (i love using the Razor html renderer in a non browser app - already got me thinking of other uses for that), although this solution is also pretty 'expensive' because of the dependency on Playwright which means including Chromium etc which are pretty large dependencies. Personally i use Xceed Words to generate documents Word/PDF which isn't as nice in terms of the code needed to create the document, but a bit more lightweight (and very cheap compared to most of the paid document generation libraries).
@@TewodrosAAssefa you can make it pretty efficient by skipping play right and controlling the chrome instances directly. You can make it so that you stream the report from the chrome instance to the Response body without having to buffer the entire pdf in memory using pipes. Been generating tens of thousands of reports daily on a container with 1 vCPU AND 1GB of RAM.
@@gabynevada This sounds great. I try to find out how this works. I save the Chromium binaries on my device. Then I probably start with ChromiumDirectory = "C:\Users\Me\DotNetBrowser" and create the EngineFactory. What is the key-line to get the pdf finally in the Response body?
@@ВладиславДараган-ш3ф right now other forms of generating PDFs are very limited, if you need the flexibility then a browser is one of the best ways to do it. Hopefully other options will keep growing and we can avoid it in the future.
Reminds me of the time i serializeda object to json to then transform said json into xml becuase the xml library did not support DateOnly and TimeOnly structs
I had a really hard time containerizing libraries that convert HTML code to PDF because they usually rely on Windows packages. Can this one run on Linux?
We have done a very similar thing to Nick's shown here, and it has been running in production for a while now on the smallest Linux Azure Container App they have.
You don't. You just need a way to use the Razor view engine to generate HTML as a string from Razor views. It works equally well with MVC, I've been using it for 10+ years in production with the IBEX xsl-fo processor (paid for, equivalent to Apache fop)
The problem that I have with generating PDFs from HTML is that I want each and every page of the document to contain a caption at the top of the page, regardless of the length of the document. How would I reach that goal with a HTML based PDF generator like Nick's solution?
The other problem with HTML to PDF is pixel-perfect rendering for intricate work (such as exact positioning on pre-cut labels) and bleeds. You do not always need pixel-perfect rendering, but when you do, HTML just does not cut it.
If you specify the Content-Type for a PDF or XLS, or whatever, then stream (Response.Write) the content you want in your file (for an XLS spreadsheet, just stream out a ), after the browser has received the file data, you can go File, SaveAs, and that file type will show as an optional file format, assuming your operating system has that file type registered as a known file type. There is no overhead on the server, and the tiny bit of processing done is done on the client.
ClosedXml might be the best one right now to generate Excel. EPPlus is no longer free. NPOI seems to be used a lot, but if you go back in the history, it was at some point filled with political statements (both on NuGet and GitHub), so I am not sure if I would trust that one.
Well, doesn't matter what I was using. I will start using this approach in small and simple projects from now on. Amazing! Thanks for sharing.
wow, amazing timing, I just explored ways to create pdfs literally yesterday
No library. installed playwright ....
I guess Nick meant that there is no third party library being used, it's all Microsoft.
Thank you for sharing this relatively simple trick Nick. Here's how I do it whenever business requires pdfs: I just tell them to prepare the templates as excel or word documents and then proceed to use NPOI to populate the data, and after that use LibreOffice headless (sofice executable) to convert to pdf. It's not the fastest or most accurate (especially if you have intricate formatting documents), but hey...it's free and it also works on Linux.
I have been looking for this for a while. Mainly for toying around and make a POC for some things that my boss won't ok, until he has a POC.
Chances are it will even be enough for what we want to do. So if I say "Hey, no library to pay for", he'll be happy. Although, if we do end up needing a library that isn't too expensive, I can probably convince him, with some good arguments added to the POC.
Thank you so much for pointing me in a feasable direction.
> No libraries, no nothing
> Installs a bunch of packages
...runs a script to install browser binaries on the machine
ye should change it to no paid libraries.
@@mrx10001 And so he did
I mean, the whole point of .NET Core was to break it up into a bunch of smaller packages instead of one giant library. Everything he installed was from Microsoft. It was pretty clear, as he kept mentioning IronPDF, that he meant third party libraries, and specifically ones that require a paid license.
@@justinian.erdmier "I meeeeeannn"
Keep in mind this will be pretty memory intensive though. For real production environments you will need something light that can also scale well. Consider using a pure library for real world scenarios.
We are not generating PDFs in a O^2 cycle
Can you suggest me one?
This is exactly what I was thinking. This is VERY heavy.
@@leonardoperes-so1nc I'm commenting here to get notified
Since this and puppeteer load chromium it is memory intensive, but if you use it as a singleton and just instantiate playwrite once then it should be fine. But as soon as you want pdfs that are editable, contain fields or you need digital signature capability, yeah this is not gonna cut it at all, and that is when you will start paying a lot of money for a bigger library (syncfusion has one of the best licensing as far as I can tell since the community version gives you a free license until you hit like a 2million $ profit margin or something like that)
This was GREAT!! Just in time for exactly this requirement for a project I’m working on!! 👍👍
I use the built in PrintDocument() set its PrinterSettings object to use Microsoft Print to PDF driver (this does have the drawback of creating a temporary file though). Setting up the page is a bit tricker than using html converters, but easy enough and I have complete control of the whole process.
I have good experience using latex. Your code generates latex code, possibly using templates and then run a pdflatdx compiler as a sub process.
Yep. I use a similar approach - generating XSL-FO from templates and running Apache FOP to do the conversion to PDF
Just wondering: did you consider using LuaTex? And if you did, what was you conclusion?
Wouldn't it be just as sensible to use the MVC Razor page render engine? Aren't they ultimately the same thing?
If only printing were this easy! Great video as always
we're using puppeteer to call our frontend page by passing the access token in the query string.
the advantage we have is the frontend team design the required document using the tools they already familiar with
the disadvantages is the complex setup and depending on the server to have chromium and so on
All these "HTML To PDF" libraries suck. They all package the Blink/Chrome browser in the nuget package, so it's huge. They use the "print to pdf" implemented by blink. Needless to say, it is extremely memory and CPU intensive. Most of us will need to create thousands if not hundreds of thousands of PDFs -- This library will bring your entire server to a crawl, and you'll be paying out the you-know-what in CPU usage. Just buy a real PDF library. The cost will be a wash. Yes, this is a free alternative to these libraries, but that will be the least of your problems if you need to do real work.
At this point, just run a cmd comand to use msedge print-to-pdf functionality, just serve your html to it and that’s it. Msedge comes default with windows, so no other dependencies needed.
I was coming to say the same: at my work we had to evaluate different libs (both free and paid) and ended up deciding for QuestPdf because it was way simpler and much better performant than any of these BS html-to-pdf libs that have all the cons you mentioned. We evaluated Playwright and IronPdf as well and both ended up in the trash can.
At some point companies have to decide to spend some money and in the context of big projects, it really is not worthy to cut down expenses on these things which can really have an impact.
@@CesarDemi81 This is almost the solution i ended up with - I use Xceed Words to generate a document (i also need to be able to save out a Word version of the PDF), use WebView2 (Edge but really Chromium under the hood) to preview the PDF in the (desktop) app - which you get for free, and then have a SaveAs word function - I have yet to find a nice solution to this - even with expensive paid libraries there always seems to be some 'hacking' involved to have a nice document output functionality
ASPOSE & ABCPDF, I'm looking at you. Such ridiculously large libraries for what they do...
If you want good looking, easy to maintain PDFs, using HTML templates is an excellent approach. But you will always need a browser engine if you want to use the latest CSS possibilities.
Not sure I like using Chromium to do this, but using the Razor engine to render components/models into HTML strings is brilliant, I did not think of that. I'm already creating PDFs from manually constructed HTML strings using SelectPdf. I might incorporate the Razor engine for templating. Thanks!
Packaged the same approach in the BlazorReports library, but more for a separate report server.
This approach works great as you can leverage Blazor and other Js libraries.
Do you have any experience how this approach deals with header/footer logic, especially one time headers followed by a different repeating header? Same for footers? Also tables across multiple pages with and without their own repeating header? Those were topics previous approaches I saw to PDF generation from HTML struggled with.
@@theCybertier It's definitely a limitation, it's not hard to include repeating headers or footers but when you need to add logic to them it gets harder. Will try to see if I can get a usable example on the repo if it's viable.
I used to use Office apps to print to a Postscript file, then had a script that would package the Postscripts as a PDF. It was free, in that my employer used Office for everything but printing to Postscripts isn't exclusive to Microsoft products and is completely free as long as you have a Postscript printer installed.
I had to develop a project with PDF reports, and what I did is, I created a TeX template with labels like __name__, __date__, etc. So those labels were replaced with code and the result was converted to PDF. I even included some graphs with TeX libraries.
The final software had to bundle the TeX distribution (MiKTeX if I recall correctly).
I have usel Blazor for years to create nice prints, also with pagination and booklet printing 😊
You said "no libraries", so how do you call Playwright if not a library ?
I can not see local image on pdf? did I do something wrong
Very nice solution to render PDFs! This should also work with Selenium instead of Playwright
If you need a proper publishing infrastructure (page layouts, footnotes, overflow options), then I’ve used XSLFO for years. Similar to HTML markup with CSS. I use a simple paid library called Ibex to render it.
PupeteerSharp also allows to detect and fine control which version of browser is installed
Hello, very information video, I have one question. please answer my question
I have a microservices architecture with 5 services, 2 of them send emails, so have different html dynamic templates in email, and other three have pdfs, .. So if i create a 6th service of blazor project, and then send requests from other services to blazor to create pdf or html (as you are showing on video), and then can download or send on email. is that a good practice? I mean at the same time blazor will be responsible for creating email template like password reset email or otp email, and also responsible as a service to send emails and print pdfs.
I/we did the same but instead of having that in the app, i did an API, you sent it a link (of the invoice) and the API returns the pdf. No overhead of installing playright/chromium on all servers, just the ones dedicated to this api
Having worked on creating pdf muliple times, the way we had done without pdf library was when the content was created in our appliction.
If we have conver a content created by another application, that is challenging. And the PDF library based on browsers seems to be the only way. The main reason being bowsers allow lot of errors in html.
The best way to extend the capability was to use a flow document to have the ability to fixed documet and then to pdf, which unfortunately Ms choose not to do.
Another prospective way is to extend the print to pdf, which also can be done to a good extent, but the libraries that i explored had one issue or another.
Using gotenberg
Container able to do html to pdf or word to pdf using libre Office
I was JUST looking for something like this!
I have used libraries that allow me to dynamically compile a template (int Razor syntax) that I had fetched from the database. I wanted to move over to the Razor components engine but since it lacks that support it is impossible. You kind of have to compile the templates into the project. Unless you compile components into a separate assembly, store it somewhere, and then load it when you generate the invoice.
I love things like this. Definitely saving this in case I need to create any PDFs. Getting my employer to shell out money for libraries is next to impossible :P
will it work as a service for web api controller ?
Some years ago I used PDFlib to generate PDFs based on a template document and dynamically populated field values.
I think it was a fairly expensive library and probably overkill in many cases, but it worked flawlessly, was extremely scalable, and could do pretty much anything with PDF files.
A smart alternative would be to communicate instead with, and print on, the client side with the user's browser! Equals no server side bloat or CPU.
I wish I knew this about 3 years ago 🤣
Next time I'll do it like this, but I will isolate it as a service in a container and send to an storage like s3
I really like this approach. I wonder if there is a way to generate the PDF using streams. For example, when there is a large amount of data that requires writing the file while the data arrives. Also, when there are many files queued to be generated.
We currently use the Syncfusion library. It does the job but I have been tempted to pivot to Playwright to save $$$.
The thing I can’t see this approach doing is document assembly i.e. taking multiple PDF and merging them into one PDF with page numbering etc.
Headless Chromium? No, thanks. Not on my servers.
put it in a purpose built container that has NOTHING else running there.
Awesome!!! Save my life!!! Thank You!!!
Hey Nick,
I love your videos and I'd have a wish on a topic.
Could you please make a video about best practices on pull-requests, focussing not on the self-explanatory branch policies, but more on the perspectives of author and reviewer?
Reason:
We had a couple of "fights" in our company because of comments on pull-requests.
We had these issues on multiple products, involving multiple developers and I feel like it's a systematic issue that must be addressed, before things get out of hand. I highly doubt that we are the only ones struggling with this, since pull-requests are a devops essential.
And I am pretty sure, that someone like you, with an own "code-cop" series, has some neat hints on how to improve as both, author and reviewer, to keep such things from happening. And even if not, it would be great if you could share your experiences with this, cause then this community will very likely be able to provide decent advice.
A lot of people are commenting that this would crush server performance and/or cost a ton of money. That assumes the reports are being generated on the server and that you are in the cloud. In some scenarios that is unavoidable. But many other scenarios this solution would work well.
For Desktop apps, this would be great (can everything Nick did can be baked into the desktop app and just send the data to the client and have them render it?).
With the web, well, you are already rendering to HTML, so you can just have the client export to pdf from his/her browser.
So really the only scenario where this would be an issue is if you were a company offering a pdf-generating service to clients. Then you do have the possibility of millions of pdfs being generated in a short amount of time (if you have a large client base). But if you are a company generating invoices for your clients in-house, then this would absolutely be a viable solution.
The point of PDFs is that you can lock editing and sign it, which is crucial for invoices or other legal documents.
You lose that when you ask client to export the html page to a pdf, them being free to edit any fields before doing so.
Nice, I like it. On desktop apps, I use WPF with Xaml in code to print to the Microsoft PDF driver. I've created some very complicated products with drawing and graphing with complete pagination.
Hi Nick, trying to convert a Html to pdf. When done via Chrome browser it's perfect, all the images and page breaks are perfect, I need to do it via azure function, please advise
How fast is it in comparison to Rotativa?
Can we create pdf/a through this?
Weird on the timing of this.... I was just adding save to PDF support in a WPF application I'm developing for a client. Unfortunately I can't see a way to integrate this solution into a WPF application.
In a WPF application I developed years ago, I created XPS files and called GhostScript to convert them to PDF. Has the advantage that, for creating XPS, I can re-use all WPF UI components.
@@coloresfelices7 ok. I'm sort of doing the same. Rendering WPF Visual element into XPS format, then using PDFSharp to convert that to PDF. The problem is the Visual element is a scroll viewer and I want to have it render the entire content not just what's visible. I need to do more research...
Thanks for sharing. Does it have capability to protect PDF, sign PDF too?
Very interesting. Will check it out for my project.
I like the open source library Quest PDF (there is a commercial licence also, but the price is okay).
Can we do similar in a blazor web assembly application?
I use Pandoc. You can also call it from the CLI.
Will this work on azure app service?
This is my question too. if anyone has tried let us know?
@@umair_me i had someone said to me that it would work but i never tried it
will the code be run on Linux without GUI?
I use just html file with scriban for templating to replace the placeholders. Then IronPDF for the pdf generation. It works well, but I want to try this now.
seems like it would be easy for me to swap out IronPDF and try this since I already have a string for the html that I pass to IronPDF. I'll report back if it works. The html has chartjs-umd in it and some data points to make some charts. Should be interesting to see if that works with Playwright
Hey....but in the containerized era there is the Gotenberg
Playwrite and gotenberg both look like they use headless chromium to create PDFs. You just containerize playwrite and have it basically be the same thing
The mail function does not work. I don't get the source code...
This was a great and creative solution.
How about PDFSharp & MigraDoc by empira Software GmbH, Germany
It is open source under MIT licence
Edit: they are still working on it, but basic functions didn't changed for years so you can AI simple PDF quickly
I have used pupeteer and print pdf to generate hundreds of pdf the code runs in lambda and step functions so server handling was never a chay
Same. I generate statements for a company using PuppeteerSharp and create and email almost 300 files in less than 4 minutes. Some of the querying and emailing also takes time so... do the math. Not a bad deal for me.
Would love to see if you can Dockerize this solution. I have tried and it failed. Maybe I did something wrong?
I still live JS Report. Can slef host and get 5 free templates which can be more than enough if used cleverly
This is actually a very interesting approach to be able to do SSG with Blazor! Experimented with it before and although it's rudimentary; it does the trick. I did not think of the use case of creating pdfs though, very interesting!
This is a pretty cool solution (i love using the Razor html renderer in a non browser app - already got me thinking of other uses for that), although this solution is also pretty 'expensive' because of the dependency on Playwright which means including Chromium etc which are pretty large dependencies. Personally i use Xceed Words to generate documents Word/PDF which isn't as nice in terms of the code needed to create the document, but a bit more lightweight (and very cheap compared to most of the paid document generation libraries).
What is the resource usage like?
@@TewodrosAAssefa you can make it pretty efficient by skipping play right and controlling the chrome instances directly.
You can make it so that you stream the report from the chrome instance to the Response body without having to buffer the entire pdf in memory using pipes.
Been generating tens of thousands of reports daily on a container with 1 vCPU AND 1GB of RAM.
@@gabynevadait’s still freaking Chrome dude, it’s a horrible idea
@@gabynevada This sounds great. I try to find out how this works. I save the Chromium binaries on my device. Then I probably start with ChromiumDirectory = "C:\Users\Me\DotNetBrowser" and create the EngineFactory. What is the key-line to get the pdf finally in the Response body?
@@ВладиславДараган-ш3ф right now other forms of generating PDFs are very limited, if you need the flexibility then a browser is one of the best ways to do it.
Hopefully other options will keep growing and we can avoid it in the future.
@@gabynevada That sounds great. But I don't understand where the conversion to pdf is?
Very nice! But do you have to install the browser on the computer for this to work?
Yes
In my case, I am using RDL through the .NET Framework API to get PDF or Excel for my .NET Core application.
Man this is amazing.
I still love the good old way using rdlc to generate pdfs.
But I guess this way is also kind of ok
Let's do the no external libraries app challenge!
yes
Hey Nick did you ever have to read pdfs programmatically? Which lib did you use in that case?
Reminds me of the time i serializeda object to json to then transform said json into xml becuase the xml library did not support DateOnly and TimeOnly structs
Regardless of the implementation, I really like the idea of generating PDFs from HTML because average developers can do it.
Nice, now show the memory consumption.
Very timely video
Question. Can you include tailwindcss or bootstrap css?
@nick does this work with something like bootstrap,etc
WOW! Realy cool. I am afraid that IronPdf has ordered you after it
I had a really hard time containerizing libraries that convert HTML code to PDF because they usually rely on Windows packages. Can this one run on Linux?
yes
We have done a very similar thing to Nick's shown here, and it has been running in production for a while now on the smallest Linux Azure Container App they have.
What would happen if I use just any html as my HtmlString and run it through Playwrite and PdfAsync ?
The same, it will just render the html through chromium and use it's pdf function.
first question, why do i need blazor?
You don't. You just need a way to use the Razor view engine to generate HTML as a string from Razor views. It works equally well with MVC, I've been using it for 10+ years in production with the IBEX xsl-fo processor (paid for, equivalent to Apache fop)
@@raduban2029 just wondered why it was in there if you don't need it, just seems to complicating a simple concept for the sake of it
I need to digitally sign PDFs, but I'm not sure where to begin or even if it's possible to do that with this but i'll give it a try
Firstly you have to get a certificate (need to buy it) and the signing tool.
@@sarcasticMoose I do own a certificate and can technically sign it I just need to add it visually into the pdf itself.
Paid PDF libraries is not only about create PDF document. It's about manipulate Signatures and aForm data
What about itext ?
Not free, well maybe very old versions.
@@neilbroomfield3080 It's free for personal use, not application releases I believe.
Can this generation method be used on a docker?
You will have to use Debian based image as required by Playwright. Other Linux is not supported by default.
@@sakesun Thank you for hint. I found a image linuxserver/chromium.
Please make more PDF manipulation videos
HTML to pdf is good for just learning and basic PDF file. need to do lot of changes when it comes to create slightly complex document.
The problem that I have with generating PDFs from HTML is that I want each and every page of the document to contain a caption at the top of the page, regardless of the length of the document. How would I reach that goal with a HTML based PDF generator like Nick's solution?
PDF's have a header concept, and I think you could see in the video that there were methods for manipulating/setting the header.
Wait until you want to have multi page tables with repeating headers ;)
@@cjb110 thanks, will try, but my experiences with other packages are ..... Quite underwhelming. Problems usually arise as @bamyazi said.
For complex layouts you need an xsl-fo processor like IBEX (commercial) or Apache FOP (foss but written in Java)
Are the PDFs UA compatible?
^^ + I am always missing an option to add a data layer (as you'd need to create most of eInvoice formats).
nice clean solution
i did something like this a few years ago for email templates
Same as PuppeterSharp, using browser and will cause issues when creating large size pdfs.
The other problem with HTML to PDF is pixel-perfect rendering for intricate work (such as exact positioning on pre-cut labels) and bleeds. You do not always need pixel-perfect rendering, but when you do, HTML just does not cut it.
Awesome !!!
What is your take on IronPdf ?
It's paid.
@@adds-kz3oc oh yeah. We have perf issue with ironPDF in Linux servers. Have been searching for an alternative solution.
Does this work in no GUI environments? Like VPS or some Bash only linux installation?
Yeah ofc
If you specify the Content-Type for a PDF or XLS, or whatever, then stream (Response.Write) the content you want in your file (for an XLS spreadsheet, just stream out a ), after the browser has received the file data, you can go File, SaveAs, and that file type will show as an optional file format, assuming your operating system has that file type registered as a known file type. There is no overhead on the server, and the tiny bit of processing done is done on the client.
9:09 Nick just violated Apple
No, he violated Safari. Because it's just the worst piece of sh**.
About excel?
ClosedXml might be the best one right now to generate Excel. EPPlus is no longer free. NPOI seems to be used a lot, but if you go back in the history, it was at some point filled with political statements (both on NuGet and GitHub), so I am not sure if I would trust that one.
I'm thinking Selenium might have the same functionality as Playwright?
Not sure. I thankfully haven’t used Selenium since Playwright came out
Somebody was on vacation 🏖🏖🏖
This is cool, I wonder if we can use this library for qr codes / barcodes for a label printer