DEV Community

Cover image for QuestPDF HTML to PDF C# Alternatives For .NET Developers
Mehr Muhammad Hamza
Mehr Muhammad Hamza

Posted on

QuestPDF HTML to PDF C# Alternatives For .NET Developers

PDF (Portable Document Format) is widely used to save data or send data in a portable, secure format. When it comes to manipulating data into a PDF file or designing a document like an invoice, C# developers often turn to robust libraries. Two popular Libraries for these tasks are IronPDF and QuestPDF. In this article, we'll delve into how to use QuestPDF for HTML to PDF conversion and compare its features with those of IronPDF.

QuestPDF

QuestPDF is an open-source .NET library designed for PDF document generation, utilizing a fluent API that supports complex layouts and full paging functionality. Unlike IronPDF, QuestPDF does not convert HTML to PDF directly but instead uses its own layout engine to design and generate PDF documents.

Features of QuestPDF

  1. Design and Generate PDF Documents: QuestPDF relies on manually written code rather than HTML tags for document design, focusing on structured, reusable components.
  2. Headers and Footers: Options to add headers and footers with dynamic content, including page numbers, custom JavaScript, and CSS.
  3. Create and Reuse Components: Facilitates writing reusable code to generate consistent document layouts.

Other Features:

  1. Composes simple components into complex documents.
  2. Does not rely on HTML-to-PDF conversion.
  3. Structured, organized code for easy maintenance.

IronPDF

IronPDF is a commercial library that provides extensive features for generating and manipulating PDFs. It excels in converting HTML with complex structures into PDFs with minimal code, making it a versatile choice for various projects.

Features of IronPDF

  1. PDF Conversions: Converts HTML, HTML strings, MVC views, Web Forms, and URLs into PDFs.
  2. PDF Imaging: Generates PDFs from images and extracts images from PDFs.
  3. PDF Files IO: Supports digital signatures, password protection, and encryption.
  4. Editing PDFs: Includes tools for adding watermarks, and pages, and altering backgrounds.
  5. PDF Content Extraction: Extracts text and embedded content from PDFs, with OCR support for text in images.
  6. Headers and Footers: Flexible options for adding text-based or HTML-based headers and footers.
  7. Compatibility: Supports various platforms, including Windows, .NET Core, Azure, AWS, Docker, and Linux.

Creating a New Project in Visual Studio

  1. Open Visual Studio.
  2. File > New Project > Console Application.
  3. Name the project and choose the path.
  4. Select the .NET Framework.

Create New Project - Visual Studio

Installing QuestPDF

We can Install QuestPDF by using one of the following ways.

1. Using Visual Studio NuGet Package Manager:

Follow these steps to install QuestPDF using the Package Manager for Solution.

  1. Tools > NuGet Package Manager > Manage NuGet Packages for Solution
  2. Browse for "QuestPDF" and install.

Install QuestPDF Using NuGet Package Manager Solution

2. Using Command-Line:

Follow these steps to install QuestPDF using the Package Manager Console.

  1. Tools > NuGet Package Manager > Package Manager Console
  2. Enter Install-Package QuestPDF.

Install QuestPDF - Package Manager Console

3. Direct Download from NuGet:

  1. Navigate to NuGet QuestPDF.
  2. Download and install the package.

Installing IronPDF

We can Install IronPDF by using one of the following ways.

1. Using Visual Studio NuGet Package Manager:

Follow these steps to install IronPDF using the Package Manager Console.

  1. Tools > NuGet Package Manager > Manage NuGet Packages for Solution
  2. Browse for "IronPDF" and install.

Install IronPDF - Nuget Package Manager for Solution

2. Using Command-Line:

Follow these steps to install IronPDF using the Package Manager Console.

  1. Tools > NuGet Package Manager > Package Manager Console
  2. Enter Install-Package IronPdf.

Install IronPDF - Package Manager Console

3. Direct Download from NuGet:

  1. Navigate to NuGet IronPDF.
  2. Download and install the package.

HTML-to-PDF Conversion

IronPDF

IronPDF makes HTML to PDF conversion straightforward with minimal code. IronPDF allows developers to create PDF files from HTML using three methods.

Use the following namespace for using IronPDF:

using IronPdf;
Enter fullscreen mode Exit fullscreen mode

1. Create PDF files from HTML string:

Creating a PDF file from an HTML string using IronPDF is a straightforward and efficient process. This powerful library allows developers to convert HTML content into high-quality PDF documents with minimal code, making it ideal for generating dynamic reports, invoices, and other documents directly from web content.

 var renderer = new ChromePdfRenderer();
 var pdf_doc = renderer.RenderHtmlAsPdf("<h1>Hello World!</h1><p>This file is created from Iron PDF using HTML String Method</p>");
 pdf_doc.SaveAs("htmlString.pdf");
Enter fullscreen mode Exit fullscreen mode

The code initializes a ChromePdfRenderer object and uses it to convert an HTML string into a PDF document. The HTML content includes a heading and a paragraph, which are rendered into the PDF and saved as "htmlString.pdf".

The generated PDF document is as:

Output PDF - Generate PDF from HTML String

2. Create PDF file from URL

Creating a PDF file from a URL using IronPDF is a seamless process that allows developers to convert entire web pages, including complex HTML implementations, into well-formatted PDF documents. This capability is perfect for archiving web content, generating dynamic reports, and creating print-ready versions of web pages with just a few lines of code.

  var renderer = new ChromePdfRenderer();
  var pdf_doc = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/PDF");
  pdf_doc.SaveAs("pdf_from_url.pdf");
Enter fullscreen mode Exit fullscreen mode

The code initializes a ChromePdfRenderer object and uses it to convert the content of a specified URL ("https://en.wikipedia.org/wiki/PDF") into a PDF document. The resulting PDF is then saved as "pdf_from_url.pdf".

Output PDF File - Convert URL to PDF

Output PDF file is as follow:

3. Create PDF file from HTML File:

Creating a PDF file from an HTML file using IronPDF allows developers to maintain the document's visual structure and ensures advanced text style support. This powerful tool is ideal for converting well-designed HTML content into high-quality PDFs, preserving the original formatting and styling seamlessly.

 var renderer = new ChromePdfRenderer();
 var pdf_doc = renderer.RenderHtmlFileAsPdf(@"D:\Tutorial Project\Website\index.html");
 pdf_doc.SaveAs("pdf_from_html_file.pdf");
Enter fullscreen mode Exit fullscreen mode

The above code initializes a ChromePdfRenderer object to convert an HTML file located at "D:\Tutorial Project\Website\index.html" into a PDF document. The rendered PDF is then saved as "pdf_from_html_file.pdf". This process allows for the direct conversion of locally stored HTML files into portable and printable PDF documents.

The HTML File used as input is as:

Input HTML File - Convert HTML File to PDF File

The Output PDF file is as:
PDF Output - Convert HTML File to PDF

QuestPDF

Unlike IronPDF, QuestPDF doesn't directly convert HTML into PDF files. This can be a drawback for users looking for an easy way to turn web content into PDFs. Instead, QuestPDF focuses on giving you detailed control over how your documents look and feel using its layout tools, which may mean more work upfront compared to HTML-based converters.

QuestPDF requires more code to achieve PDF documents generation resembling HTML, emphasizing its layout engine specifically designed for precise control over document structure and styling. This approach ensures meticulous customization of content arrangement and appearance, making it ideal for creating detailed and visually appealing PDF reports and documents.

using QuestPDF.Fluent;
using QuestPDF.Helpers;
using QuestPDF.Infrastructure;

 static void Main(string[] args)
{
    QuestPDF.Settings.License = LicenseType.Community;
    Document.Create(container =>
    {
        container.Page(page =>
        {
            page.Size(PageSizes.A4);
            page.Margin(2, Unit.Centimetre);
            page.PageColor(Colors.White);
            page.DefaultTextStyle(x => x.FontSize(20));
            page.Content()
               .PaddingVertical(1, Unit.Centimetre)
               .Column(x =>
               {
                   x.Spacing(20);
                   x.Item().Text("Hello World!");
               });
        });
    }).GeneratePdf("Example.pdf");
}
Enter fullscreen mode Exit fullscreen mode

This code snippet uses QuestPDF's fluent API to create a PDF document named "Example.pdf". It defines a single page with A4 size, sets margins and page color, and specifies a default text style with a font size of 20. Within the page, it adds content vertically padded and organized into columns with specific spacing, displaying the text "Hello World!". The GeneratePdf method finalizes the document generation process by saving it as "Example.pdf".

Generate PDF - QuestPDF

Custom Headers and Footers

IronPDF

IronPDF allows both text-based and HTML-based headers and footers:

PdfDocument doc = new PdfDocument("htmlString.pdf");
TextHeaderFooter header = new TextHeaderFooter();
header.CenterText = "This is Header";
header.DrawDividerLine = true;
doc.AddTextHeaders(header);


TextHeaderFooter footer = new TextHeaderFooter();
footer.CenterText = "This is Footer";
footer.DrawDividerLine = true;
doc.AddTextFooters(footer);
doc.SaveAs("htmlString.pdf");
Enter fullscreen mode Exit fullscreen mode

This code snippet initializes a PdfDocument object named doc by loading an existing PDF file named "htmlString.pdf". It then creates TextHeaderFooter objects for both header and footer sections, customizing their text and divider line settings. These header and footer elements are added to the PdfDocument using AddTextHeaders and AddTextFooters methods respectively. Finally, the modified document is saved back to "htmlString.pdf", incorporating the specified header and footer content and styling into the PDF file.

Output - Add Header and Footer Using IronPDF

QuestPDF

QuestPDF uses its layout engine for headers and footers:

 Document.Create(container =>
   {
       container.Page(page =>
       {
           page.Size(PageSizes.A4);
           page.Margin(2, Unit.Centimetre);
           page.PageColor(Colors.White);
           page.DefaultTextStyle(x => x.FontSize(20));
           page.Header().Text("This is Header")
               .SemiBold().FontSize(12).FontColor(Colors.Blue.Medium);
           page.Footer()
              .AlignCenter()
              .Text(x => x.Span("This is Footer"));
       });
   }).GeneratePdf("Example.pdf");
Enter fullscreen mode Exit fullscreen mode

This code snippet utilizes QuestPDF's fluent API to generate a PDF document named "Example.pdf". It defines a single page with A4 size, sets margins, and specifies a white page background. The page's default text style is set to a font size of 20. Additionally, it adds a header with blue semi-bold text and a footer centered with plain text stating "This is Footer". The GeneratePdf method finalizes the document creation process by saving it as "Example.pdf", incorporating the specified header and footer styling into the PDF file.

Output - Add Header and Footer using QuestPDF

Both IronPDF and QuestPDF provide comprehensive solutions for PDF generation in C#, with IronPDF excelling in HTML conversion and robust paging support with elements support paging functionality. QuestPDF offers a meticulous layout engine for precise document design and supports full paging functionality, making it an ideal choice for projects requiring detailed control over document structure.

Licensing

IronPDF:

IronPDF offers a free trial license, with commercial licenses starting at $749, including a 30-day money-back guarantee and a year of support and upgrades.

Pricing - IronPDF

QuestPDF

QuestPDF library offers a free Community license, with commercial licenses starting at $699.

Pricing - QuestPDF

Conclusion:

In conclusion, both IronPDF and QuestPDF offer robust solutions for generating and manipulating PDF documents in C#. IronPDF stands out with its comprehensive HTML-to-PDF conversion capabilities, making it ideal for projects requiring seamless integration with web content. Its support for text and HTML-based headers and footers, along with extensive PDF manipulation features, ensures versatility across various applications.

On the other hand, QuestPDF distinguishes itself with a meticulous layout engine that provides detailed control over document structure and styling. While it lacks direct HTML-to-PDF conversion, QuestPDF excels in creating complex layouts and reusable components, making it a preferred choice for projects demanding precise document design and full paging support.

Ultimately, the choice between IronPDF and QuestPDF depends on specific project requirements, such as the need for HTML conversion versus detailed layout control. Both libraries offer valuable tools for C# developers looking to enhance their PDF generation capabilities with reliable and feature-rich solutions.

Top comments (0)