Home  >  Article  >  Backend Development  >  html to pdf c

html to pdf c

PHPz
PHPzOriginal
2023-05-09 11:15:07750browse

HTML to PDF C#: How to convert HTML to PDF in ASP.NET MVC application

In today's digital era, more and more people use PDF files to archive, share and view documents. Sometimes, converting HTML to PDF files can make it easier for your users to browse the document. In this article, we will learn how to convert HTML to PDF using C#.

In order to convert HTML to PDF, we need to use a library called WkHtmlToPdf. This library is based on WebKit and can convert HTML pages to PDF files. The library provides many configuration options to easily convert HTML into the desired format.

let's start! First you need to install the WkHtmlToPdf library from the NuGet package manager. Open Visual Studio and right-click the "References" path in the project and select "Manage NuGet Packages". Type "WkHtmlToPdf" into the search box and press Enter. Select the library and install it.

Next, we need to create a class called "HtmlToPdfConverter", which is responsible for handling HTML to PDF conversion. In this class, we need to write the following method:

public byte[] ConvertHtmlToPdf(string html)
{
    byte[] pdfBuffer; // 将转换后的PDF文件存储到字节数组中

    var options = new PdfOptions
    {
        // 设置PDF选项,包括页面大小、宽度和高度等
        PageSize = PaperKind.A4,
        Orientation = PdfOrientation.Portrait
    };

    var htmlToPdf = new HtmlToPdfConverter(options); // 创建HTML转换PDF对象

    pdfBuffer = htmlToPdf.GeneratePdf(html); // 生成PDF文件,并将其存储在缓冲区中

    return pdfBuffer;
}

With the above code, we convert the parameterized HTML to PDF format and then store the file in a byte array. The HtmlToPdfConverter class is the core class of the WkHtmlToPdf library, which provides many useful options to configure PDF output. In the above code, we set options such as page size and page orientation.

Next, let’s see how to use our HtmlToPdfConverter class in an ASP.NET MVC application. First, we need to create an action method in the controller to call our converter. For example:

public ActionResult ConvertHtmlToPdf()
{
    string html = "<html><body><h1>测试HTML转PDF</h1></body></html>";

    byte[] pdfBuffer = new HtmlToPdfConverter().ConvertHtmlToPdf(html);

    return File(pdfBuffer, "application/pdf"); // 返回转换后的PDF文件
}

In this example, we get the content from a simple HTML string, pass it to our HtmlToPdfConverter method, and get the PDF file from the returned byte array. Finally, we use the MVC return type "File" to send the PDF file to the client. In this return type, the first parameter is a byte array, and the second parameter is the output MIME type, which is "application/pdf".

Finally, we configure the action method to be called on a certain URL. For example, add the following code to the default route to access "/Home/ConvertHtmlToPdf":

routes.MapRoute(
    name: "ConvertHtmlToPdf",
    url: "Home/ConvertHtmlToPdf",
    defaults: new { controller = "Home", action = "ConvertHtmlToPdf" });

Next, we can simply navigate to that URL in the application to convert HTML to PDF.

In this article, we introduced how to convert HTML to PDF using C# in an ASP.NET MVC application. We use the WkHtmlToPdf library to perform HTML to PDF conversion and output it to the client using a simple action method. This method can be used to generate various types of PDF files, helping to optimize the user experience and data sharing of applications.

The above is the detailed content of html to pdf c. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn