CSHTML 轉換為 PDF (MVC Core)
此程式碼範例示範了將視圖轉換為 PDF 的過程。
兩個軟體包 IronPdf.Extensions.Mvc.Core 和 IronPdf 協同工作,以實現將視圖渲染為 PDF。 IronPdf.Extensions.Mvc.Core 套件是 IronPdf 的擴展,能夠將視圖渲染為 PDF。
Use the RenderRazorViewToPdf method to render Views to PDFs. This method requires an IRazorViewRenderer, the path to the ".cshtml" file, and the data required to display on the ".cshtml" file. Please visit the How to Convert View to PDF in ASP.NET Core MVC how-to article to learn more.
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.
如何在ASP.NET MVC Core 中將 CSHTML 檔案轉換為 PDF
- 安裝用於將 CSHTML 檔案轉換為 PDF 的 C# 庫
- 實例化`ChromePdfRenderer`類
- 使用`RenderRazorViewToPdf`方法進行渲染
- 將`IRazorViewRenderer`物件和 CSHTML 檔案路徑傳遞給該方法
- 將產生的 PDF 文件下載到桌面

