[英]Exporting a PDF-file with ASP.NET MVC
我有一個ASP.NET MVC4應用程序,我想在其中導出一個HTML頁面到PDF文件,我使用這個代碼,它的工作正常: 代碼
此代碼將html頁面轉換為在線PDF,我想直接下載該文件。
如何更改此代碼以獲得此結果?
使用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");
}
將其作為附件並在返回結果時為其指定文件名:
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");
采用:
這是針對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");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.