Simple HTML to PDF is a lightweight NuGet package that provides a simple HTML to PDF converter library for .NET projects. It allows you to easily convert HTML content to PDF documents using the wkhtmltopdf
command-line tool.
You can install the Simple HTML to PDF package via NuGet Package Manager or by using the .NET CLI.
-
Open the NuGet Package Manager in Visual Studio.
-
Search for "SimpleHtmlToPdf" and select the package.
-
Click on the "Install" button to install the package into your project.
Execute the following command in the terminal:
dotnet add package SimpleHtmlToPdf
To use the Simple HTML to PDF library in your project, follow these steps:
-
Install the Simple HTML to PDF package (as described in the Installation section).
-
Add a reference to the library in your project:
using SimpleHtmlToPdf.Interfaces; using SimpleHtmlToPdf.Settings; using SimpleHtmlToPdf.Settings.Enums; using SimpleHtmlToPdf.UnmanagedHandler;
-
Ask for an instance of the
IConverter
class from the system's IoC:public HomeController(IConverter converter) { ... }
-
Convert an HTML string to a PDF document:
// Define the HTML-to-PDF request input var doc = new HtmlToPdfDocument() { GlobalSettings = { // Color mode of the output file ColorMode = ColorMode.Color, // Orientation of the output file Orientation = Orientation.Landscape, // Paper size of the output file PaperSize = PaperKind.A4Plus, }, Objects = { new ObjectSettings() { // HTML content to convert HtmlContent = "<html><body>Test</body></html>", // The default encoding used. WebSettings = { DefaultEncoding = "utf-8" }, }, } }; // Convert our HTML document to a PDF document var pdf = Converter.Convert(doc);
Replace
HtmlContent
with the actual HTML content you want to convert. -
Send the PDF file to the browser:
return File(pdf, "application/pdf", "Test.pdf");
Customize based on your requirements.
Contributions are welcome! If you'd like to contribute to this project, please follow these steps:
-
Fork the repository.
-
Create a new branch:
git checkout -b feature/your-feature-name
-
Make your changes and commit them:
git commit -m "Add your commit message"
-
Push your changes to your forked repository:
git push origin feature/your-feature-name
-
Open a pull request in this repository, and provide a detailed description of your changes.
This project is licensed under the Apache 2 License.
- The
wkhtmltopdf
tool, which enables HTML to PDF conversion. - The DinkToPDF library, which provided the initial code.