The Easiest Way to Create PDFs in .NET
- November 4, 2023
- Posted by: MainInstructor
- Category: C C# Go Software Engineering
Video Title: The Easiest Way to Create PDFs in .NET
Hello everybody I’m Nick in this video I’m going to show you how you can get started with creating PDFs in your do net applications very very easily and I think the library we’re going to Showcase in this video today is actually an amazing case study in developer experience because it solves all the
Things in my opinion that makes this sort of thing pretty hard to do PDF Creation in general is a total pain in where you you know where and it usually revolves around having wrappers around other low level libraries and so on but in this video we’re going to see how
Easy it can be if you like content and you want to see more make you subscribe for more training check out my courses on do tr.com okay so let me show you what I have here and actually what I’m going to do in this video is actually
Just show you how we can build a very simple PDF page and we’re going to do that by using a library Called Quest PDF now there other libraries that can sort of create PDFs or convert them from let’s say HTML to PDFs but Quest PDF has a very very nice developer experience
On creating them through a fluent API which I personally really like and it suits my use cases when I’m creating PDFs especially for invoices and stuff like that programmatically now you can install Quest PDF by simply searching for Quest PDF on new get and then installing the latest version and once
You have that you can start working with it in any cop. net context now there’s a few things I want to mention first and foremost Quest PDF is an open source Library so please use the link in the description if you like what you’re
Going to see in this video and give it a star because because it really really helps the developers working on this product also they have sponsorships enabled so if you want to support the project financially then you can do it either monthly or with a onetime payment now I’ve personally decided that every
Time I’m showcasing a library and open source project on my YouTube videos if they have sponsorships enabled I will go ahead and do a onetime payment so in here I’m going to go ahead and donate a onetime donation of $250 here we go we have sponsored now
The project and if you want to do the same then all the details are in the description the other thing I want to mention is that Quest PDF actually has a pricing Model A licensing model it is free for Developers for up to a million doar in annual gross revenue but then if
You’re a company with up to 10 developers you should pay $500 per year which is basically nothing if you’re making the type of money and $2,000 a year for an unlimited amount of developers these are insanely small numbers and I have nothing against libraries that actually have a pricing
Model in fact I recommend it because we make OSS sustainable that way now with all that out of the way let’s go straight into the implementation of creating a PDF and explain why I think quest PDF is so so cool now the first thing we need to do in your applications
When you’re using something like this is you actually have to specify what type of license you’re using in the application and to do that I’m going to say Quest PDF settings and then license and I’m going to select the community license over here the moment we do that
We’re able to use it in our obligations there’s no checking of a license key in our system Quest PDF sort of trust you that you’re going to do the right thing and if you need to use um an Enterprise or a professional license you will do
That now let’s say we want to build a PDF well all we have to do to get started is say document and then use the fluent API and say create the moment we do that we have access to a container and in this top level container we can
Specify anything about our PDF now in the end you have a couple of options of what you want to do with this PDF for example you can just generate a PDF or you can generate an XPS file as well we’re going to say generate PDF or images but the coolest thing about this
Library is that you can actually have a previewer a way to see how your PDF will look as you’re building it and you can do that by saying show in previewer now to install the prevor you have to install a global tool this is very easy
To do you just go to your console and you say net tool install Quest pdf. prev and then Global specifying that this is a global tool the moment you do that it’s going to be downloaded and installed in your system and then you’re able to use the previewer so this will
Look something like this all I’m going to do here is I’m going to go to the terminal actually and I’m going to say net watch to have hot reload kick in as I’m building my application and what’s going to suddenly appear is actually an extra window let’s wait for it here you
Go this is a quest PDF previewer we’re going to see the page we’re building in the PDF we’re building as we’re building it it’s so so cool so I’m going to separate my PDF viewer and my ID so you can see both on the same page so now
That I have both let’s go ahead and start building our page so first I’m going to say container and then page to create my page and then I’m going to give this descriptor an alias call it page and start building it so the moment I save as you can see hot reload kicks
In and now we have a view of the page over here I should be able to just zoom in a bit so you can see it better first I’m going to say page do size to specify a size and we can specify the page size
Using page sizes Dot and you can say A4 so paper A4 a01 to three in a bunch of different other sizes we’re just going to say A4 here assuming we want to make something that we want to print now right below I’m going to say page do
Header and I’m going to create a header now the first thing I’m going to say is I want some text that text should be hello from Ryder again the moment I save you can see that the header actually just appears up here now and everything happens in real time now I can customize
The text so I can say I want this to be a semi bolt for example and the moment I save that you can see the change then I want to specify a font size of 30 and everything is customizable here so you can also say like font color and say
Colors do I don’t know Amber for some reason and you can use the medium type of Amber well this doesn’t show great in the video so maybe change that for something like I don’t know yellow that’s even worse isn’t it yes it is well I’m just going to go with Classic Blue that’s
Better but now this is a bit on the top left corner and nothing really happen happen so let’s go ahead and give a bit of structure on our page so we’re going to go above this and I’m going to say page do margin and I’m going to give it
A margin of two with unit being either centimet feet for some reason I guess if you’re printing stuff it makes sense inch meter millimeter or point I’m going to say 2 cm and as you can see this now moves it in 2 cm on an A4 page other
Things I can do is Page color so I can say that I want this to be colors dot white over here and I want to set a default text style over here so I’m going to say font size 20 as the default font size then let’s move into the
Content of the page so I’m going to say page do content and that’s the main thing of the page so I’m going to add some padding I’m going to say padding vertical and you have other types of padding as well if you want to so I’m
Just going to say padding you know top left horizontal whatever I’m just going to say 1 cm and then I’m going to create a column in that column I can add multiple things for example I can have some spacing I’m going to say 20 and the
Default 20 is point you can change that to anything you want like centimeters and so on and I’m going to just add a couple of items in my PDF so the first item I want to add is text and you actually have access to a bunch of
Placeholders if you want to so for example you have lurm ipsum image color integer email so if I say email for example and I save you see a random emailing appear here I’m just going to say lurm ipsum and the moment I save lur makes him appears and the other thing I
Can add is I guess an image and again I’m going to use a placeholder of an image and I’m going to say at 200 by 100 and you see a randomly generated image appears here so you see how I’m starting to build my page and structure my page
Now the last thing PDFs usually have is a footer so I’m going to say page do footer over here and I’m going to say align Center because I want to have the page number in the center of my PDF so I’m going to say that the text here is
Is actually a span with text Page and a space and then follow that up with the page number so get current page number and that’s it maybe my face heighted but as you can see now on the bottom over here we now have the page number and the
More pages we have the more we’re going to add here now you can understand how with this way of programmatically generating the PDF you can do basically anything the page object has anything you will ever need from the content the defaults the margins The Continuous sizes things like this maximum size
Minimum size it’s very very customizable and very very extensible I’ve been trying to solve this problem for my business myself and Quest PDF for what I’ve seen and what I’ve tried is the best experience for my use case of generating PDFs in that way now this is
Just a way to get started with there’s tons of examples on the quest PDF GitHub repo on how you can generate invoices and a bunch of different other stuff and the preview is what the game changer for me because being able to change every everything in real time makes things so
So easy of course it goes without saying but if you want to actually generate a PDF you can easily do that by saying generate PDF and I’m going to give it a name called test.pdf save and then if I quickly just uh run this then as you’re
Going to see I have a quest PDF object created over here and if I open it on my browser that PDF is then created and that’s exactly what you should expect for your application as well I think quest PDF is a great Library I really don’t mind the license in fact I really
Like that it does have a license meaning that it can be a sustainable project and I highly recommend it to everyone of you but now from you what you using to generate your PDFs and you prefer the iron PDF approach of creating them leave a comment down below and let me know
Well that’s all I had for you for this video thank you very much for watching and as always keep coding
Video Keywords: .Net, Elfocrash,elfo,coding,.netcore,dot net,core,C#,how to code,tutorial,development,software engineering,microsoft,microsoft mvp,.net core,nick chapsas,chapsas,dotnet,.net,create pdf,create pdf in .net,pdf in .net,create pdf in c#,create pdf with c#,open pdf in .net,open pdf in c#,questpdf,quest pdf,quest pdf c#,The Easiest Way to Create PDFs in .NET
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Thanks for sharing!!! It would be nice to see a demo with variables instead of writing strings, just an idea!
Been using PDFSharpCore for a project. What a pain.
This brings bad memories. Years ago, one of a 'good' solutions was to convert Html to PDF. It was janky as hell. This was around Net Framework 4.0. It was still not as bad as using interlop library for excel and word.
I've been using ActiveReports and rpx files. But this looks good
‘in feet, for some reason?’ Lol.
We are using stimulsoft for so ling now and the experience was perfect
Awesome lib: thanks for sharing.
Other alternative options?
Imho, Aspose package is much more reliable and suitable for such tasks.
Nice! I wonder why they used callbacks instead of declarative code.
Seems very user friendly – Devexpress also has pdf gen capability
Writing markup in code looks fun. I usually write my markup in markup.
This looks like a great tool for doing what it’s made to do! The visualizer is cool
The only nock I have on this is that it doesn’t have a way to handle variable-length content. You seemingly have to know how much fits on a page ahead of time, but in some cases that’s impossible
Have you checked Syncfusion? (not only PDF library) Is also similar licence if you'er under 1m $ , but more pricey if youre over than that 😉
What about templates?
I've done some pdf long ago in php programmaticly with a library and a template
Maybe I would have done it without template with a live preview like
QuestPDF
Great video! There’re also libraries which build PDF based on HTML page. This could be convenient for backend developers with basic frontend knowledge. With HTML template you can gain same preview experience with well-known CSS styling on board
I am skeptical over the nuget package choice name due to licensing. You can't release code under MIT and then add commercial restrictions without altering the license to something "MIT like" which tells me the nuget dev wants to pretend to be open source to gain users but then still charge them money by sowing fear mongering around commercial restrictions. If I follow the MIT lic I should not need to pay him a penny. I don't like people like this and I don't like supporting their 2 face approach in the community either. If you want people to pay you there are more honest less upsetting ways to do it
I am using a puppeteersharp to render HTML to image but it is slow because it needs to create a new instance of the browser take a screenshot and close it. Do you guys know how can I improve the speed? I am talking about tens and maybe hundreds of requests per second.
I wish you could explain all of that in Visual Studio ..