Search code examples
c#asp.net-mvcselectpdf

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 website https://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? enter image description here

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?


Solution

  • 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.