简体   繁体   中英

How to use another cshtml file as SelectPDF header in ASP.NET MVC?

anyone using SelectPDF in ASP.NET MVC project here? I have two files, content.cshtml and also header.cshtml. I want to create an invoice which header is actually a table with page number shown as following prototype:

The header template including the logo and table is ready in header.cshtml. Without the header, I can convert the content using the following code without problem:

string html = await ViewToHtmlAsync("~/Views/content.cshtml");
SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf();
PdfDocument pdf = converter.ConvertHtmlString(html);

*ViewToHtmlAsync is the function that convert entire content.cshtml file as string.

The problem is header, header must be created using converter. I tried to follow the sample provided by the official websitehttps://selectpdf.com/demo/html-to-pdf-headers-and-footers.aspx but I am stuck with the following code, it can't be simply fixed by adding some reference? 在此处输入图片说明

My question is how do I create header which its template is from another cshtml file using selectPDF? If yes, how do I differentiate the page number then?

You are working with ASP.NET MVC project, but the Documentation page you cite provides a sample for ASP.NET project.

Here is a code sample which will work for ASP.NET MVC:

        string html = ViewToHtmlAsync ("~/Views/content.cshtml");
        SelectPdf.HtmlToPdf converter = new SelectPdf.HtmlToPdf();
        PdfDocument pdf = converter.ConvertHtmlString(html);

        string headerHtml = ViewToHtmlAsync("~/Views/header.cshtml");

        var header = new PdfHtmlSection(headerHtml);
        header.AutoFitHeight = HtmlToPdfPageFitMode.AutoFit;
        converter.Header.Add(header);

        SelectPdf.PdfDocument doc = converter.ConvertHtmlString("YOUR PAGE CONTENT");
        SelectPdf.PdfPage page = doc.AddPage();

Yes, SelectPDF has provide me the correct answer. ConvertHtmlString(string) with one parameter will assume it is url, not the string content of html. Change to two parameter which is ConvertHtmlString(string, string) solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM