gpt4 book ai didi

javascript - 可编辑的cshtml查看页面另存为PDF

转载 作者:行者123 更新时间:2023-12-03 08:27:45 24 4
gpt4 key购买 nike

我正在尝试创建包含

的 Web 应用程序
  1. 可编辑的cshtml View 页面

然后

  • 点击按钮后将编辑的内容保存为 PDF
  • 这就是整个cshtml View 页面,

            @model IEnumerable<int>

    @{
    ViewBag.Title = "Create Template";
    }
    <!DOCTYPE html>
    <html>
    <head>
    <title>Create a Template</title>
    </head>
    <body>
    <div id='template'>
    <h1 contentEditable='True'>Product Name</h1>
    <div >
    <h2 contentEditable='True'>Product Description</h2>
    <p contentEditable='True'>London is the capital city of England.</p>
    </div>
    </div>

    <button id="btn_sumbit" type="button" class="btn btn-danger submit">Save as PDF</button>

    </body>
    </html>

    @section Scripts {

    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/jqueryui")

    <script type="text/javascript">

    $('#btn_sumbit').on('click', function () {

    var div_value = document.getElementById('template').innerHTML;

    RazorPDF.PdfResult(div_value, "PDF");
    });

    </script>

    }

    我正在使用RazorPDF执行此任务,但一旦我单击此按钮,它不会保存到 PDF

    我可以编辑这个cshtml View 页面,我想将最终编辑的内容保存为pdf(这是动态 View )

    最佳答案

    我认为你做错了。 “RazorPDF.PdfResult(...)”行属于 Controller 而不是 View 。

    观看此视频:http://nyveldt.com/blog/post/Introducing-RazorPDF它应该可以让您更清楚地了解 RazorPDF 中的工作原理。

    编辑:

    只需创建一个 Controller 方法,该方法根据参数生成 PDF View 。

    public ActionResult Pdf(string name, string description) {
    var product = new Product();
    product.Name = name;
    product.Description = description;

    var pdfResult = new PdfResult(product, "Pdf");

    return pdfResult;
    }

    当然,您需要创建一个包含信息的产品类。

    在你的 Javascript 部分你可以写:

    location.href = @Url.Action("Pdf", "Controllername") + "?name=" + name + "&description=" + description;

    这有望生成一个您可以在 Javascript 中跟踪的链接。 name 和description 是保存用户输入信息的Javascript 变量。

    你明白吗?我的方法是根据可编辑 View 的信息在不同的 View (如该视频中)中生成 PDF 内容。

    告诉我它是否有效。 ;-)

    关于javascript - 可编辑的cshtml查看页面另存为PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33475288/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com