2013-08-27 11 views
5

У меня есть приложение ASP.NET MVC4, в котором я хотел бы экспортировать HTML страницу в PDF-файл, я использую этот код и он работает отлично: codeЭкспорт PDF-файл с помощью ASP.NET MVC

Этот код преобразует html-страницу в онлайн-PDF, я хотел бы загрузить непосредственно файл.

Как я могу изменить этот код для получения этого результата?

+2

Пожалуйста отправьте соответствующий код. Вы не должны ожидать, что люди пойдут туда и прочитают всю статью. –

ответ

5

С в FileContentResult:

protected FileContentResult ViewPdf(string pageTitle, string viewName, object model) 
{ 
    // Render the view html to a string. 
    string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model); 

    // Let the html be rendered into a PDF document through iTextSharp. 
    byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle); 

    // Return the PDF as a binary stream to the client. 
    return File(buffer, "application/pdf","file.pdf"); 
} 
3

Сделать это как вложение и дать ему имя при возврате результата:

protected ActionResult ViewPdf(string pageTitle, string viewName, object model) 
{ 
    // Render the view html to a string. 
    string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model); 

    // Let the html be rendered into a PDF document through iTextSharp. 
    byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle); 

    // Return the PDF as a binary stream to the client. 
    return File(buffer, "application/pdf", "myfile.pdf"); 
} 

Что делает файл появится в качестве вложения и всплывающее окно Сохранить как диалоговое окно следующая строка:

return File(buffer, "application/pdf", "myfile.pdf"); 
1

Использование:

Это для VB.NET (C# ниже)

Public Function PDF() As FileResult 
     Return File("../PDFFile.pdf", "application/pdf") 
    End Function 

В вашем методе действий. Где PDFFIle - это ваше имя файла.

Для C#

Public FileResult PDF(){ 
    return File("../PDFFile.pdf", "application/pdf"); 
} 
Смежные вопросы