7

Rotativa ライブラリによって生成された PDF でヘッダーとフッターを指定しようとしています。著者がここで答えたように、CSS (ここで説明) を使用して可能にする必要があります。しかし、私はこれを行うことができません。

meta タグにスタイルシートをロードしました。

<link href="print.css" rel="stylesheet" type="text/css" media="print" />

そして、一番下のスタイルシートで:

@page {
    @top-left {
        content: "TOP SECRET";
        color: red
    }
    @bottom-right {
        content: counter(page);
        font-style: italic
    }
}

次に、次の方法で PDF を生成します。

public ActionResult ShowPdf()
{
     var model = new Model();
     return new ViewAsPdf("view.cshtml", model)
                {
                    FileName = "Report.pdf",
                    CustomSwitches = "--print-media-type"
                };
}

そして、PDF のヘッダーとフッターには何も表示されません。何か案は?

4

5 に答える 5

12

wkhtmltopdf (またはアーカイブされたより優れたバージョン)のドキュメントを見つけました。そこには、ヘッダーとフッターの管理方法が記載されています。

--header-center "text"基本的に、引数リストに(または同様のスイッチを)追加するだけで済みます。

Rotativa で使用すると、次のようになります。

public ActionResult ShowPdf()
{
     var model = new Model();
     return new ViewAsPdf("view.cshtml", model)
                {
                    FileName = "Report.pdf",
                    CustomSwitches = "--print-media-type --header-center \"text\""
                };
}

(必要かどうかはわかりません--print-media-type。)

于 2013-03-18T14:09:31.927 に答える
9

ヘッダー/フッターにテキストの代わりにビューを表示したい場合は、次のようにできます。

public ActionResult ViewPDF()
{
      string customSwitches = string.Format("--print-media-type --allow {0} --footer-html {0} --footer-spacing -10",
                Url.Action("Footer", "Document", new { area = ""}, "https"));


     return new ViewAsPdf("MyPDF.cshtml", model)
                {
                    FileName = "MyPDF.pdf",
                    CustomSwitches = customSwitches
                };
}

[AllowAnonymous]
public ActionResult Footer()
{
    return View();
}

フッター アクションに [AllowAnonymous] 属性を追加することを忘れないでください。そうしないと、Rotatina はパスにアクセスできません。

于 2014-10-24T09:16:17.177 に答える
5

これが私がやった方法です(完全に):

public ActionResult PrintPDF(int? selectedSiteRotaId, int selectedSiteId)
{
    string footer = "--footer-center \"Printed on: " + DateTime.Now.Date.ToString("MM/dd/yyyy") + "  Page: [page]/[toPage]\"" + " --footer-line --footer-font-size \"9\" --footer-spacing 6 --footer-font-name \"calibri light\"";

    return new ActionAsPdf("RenderPDF", new { selectedSiteRotaId = selectedSiteRotaId, selectedSiteId = 7 }) 
    {
        FileName = "PDF_Output.pdf",
        PageOrientation = Orientation.Landscape,
        MinimumFontSize = 10, 
        //PageMargins  = new Margins(5,5,5,5),
        PageSize = Size.A3,
        CustomSwitches = footer
    };

    //var pdfResult = new ActionAsPdf("RenderPDF", new { selectedSiteRotaId = selectedSiteRotaId, selectedSiteId = 7 })
    //{
    //    FileName = "PDF_Output.pdf",
    //    PageOrientation = Orientation.Landscape,
    //    MinimumFontSize = 10
    //};

    //var binary = pdfResult.BuildPdf(this.ControllerContext);

    //return File(binary, "application/pdf");
}


public ActionResult RenderPDF(int? selectedSiteRotaId, int selectedSiteId)
{
    return RedirectToAction("Index", "PrintPDF", new { selectedSiteRotaId = selectedSiteRotaId, selectedSiteId = 7 });
}
于 2016-01-05T12:42:31.463 に答える