CSHTML 轉 PDF(MVC Core)

此程式碼範例展示了將視圖轉換為 PDF 的過程。

兩個套件,IronPdf.Extensions.Mvc.CoreIronPdf,共同協作以實現將視圖呈現為 PDF。 IronPdf.Extensions.Mvc.Core 套件作為 IronPdf 的擴展,啟用將視圖轉換為 PDF。

使用 RenderRazorViewToPdf 方法將視圖渲染為 PDF。 此方法需要一個 IRazorViewRenderer,".cshtml" 文件的路徑,以及顯示在 ".cshtml" 文件上的所需數據。請訪問如何將 ASP.NET Core MVC 中的視圖轉換為 PDF的文章以了解更多信息。

This action also enables you to access the full range of features provided by the RenderingOptions class, such as applying page numbers in PDFs with IronPDF, adding text and HTML headers and footers with IronPDF, and customizing PDF paper size. The resulting PDF document can be further edited or exported as needed.

class="hsg-featured-snippet">

如何在 ASP.NET MVC Core 中將 CSHTML 文件轉換為 PDF

  1. 安裝用於將 CSHTML 文件轉換為 PDF 的 C# 庫
  2. 實例化 ChromePdfRenderer
  3. 使用 RenderRazorViewToPdf 方法進行渲染
  4. IRazorViewRenderer 對象和 CSHTML 文件路徑傳遞給方法
  5. 將生成的 PDF 文件下載到桌面

學習如何在 ASP.NET MVC Core 中將 CSHTML 視圖轉換為 PDF。

準備好開始了嗎?
Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布