在ASP.NET MVC中将HTML转换为PDF

Ram*_*war 6 asp.net-mvc pdf-conversion

我在一个项目中工作,需要当前的html页面转换为pdf,pdf将自动保存在服务器上的按钮点击,它的参考将保存在数据库中.如果数据来自数据库但数据可以转换视图这个表单是静态的,这意味着它在视图中有这么多单选按钮和文本框,我可以在其中编写详细信息并在单击保存按钮后选中复选框,它将保存在服务器上,并且其引用将保存在数据中基础.

原因是我不保存数据库中的数据是报告对客户端的使用较少,但如果我将数据保存在数据库中,那么数据库变得非常庞大并且处理变得复杂.因为该报告有大约100个字段.所以,如果任何人可以帮助我.

小智 5

您可以使用SelectPdf中的Free Html To Pdf Converter(http://selectpdf.com/community-edition/).

MVC的代码如下所示:

[HttpPost]
public ActionResult Convert(FormCollection collection)
{
    // read parameters from the webpage
    string url = collection["TxtUrl"];

    string pdf_page_size = collection["DdlPageSize"];
    PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true);

    string pdf_orientation = collection["DdlPageOrientation"];
    PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(
        typeof(PdfPageOrientation), pdf_orientation, true);

    int webPageWidth = 1024;
    try
    {
        webPageWidth = System.Convert.ToInt32(collection["TxtWidth"]);
    }
    catch { }

    int webPageHeight = 0;
    try
    {
        webPageHeight = System.Convert.ToInt32(collection["TxtHeight"]);
    }
    catch { }

    // instantiate a html to pdf converter object
    HtmlToPdf converter = new HtmlToPdf();

    // set converter options
    converter.Options.PdfPageSize = pageSize;
    converter.Options.PdfPageOrientation = pdfOrientation;
    converter.Options.WebPageWidth = webPageWidth;
    converter.Options.WebPageHeight = webPageHeight;

    // create a new pdf document converting an url
    PdfDocument doc = converter.ConvertUrl(url);

    // 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;
}
Run Code Online (Sandbox Code Playgroud)

VB.NET MVC版本的代码可以在这里找到:http://selectpdf.com/convert-from-html-to-pdf-in-asp-net-mvc-csharp-and-vb-net/

  • 免费版只能生成长达 5 页的文档 (2认同)

Ram*_*war 2

我使用 Canvas 转 PDF,这对我来说非常有用。这是相同的完美教程:https://www.freakyjolly.com/jspdf-multipage-example-generate-multipage-pdf-using-single-canvas-of-html-document-using-jspdf/

谢谢大家。