2013-08-27 7 views
5

のエクスポート:codeは、私はこのコードを使用し、それが正常に動作しています、私はPDFファイルにhtmlページをエクスポートしたいと思いれるASP.NET MVC4アプリケーションを持っているASP.NET MVCでPDFファイル

このコードはhtmlページをオンラインPDFに変換します。ファイルを直接ダウンロードしたいと思います。

この結果を得るためにこのコードを変更するにはどうすればよいですか?

+2

関連するコードを投稿してください。人々はそこに行って記事全体を読むことを期待してはいけません。 –

答えて

5

については

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

使用:

これは(C#以下)VB.NETのためのものである

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

あなたの行動方法。 PDFFIleはファイル名です。 FileContentResultでC#

Public FileResult PDF(){ 
    return File("../PDFFile.pdf", "application/pdf"); 
} 
関連する問題