6

Rotativaツールを使用してPDFを表示しています。次のコードで正常に動作します。

public ActionResult PreviewDocument()
{

     var htmlContent = Session["html"].ToString();
     var model = new PdfInfo { Content = htmlContent, Name = "PDF Doc" };
     return new ViewAsPdf(model);
}

ボタンをクリックしてブラウザの「名前を付けて保存」ダイアログからPDFをダウンロードし、一部のiframeに表示しない方法を知りたいと思いました。「newViewAsPdf(model)」はPDFデータを返すだけです。

前もって感謝します。

4

3 に答える 3

11

次のように、Rotativa呼び出しに属性を追加できます。

return new PartialViewAsPdf("PreviewDocument", pdfModel)
                   {
                       PageSize = Size.A4,
                       FileName = "PDF Doc.pdf"
                   };

そして、それはあなたのためにファイルを作成します。:)

于 2015-01-06T07:37:04.823 に答える
4

私はついにこれを行う方法を手に入れました。

実際、rotativaのメソッド "return new ViewAsPdf(model)"はHttpResponseStreamを返します。なかなかできないところ。ただし、カスタム属性を使用してアクションが実行されると、応答を変更/変更できます。アクションフィルターのOnResultExecuted()メソッドをオーバーライドできます。

コントローラーのアクション

[HttpGet]
[ActionDownload] //here a custom action filter added
public ActionResult DownloadDocument()
{   
    var htmlContent = "<h1>sachin Kumar</hi>";

    var model = new PdfInfo {FtContent = htmlContent, FtName = "Populate Form"};
    return new ViewAsPdf(model);
}

カスタムアクションフィルター:

public class ActionDownloadAttribute : ActionFilterAttribute
{
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {   
            //Add content-disposition header to response so that browser understand to download as an attachment.   
        filterContext.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + "Report.pdf"); 

            base.OnResultExecuted(filterContext);
    }
}
于 2013-01-09T12:23:38.550 に答える
3

returnnewActionAsPdfを使用できます。カスタム属性などは必要ありません。例:https ://github.com/webgio/Rotativa/

public ActionResult PrintPreviewDocument()
{
    return new ActionAsPdf("PreviewDocument") { FileName = "PDF Doc.pdf" };
}

public ActionResult PreviewDocument()
{

    var htmlContent = Session["html"].ToString();
    var model = new PdfInfo { Content = htmlContent, Name = "PDF Doc" };
    return View(model);
}
于 2013-10-01T14:31:48.493 に答える