6

HTMLページをPDFファイルにエクスポートしたいASP.NET MVC4アプリケーションがあります。このコードを使用すると、正常に動作します:

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

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

4

3 に答える 3

6

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");
}
于 2013-08-27T15:58:41.233 に答える
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");
于 2013-08-27T15:58:30.917 に答える
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");
}
于 2013-08-27T15:58:36.443 に答える