如何以無頭方式將 Razor 視圖轉換為 PDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

查克尼思·賓

「無頭渲染」一詞指的是在沒有圖形使用者介面的情況下渲染網頁內容的過程。(圖形用戶介面)或瀏覽器視窗。 While theIronPdf.Extensions.Razor套件非常有用,但它不提供無頭渲染功能。 無頭渲染可以解決IronPdf.Extensions.Razor套件無法滿足的使用案例間隙。

We will utilize the Razor.Templating.Corecshtml轉換套件(Razor視圖)轉換為 HTML,然後使用 IronPDF 從中生成 PDF 文件。

本文靈感來自於下列 YouTube 影片:

開始使用 IronPDF

立即在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer


在 ASP.NET Core Web 應用程式中安裝 Razor.Templating.Core 套件,以將 Razor 視圖轉換為 HTML 文件。

Install-Package Razor.Templating.Core

將 Razor 視圖轉換為 PDFs

您將需要一個ASP.NET Core網頁應用程序(模型-視圖-控制器)將檢視轉換為 PDF 檔案的專案。

新增視圖

  • 在“Home”文件夾上點擊右鍵。 選擇「新增」和「添加視圖」。
  • 創建一個空的 Razor 視圖,並命名為 "Data.cshtml"。

    新增檢視

編輯 Data.cshtml 檔案

將您想要渲染為PDF的HTML字符串添加至以下位置:

<table class="table">
    <tr>
        <th>Name</th>
        <th>Title</th>
        <th>Description</th>
    </tr>
    <tr>
        <td>John Doe</td>
        <td>Software Engineer</td>
        <td>Experienced software engineer specializing in web development.</td>
    </tr>
    <tr>
        <td>Alice Smith</td>
        <td>Project Manager</td>
        <td>Seasoned project manager with expertise in agile methodologies.</td>
    </tr>
    <tr>
        <td>Michael Johnson</td>
        <td>Data Analyst</td>
        <td>Skilled data analyst proficient in statistical analysis and data visualization.</td>
    </tr>
</table>
<table class="table">
    <tr>
        <th>Name</th>
        <th>Title</th>
        <th>Description</th>
    </tr>
    <tr>
        <td>John Doe</td>
        <td>Software Engineer</td>
        <td>Experienced software engineer specializing in web development.</td>
    </tr>
    <tr>
        <td>Alice Smith</td>
        <td>Project Manager</td>
        <td>Seasoned project manager with expertise in agile methodologies.</td>
    </tr>
    <tr>
        <td>Michael Johnson</td>
        <td>Data Analyst</td>
        <td>Skilled data analyst proficient in statistical analysis and data visualization.</td>
    </tr>
</table>
HTML

編輯 Program.cs 文件

在「Program.cs」檔案內,新增以下程式碼。 下面的代碼使用 Razor.Templating.Core 庫中的 RenderAsync 方法將 Razor Views 轉換為 HTML。 其次,它實例化 ChromePdfRenderer 類別並將返回的HTML字符串傳遞給 RenderHtmlAsPdf 方法。 用戶可以利用RenderingOptions來訪問一系列功能,例如添加自定义文字,包括 HTML 標題和頁腳在生成的 PDF 中,定義自訂邊距,並應用頁碼.

app.MapGet("/PrintPdf", async () =>
{
    IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01";
    IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

    string html = await RazorTemplateEngine.RenderAsync("Views/Home/Data.cshtml");

    ChromePdfRenderer renderer = new ChromePdfRenderer();
    PdfDocument pdf = renderer.RenderHtmlAsPdf(html, "./wwwroot");

    return Results.File(pdf.BinaryData, "application/pdf", "razorViewToPdf.pdf");
});
app.MapGet("/PrintPdf", async () =>
{
    IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01";
    IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;

    string html = await RazorTemplateEngine.RenderAsync("Views/Home/Data.cshtml");

    ChromePdfRenderer renderer = new ChromePdfRenderer();
    PdfDocument pdf = renderer.RenderHtmlAsPdf(html, "./wwwroot");

    return Results.File(pdf.BinaryData, "application/pdf", "razorViewToPdf.pdf");
});
app.MapGet("/PrintPdf", Async Function()
	IronPdf.License.LicenseKey = "IRONPDF-MYLICENSE-KEY-1EF01"
	IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All

	Dim html As String = Await RazorTemplateEngine.RenderAsync("Views/Home/Data.cshtml")

	Dim renderer As New ChromePdfRenderer()
	Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html, "./wwwroot")

	Return Results.File(pdf.BinaryData, "application/pdf", "razorViewToPdf.pdf")
End Function)
VB   C#

更改資產連結

導航至「Views」資料夾 -> 「Shared」資料夾 -> 「_Layout.cshtml」檔案。在連結標籤中,將「~/」更改為「./」。

這很重要,因為“~/”與IronPDF不兼容。

執行專案

這將向您展示如何運行項目並生成PDF文檔。

執行ASP.NET Core MVC專案

輸出 PDF

下載 ASP.NET Core MVC 專案

您可以下載本指南的完整程式碼。它是一個壓縮檔,您可以在 Visual Studio 中作為 ASP.NET Core Web App 打開。(模型-視圖-控制器)專案。

點擊這裡下載專案。

Chaknith related to 下載 ASP.NET Core MVC 專案

查克尼思·賓

軟體工程師

Chaknith 是開發者界的夏洛克福爾摩斯。他第一次意識到自己可能有個軟體工程的未來,是在他為了娛樂而參加程式挑戰的時候。他的重點是 IronXL 和 IronBarcode,但他也引以為豪的是,他幫助客戶解決所有產品的問題。Chaknith 利用他與客戶直接對話中獲得的知識,以進一步改進產品。他的實際反饋超越了 Jira 工單,並支持產品開發、文件撰寫和行銷,以提升客戶的整體體驗。不在公司時,他通常在學習機器學習、寫程式和徒步旅行。