如何从 cshtml 打印 <img> 标签,仅打印其他标签

How to print <img> tag from cshtml, only other tags printed

提问人:wa1 提问时间:1/13/2023 最后编辑:wa1 更新时间:1/13/2023 访问量:27

问:

我即将将我的cshtml视图导出为pdf,它有效,但仅适用于文本,而不是图像/标签。请帮忙

这是我的CSHTML

@model VDITest_MuhammadRidwan.Models.MemberModel

@{
    ViewData["Title"] = "Details";
}

<h1>Details</h1>

<div id="PrintPDF">
    <h4>Member Details</h4>
    <hr />
        <img src='@Model.AvatarUrl' alt='avatar' style='width:250px; height:250px; border-radius:50%;' />
    <dl class="row">
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Name)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Name)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.NIK)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.NIK)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Address)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Address)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.Phone)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.Phone)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.BirthPlace)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.BirthPlace)
        </dd>
        <dt class="col-sm-2">
            @Html.DisplayNameFor(model => model.BirthDate)
        </dt>
        <dd class="col-sm-10">
            @Html.DisplayFor(model => model.BirthDate)
        </dd>
    </dl>
</div>

<div>


    <a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
    <a asp-action="Index">Back to List</a>
   <form method="post" asp-controller="Home" asp-action="ExportExcel">
        <input type="hidden" name="ExportData" id="ExportData" />
        <input type="submit" id="btnSubmit" value="Export" class="btn btn-primary" />
    </form>

</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type="text/javascript">
    $(function () {
        $("#btnSubmit").click(function () {
            $("input[name='ExportData']").val($("#PrintPDF").html());
        });
    });
</script>

这是我的控制器操作

       [HttpPost]
        public FileResult ExportExcel(string ExportData)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                StringReader reader = new StringReader(ExportData);
                Document PdfFile = new Document(PageSize.A4);
                PdfWriter writer = PdfWriter.GetInstance(PdfFile, stream);

                Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

                PdfFile.Open();
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, PdfFile, reader);
                PdfFile.Close();
                return File(stream.ToArray(), "application/pdf", "ExportData.pdf");
            }
        }

我已经调试了控制器以检查字符串 ExportData 参数,所以这是我从字符串 ExportData 参数中得到的

<h4>Member Details</h4>
<hr>
    <img src="/avatar\/0effac8e-7bb7-4727-a77d-165bb7eecf38bocilkematian.PNG" alt="avatar" style="width:250px; height:250px; border-radius:50%;">
<dl class="row">
    <dt class="col-sm-2">
        Name
    </dt>
    <dd class="col-sm-10">
        Joko
    </dd>
    <dt class="col-sm-2">
        Birth Date
    </dt>
    <dd class="col-sm-10">
        1233456789
    </dd>
    <dt class="col-sm-2">
        Address
    </dt>
    <dd class="col-sm-10">
        India
    </dd>
    <dt class="col-sm-2">
        Phone
    </dt>
    <dd class="col-sm-10">
        123456789654
    </dd>
    <dt class="col-sm-2">
        Birth Place
    </dt>
    <dd class="col-sm-10">
        Jombang
    </dd>
    <dt class="col-sm-2">
        Birth Date
    </dt>
    <dd class="col-sm-10">
        18/01/2023
    </dd>
</dl>

除图像外,全部转换为pdf。请帮忙,我是新来的,谢谢

我试图将我的cshtml转换为pdf,我希望所有cshtml都转换了,包括图像标签,但实际结果除了图像标签外都是打印的。

结果是这样的pdf result

C# ASP.NET-MVC PDF 剃刀 XHTML

评论

0赞 burnsi 1/14/2023
你能检查一下该路径上是否有图像吗?
0赞 wa1 1/14/2023
是的,当我检查详细信息页面时,图像显示没有问题

答: 暂无答案