Asp.Net 核心 MVC - 在将视图转换为 pdf 时图像不 load/display
Asp.Net Core MVC - Image does not load/display while converting view into pdf
这与我上一个问题有关。我正在尝试将视图转换为 pdf。视图正在按预期转换为 pdf 文件,但是当视图转换为 pdf 时,视图内的图像没有得到 loaded/displayed。如果我 运行 在浏览器上独立查看,它会显示图像,但是当我通过这种转换时,它不会 load/display 图像。
这里是控制器方法的代码:
public async Task<IActionResult> Receipt(string rId)
{
//Receipt Data-----------------------------------------------------------------
ParentForApply ParentVM = new ParentForApply();
ParentVM.DonateDataList = _db.ApplicantDonation.Where(i => i.ReceiptId == rId);
WC.ReceiptId = ParentVM.DonateDataList.First().ReceiptId;
WC.DateOfD = ParentVM.DonateDataList.First().DateOfD;
WC.SchAmount = ParentVM.DonateDataList.First().SchAmount;
WC.FullName = ParentVM.DonateDataList.First().FullName;
WC.ApplicantContact = ParentVM.DonateDataList.First().ApplicantContact;
WC.ApplicantEmail = ParentVM.DonateDataList.First().ApplicantEmail;
WC.ApplicantCity = ParentVM.DonateDataList.First().ApplicantCity;
WC.ApplicantState = ParentVM.DonateDataList.First().ApplicantState;
WC.DonorFullName = ParentVM.DonateDataList.First().DonorFullName;
WC.ContactNo = ParentVM.DonateDataList.First().ContactNo;
WC.EmailId = ParentVM.DonateDataList.First().EmailId;
//Receipt Data-----------------------------------------------------------------
using (var stringWriter = new StringWriter())
{
var viewResult = _compositeViewEngine.FindView(ControllerContext, "ViewReceipt", false);
if (viewResult == null)
{
throw new ArgumentNullException("View Can't Be Found");
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
var viewContext = new ViewContext(
ControllerContext,
viewResult.View,
viewDictionary,
TempData,
stringWriter,
new HtmlHelperOptions()
);
await viewResult.View.RenderAsync(viewContext);
var htmlToPdf = new HtmlToPdf(1000, 1000);
htmlToPdf.Options.DrawBackground = true;
var pdf = htmlToPdf.ConvertHtmlString(stringWriter.ToString());
var pdfbytes = pdf.Save();
return File(pdfbytes, "application/pdf");
}
}
这是放置图片的查看页面的代码:
<div class="receiptheader" style="background-color:white;">
<table>
<tr>
<td><img src="~/images/logo.png" class="p-3" id="headImg" style="height:100px;width:100px;" /></td>
<td>
<h1>Derawala Education & Charitable Trust</h1>
<h6>Building Generation, Building Nation</h6>
</td>
</tr>
</table>
</div>
您的图片 src 需要使用完整可访问路径的地址才能正常工作。
喜欢:
<img id="mainImage" style="width:200px;height:160px" src="https://localhost:5001/Images/ProductImages/d0f8bbb9-effd-4f93-b5bc-b28fdfe4a233Capture.PNG">
对我有用。
这与我上一个问题有关。我正在尝试将视图转换为 pdf。视图正在按预期转换为 pdf 文件,但是当视图转换为 pdf 时,视图内的图像没有得到 loaded/displayed。如果我 运行 在浏览器上独立查看,它会显示图像,但是当我通过这种转换时,它不会 load/display 图像。
这里是控制器方法的代码:
public async Task<IActionResult> Receipt(string rId)
{
//Receipt Data-----------------------------------------------------------------
ParentForApply ParentVM = new ParentForApply();
ParentVM.DonateDataList = _db.ApplicantDonation.Where(i => i.ReceiptId == rId);
WC.ReceiptId = ParentVM.DonateDataList.First().ReceiptId;
WC.DateOfD = ParentVM.DonateDataList.First().DateOfD;
WC.SchAmount = ParentVM.DonateDataList.First().SchAmount;
WC.FullName = ParentVM.DonateDataList.First().FullName;
WC.ApplicantContact = ParentVM.DonateDataList.First().ApplicantContact;
WC.ApplicantEmail = ParentVM.DonateDataList.First().ApplicantEmail;
WC.ApplicantCity = ParentVM.DonateDataList.First().ApplicantCity;
WC.ApplicantState = ParentVM.DonateDataList.First().ApplicantState;
WC.DonorFullName = ParentVM.DonateDataList.First().DonorFullName;
WC.ContactNo = ParentVM.DonateDataList.First().ContactNo;
WC.EmailId = ParentVM.DonateDataList.First().EmailId;
//Receipt Data-----------------------------------------------------------------
using (var stringWriter = new StringWriter())
{
var viewResult = _compositeViewEngine.FindView(ControllerContext, "ViewReceipt", false);
if (viewResult == null)
{
throw new ArgumentNullException("View Can't Be Found");
}
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
var viewContext = new ViewContext(
ControllerContext,
viewResult.View,
viewDictionary,
TempData,
stringWriter,
new HtmlHelperOptions()
);
await viewResult.View.RenderAsync(viewContext);
var htmlToPdf = new HtmlToPdf(1000, 1000);
htmlToPdf.Options.DrawBackground = true;
var pdf = htmlToPdf.ConvertHtmlString(stringWriter.ToString());
var pdfbytes = pdf.Save();
return File(pdfbytes, "application/pdf");
}
}
这是放置图片的查看页面的代码:
<div class="receiptheader" style="background-color:white;">
<table>
<tr>
<td><img src="~/images/logo.png" class="p-3" id="headImg" style="height:100px;width:100px;" /></td>
<td>
<h1>Derawala Education & Charitable Trust</h1>
<h6>Building Generation, Building Nation</h6>
</td>
</tr>
</table>
</div>
您的图片 src 需要使用完整可访问路径的地址才能正常工作。
喜欢:
<img id="mainImage" style="width:200px;height:160px" src="https://localhost:5001/Images/ProductImages/d0f8bbb9-effd-4f93-b5bc-b28fdfe4a233Capture.PNG">
对我有用。