简体   繁体   English

从视图Mvc 3生成pdf

[英]Generate a pdf from view Mvc 3

Today I received a task to generate a pdf from view .... as I am beginner in programming ... someone would help me with this task .. passing some tips .. where to start researching. 今天,我收到了一个从视图...生成pdf的任务。作为我的编程初学者,有人会帮助我完成此任务。 Pos'm having difficulty doing this task. Pos无法完成此任务。 I tried to use an example in this link http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3 but it always throws an error in this part of the code 我试图在此链接中使用示例http://www.codeproject.com/Articles/260470/PDF-reporting-using-ASP-NET-MVC3,但它总是在此部分代码中引发错误

public byte[] Render(string htmlText, string pageTitle)
{
    byte[] renderedBuffer;

    using (var outputMemoryStream = new MemoryStream())
    {
        using (var pdfDocument = new Document(PageSize.A4, HorizontalMargin, HorizontalMargin, VerticalMargin, VerticalMargin))
        {
            PdfWriter pdfWriter = PdfWriter.GetInstance(pdfDocument, outputMemoryStream);
            pdfWriter.CloseStream = false;
            pdfWriter.PageEvent = new PrintHeaderFooter { Title = pageTitle };
            pdfDocument.Open();
            using (var htmlViewReader = new StringReader(htmlText))
            {
                using (var htmlWorker = new HTMLWorker(pdfDocument))
                {
                    htmlWorker.Parse(htmlViewReader);// erro here 
                }
            }
        }

        renderedBuffer = new byte[outputMemoryStream.Position];
        outputMemoryStream.Position = 0;
        outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
    }

    return renderedBuffer;
}

It is not answer to your question per se, but we are mostly using NReco.PDF 它本身并不能回答您的问题,但是我们主要使用的是NReco.PDF

And example of the code to generate PDF from HTML content would be: 从HTML内容生成PDF的代码示例为:

var htmlContent = String.Format("<body>Hello world: {0}</body>", 
    DateTime.Now);
var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();  
var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);

So only thing missing in this example is getting output of you MVC controller as HTML. 因此,此示例中唯一缺少的是将MVC控制器的输出作为HTML输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM