2

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?

Sheng Jie
  • 141
  • 3
  • 16

2 Answers2

2

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();
Okta
  • 70
  • 10
  • Thanks for reply. I have tried your method but hit another error System.Exception: Conversion error: Could not open url. – Sheng Jie Nov 19 '20 at 16:26
  • is there any restriction on the headerHtml? Eg. must trim space, must include tag? something like that? – Sheng Jie Nov 19 '20 at 16:28
  • 1
    From the docs: ConvertHtmlString(String), that has only the htmlString parameter, can be used to convert html strings that don't have external references to javascript, css files or images. In case the html string contains references to external resources specified using relative paths, the second method needs to be used. ConvertHtmlString(String, String) contains an additional parameter: baseUrl. Sample HTML: https://selectpdf.com/demo/convert-html-code-to-pdf.aspx – Okta Nov 19 '20 at 18:40
  • 3
    The problem is PdfHtmlSection(headerHtml). Constructor with 1 parameter assumes that's an url. Change it to PdfHtmlSection(headerHtml, string.Empty) to fix the problem. – selectpdf Nov 20 '20 at 07:59
  • Hi @selectpdf, do you know how can I pass the page number to my header.cshtml? I want to place it inside the table – Sheng Jie Nov 24 '20 at 06:05
2

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.

Sheng Jie
  • 141
  • 3
  • 16