将HTML转换为Pdf时身份验证失败错误

问题描述 投票:0回答:2

我尝试使用“ HtmlToPdf” nuget将HTML转换为PDF,在本地测试中工作正常,但是当我将网站上传到主机时出现此错误:

 Conversion error: Authentication error.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Exception: Conversion error: Authentication error.

这是我的Convert方法代码

        [AllowAnonymous]
        public ActionResult Convert(int id)
        {

            HtmlToPdf converter = new HtmlToPdf();
            var context = System.Web.HttpContext.Current;
            string baseUrl = context.Request.Url.Host + ":"+context.Request.Url.Port + "/Doctor/DietTherapy/LineRegimePrint/";
            PdfDocument doc = converter.ConvertUrl(baseUrl + id);

            // save pdf document
            byte[] pdf = doc.Save();

            // close pdf document
            doc.Close();

            // return resulted pdf document
            FileResult fileResult = new FileContentResult(pdf, "application/pdf");
            fileResult.FileDownloadName = "Document.pdf";
            return fileResult;
        }

我如何授权用户进行此转换?

asp.net-mvc asp.net-mvc-5 html-to-pdf
2个回答
0
投票

听起来您只需要由PDF库制作authenticate the request。例如,如果使用的是基本HTTP身份验证:

HtmlToPdf converter = new HtmlToPdf();
converter.Options.Authentication.Username = "some username";
converter.Options.Authentication.Password = "some password";
// the rest of your code...

链接的文档还包含其他身份验证方法的示例。


0
投票

您是否检查过这行代码(在主机服务器上运行时)是否获得了正确的值?

string baseUrl = context.Request.Url.Host + ":"+context.Request.Url.Port + "/Doctor/DietTherapy/LineRegimePrint/";
© www.soinside.com 2019 - 2024. All rights reserved.