跳過到頁腳內容
使用IRONPDF

如何將ASP.NET MVC視圖轉換為PDF:IronPDF vs iTextSharp

Full Comparison

Looking for a detailed feature-by-feature breakdown? See how IronPDF stacks up against Itext on pricing, HTML support, and licensing.

View Full Comparison

將 ASP.NET MVC 檢視轉換為 PDF 文件,是現代網路應用程式的一項核心需求。 無論您是生成發票、報告還是證書,挑戰都很明確:如何將 Razor 檢視轉為能完整保留格式與樣式的 Professional PDF 檔案? 本指南將傳統的 iTextSharp 函式庫與 IronPDF(基於 Chromium 的現代化解決方案)進行比較,以便您為 PDF 生成工作流程選擇合適的工具。

IronPDF 與 iTextSharp MVC 在 C# 中將視圖轉換為 PDF 檔案比較:圖 1 - IronPDF

為何需要將 MVC 檢視轉換為 PDF?

企業在無數關鍵作業中皆仰賴 PDF 生成功能。 發票系統需要具備防篡改功能的帳單文件。 人力資源部門負責產生僱用證明和合約。 銷售團隊負責製作報價單和方案。 教育平台頒發結業證書。 每個場景都需要伺服器端產生 PDF,以在所有裝置和平台上保持一致的格式。

根據 ASP.NET Core 文件所述,Razor 檢視提供了一套出色的範本系統,可用於產生動態內容,並將其轉換為 PDF。 關鍵問題在於,哪個函式庫能為您提供最精準、易於維護且符合法律規範的解決方案。

IronPDF 與 iTextSharp MVC 在 C# 中將視圖轉換為 PDF 檔案比較:圖 2 - 功能

iTextSharp 如何處理 MVC 到 PDF 的轉換?

十多年來,iTextSharp 一直是 .NET PDF 生成領域的主力工具。 它最初是從 Java iText 庫移植過來的,提供了對 PDF 創建的底層控制。 然而,它在處理 HTML 轉換方面的方法顯得過時了,尤其是在處理現代網頁內容時。

安裝 iTextSharp

若要將 iTextSharp 加入您的 ASP.NET Core MVC 專案,請透過套件管理主控台安裝 NuGet 套件:

Install-Package iTextSharp
Install-Package iTextSharp
SHELL

IronPDF 與 iTextSharp MVC 在 C# 中將視圖轉換為 PDF 檔案:圖 3 - 安裝 iTextSharp

iTextSharp 的基本實現

以下是一個完整的範例,展示如何在 ASP.NET MVC 專案中使用 iTextSharp 的 XMLWorkerHelper 類別將 MVC 視圖轉換為 PDF:

using iTextSharp.text;
using iTextSharp.tool.xml;
using iTextSharp.text.pdf;
using System.IO;

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult GeneratePDF()
    {
        // Create a simple invoice HTML
        string invoiceHtml = @"
            <h1>Invoice #1001</h1>
            <p>Date: " + DateTime.無w.ToString("MM/dd/yyyy") + @"</p>
            <table border='1'>
                <tr><th>Item</th><th>Price</th></tr>
                <tr><td>Product A</td><td>$99.99</td></tr>
                <tr><td>Product B</td><td>$149.99</td></tr>
            </table>
            <p><strong>Total: $249.98</strong></p>";

        // Create PDF document using iTextSharp
        using var stream = new MemoryStream();
        var document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.GetInstance(document, stream);
        document.Open();

        using (var srHtml = new StringReader(invoiceHtml))
        {
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, srHtml);
        }

        document.Close();

        // Return the PDF file
        return new FileContentResult(stream.ToArray(), "application/pdf")
        {
            FileDownloadName = "invoice.pdf"
        };
    }
}
using iTextSharp.text;
using iTextSharp.tool.xml;
using iTextSharp.text.pdf;
using System.IO;

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult GeneratePDF()
    {
        // Create a simple invoice HTML
        string invoiceHtml = @"
            <h1>Invoice #1001</h1>
            <p>Date: " + DateTime.無w.ToString("MM/dd/yyyy") + @"</p>
            <table border='1'>
                <tr><th>Item</th><th>Price</th></tr>
                <tr><td>Product A</td><td>$99.99</td></tr>
                <tr><td>Product B</td><td>$149.99</td></tr>
            </table>
            <p><strong>Total: $249.98</strong></p>";

        // Create PDF document using iTextSharp
        using var stream = new MemoryStream();
        var document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.GetInstance(document, stream);
        document.Open();

        using (var srHtml = new StringReader(invoiceHtml))
        {
            XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, srHtml);
        }

        document.Close();

        // Return the PDF file
        return new FileContentResult(stream.ToArray(), "application/pdf")
        {
            FileDownloadName = "invoice.pdf"
        };
    }
}
Imports iTextSharp.text
Imports iTextSharp.tool.xml
Imports iTextSharp.text.pdf
Imports System.IO

Public Class HomeController
    Inherits Controller

    Public Function Index() As ActionResult
        Return View()
    End Function

    Public Function GeneratePDF() As ActionResult
        ' Create a simple invoice HTML
        Dim invoiceHtml As String = "
            <h1>Invoice #1001</h1>
            <p>Date: " & DateTime.Now.ToString("MM/dd/yyyy") & "</p>
            <table border='1'>
                <tr><th>Item</th><th>Price</th></tr>
                <tr><td>Product A</td><td>$99.99</td></tr>
                <tr><td>Product B</td><td>$149.99</td></tr>
            </table>
            <p><strong>Total: $249.98</strong></p>"

        ' Create PDF document using iTextSharp
        Using stream As New MemoryStream()
            Dim document As New Document(PageSize.A4)
            Dim writer As PdfWriter = PdfWriter.GetInstance(document, stream)
            document.Open()

            Using srHtml As New StringReader(invoiceHtml)
                XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, srHtml)
            End Using

            document.Close()

            ' Return the PDF file
            Return New FileContentResult(stream.ToArray(), "application/pdf") With {
                .FileDownloadName = "invoice.pdf"
            }
        End Using
    End Function
End Class
$vbLabelText   $csharpLabel

此範例將 HTML 內容轉為基本的 PDF 文件。 XMLWorkerHelper 類別處理 HTML 字串,並使用物件模型為 PDF 文件新增元素。 PdfWriter 處理實際的 PDF 生成過程,而 Document 管理頁面結構。

輸出

IronPDF 與 iTextSharp MVC 在 C# 中將視圖轉換為 PDF 檔案:圖 4 - PDF 輸出

iTextSharp 的主要限制有哪些?

XMLWorkerHelper 類別支援基本的 HTML 標籤和內嵌 CSS。 現代 CSS3 屬性、flexbox 佈局及網格系統無法呈現。 依賴 JavaScript 的內容完全消失。 漸層、陰影和變換等複雜樣式將被忽略。 即使是標準的 Bootstrap 類別也無法套用,導致精心設計的介面看起來單調且不專業。

許多開發者在 Stack Overflow 上報告了這些限制,導致在使用 iTextSharp 將 MVC 視圖轉換為 PDF 時感到沮喪。

或許更令人擔憂的是 iTextSharp 的授權模式。 該庫使用 AGPL 許可證,如果您使用免費版本,則需要將整個應用程式開源。 商業授權的年費起價為數千美元,對許多企業而言成本過高,難以負擔。 這種授權限制迫使許多開發者尋求更符合商業開發需求的替代方案。 正如微軟的 .NET 文件所述,對於商業專案而言,選擇具備適當授權的函式庫至關重要。

IronPDF 與 iTextSharp MVC 在 C# 中建立 PDF 檔案的對比:圖 5 - IronPDF 與 iTextSharp MVC 在 C# 中建立 PDF 檔案的對比

如何使用現代化函式庫將 MVC 檢視轉換為 PDF?

IronPDF 代表了在 ASP.NET Core MVC 中生成 PDF 的現代化解決方案。 它基於 Chromium 渲染引擎構建,可將 HTML 轉換為 PDF,並保持其在 Google Chrome 中的顯示效果,同時保留所有樣式、JavaScript 執行和響應式設計元素。

如何安裝 IronPDF?

透過 NuGet 套件管理員主控台將 IronPDF 加入您的專案:

Install-Package IronPdf
Install-Package IronPdf
SHELL

IronPDF 與 iTextSharp MVC 在 C# 中將視圖轉換為 PDF 檔案:圖 6 - 安裝

採用 IronPDF 的現代實現

上述建立的同一張發票,現在透過 IronPDF 的 ChromePdfRenderer 採用現代 CSS 及完整的 HTML5 渲染:

using IronPdf;

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult GenerateModernPDF()
    {
        // Create a styled invoice HTML with modern CSS
        string invoiceHtml = @"
            <style>
                body { font-family: 'Segoe UI', Arial; padding: 40px; }
                .invoice-header {
                    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                    color: white;
                    padding: 30px;
                    border-radius: 10px;
                }
                table {
                    width: 100%;
                    border-collapse: collapse;
                    margin: 20px 0;
                }
                th {
                    background-color: #f3f4f6;
                    padding: 12px;
                    text-align: left;
                }
                td {
                    padding: 12px;
                    border-bottom: 1px solid #e5e7eb;
                }
                .total {
                    font-size: 24px;
                    color: #10b981;
                    text-align: right;
                    margin-top: 20px;
                }
            </style>
            <div class='invoice-header'>
                <h1>Invoice #1001</h1>
                <p>Date: " + DateTime.無w.ToString("MMMM dd, yyyy") + @"</p>
            </div>
            <table>
                <tr><th>Item</th><th>Quantity</th><th>Price</th></tr>
                <tr><td>Premium Package</td><td>1</td><td>$99.99</td></tr>
                <tr><td>Professional Services</td><td>2</td><td>$149.99</td></tr>
            </table>
            <div class='total'>Total: $249.98</div>
            <p>Page numbers and additional content can be added to each page.</p>";

        // Use Chromium engine for rendering
        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf(invoiceHtml);

        // Set content disposition for download
        Response.Headers.Append("Content-Disposition", "attachment; filename=modern-invoice.pdf");

        // Return the PDF file with binary data
        return new FileContentResult(pdf.BinaryData, "application/pdf");
    }
}
using IronPdf;

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult GenerateModernPDF()
    {
        // Create a styled invoice HTML with modern CSS
        string invoiceHtml = @"
            <style>
                body { font-family: 'Segoe UI', Arial; padding: 40px; }
                .invoice-header {
                    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                    color: white;
                    padding: 30px;
                    border-radius: 10px;
                }
                table {
                    width: 100%;
                    border-collapse: collapse;
                    margin: 20px 0;
                }
                th {
                    background-color: #f3f4f6;
                    padding: 12px;
                    text-align: left;
                }
                td {
                    padding: 12px;
                    border-bottom: 1px solid #e5e7eb;
                }
                .total {
                    font-size: 24px;
                    color: #10b981;
                    text-align: right;
                    margin-top: 20px;
                }
            </style>
            <div class='invoice-header'>
                <h1>Invoice #1001</h1>
                <p>Date: " + DateTime.無w.ToString("MMMM dd, yyyy") + @"</p>
            </div>
            <table>
                <tr><th>Item</th><th>Quantity</th><th>Price</th></tr>
                <tr><td>Premium Package</td><td>1</td><td>$99.99</td></tr>
                <tr><td>Professional Services</td><td>2</td><td>$149.99</td></tr>
            </table>
            <div class='total'>Total: $249.98</div>
            <p>Page numbers and additional content can be added to each page.</p>";

        // Use Chromium engine for rendering
        var renderer = new ChromePdfRenderer();
        var pdf = renderer.RenderHtmlAsPdf(invoiceHtml);

        // Set content disposition for download
        Response.Headers.Append("Content-Disposition", "attachment; filename=modern-invoice.pdf");

        // Return the PDF file with binary data
        return new FileContentResult(pdf.BinaryData, "application/pdf");
    }
}
Imports IronPdf

Public Class HomeController
    Inherits Controller

    Public Function Index() As ActionResult
        Return View()
    End Function

    Public Function GenerateModernPDF() As ActionResult
        ' Create a styled invoice HTML with modern CSS
        Dim invoiceHtml As String = "
            <style>
                body { font-family: 'Segoe UI', Arial; padding: 40px; }
                .invoice-header {
                    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                    color: white;
                    padding: 30px;
                    border-radius: 10px;
                }
                table {
                    width: 100%;
                    border-collapse: collapse;
                    margin: 20px 0;
                }
                th {
                    background-color: #f3f4f6;
                    padding: 12px;
                    text-align: left;
                }
                td {
                    padding: 12px;
                    border-bottom: 1px solid #e5e7eb;
                }
                .total {
                    font-size: 24px;
                    color: #10b981;
                    text-align: right;
                    margin-top: 20px;
                }
            </style>
            <div class='invoice-header'>
                <h1>Invoice #1001</h1>
                <p>Date: " & DateTime.Now.ToString("MMMM dd, yyyy") & "</p>
            </div>
            <table>
                <tr><th>Item</th><th>Quantity</th><th>Price</th></tr>
                <tr><td>Premium Package</td><td>1</td><td>$99.99</td></tr>
                <tr><td>Professional Services</td><td>2</td><td>$149.99</td></tr>
            </table>
            <div class='total'>Total: $249.98</div>
            <p>Page numbers and additional content can be added to each page.</p>"

        ' Use Chromium engine for rendering
        Dim renderer As New ChromePdfRenderer()
        Dim pdf = renderer.RenderHtmlAsPdf(invoiceHtml)

        ' Set content disposition for download
        Response.Headers.Append("Content-Disposition", "attachment; filename=modern-invoice.pdf")

        ' Return the PDF file with binary data
        Return New FileContentResult(pdf.BinaryData, "application/pdf")
    End Function
End Class
$vbLabelText   $csharpLabel

ChromePdfRenderer會將 HTML 完全按照 Chrome 的顯示方式進行轉換。 漸層背景、現代字型及精緻的樣式皆能準確呈現。 此渲染器可處理 iTextSharp 無法處理的複雜 CSS 屬性,包括彈性盒模型 (flexbox)、網格佈局 (grid layouts),以及以靜態框架形式呈現的 CSS 變換。 RenderHtmlAsPdf 方法將 HTML 在 MVC 中轉換為 PDF 變得簡單。

輸出

IronPDF 與 iTextSharp MVC 在 C# 中將視圖轉換為 PDF 檔案比較:圖 7 - IronPDF 輸出

立即開始免費試用,體驗由 Chrome 驅動的渲染引擎所帶來的現代化 PDF 生成功能。

IronPDF 與 iTextSharp MVC 在 C# 中將視圖轉換為 PDF 文件比較:圖 8 - 跨平台相容性

該如何在這兩款 PDF 函式庫之間做出選擇?

在比較 iTextSharp 與 IronPDF for .NET 用於 .NET MVC 視圖轉換時,渲染品質、開發者體驗及授權方式等幾個因素,使這兩套函式庫呈現明顯差異。

功能對比

iTextSharp 與 IronPDF 功能比較
特點 iTextSharp IronPDF
HTML5 支援 限額 完整
CSS3 渲染 僅限基礎翻譯 完成
JavaScript 執行
Flexbox/Grid 支援
Bootstrap 相容性 部分 完整
Web 字型支援 限額 完成
SVG圖形
響應設計
API 的複雜性 低階 高階

許可證上的考慮因素

這些庫之間的許可差異會對商業開發產生重大影響。 iTextSharp 的 AGPL 授權協議帶來了許多企業無法接受的法律義務。 使用免費版本需要開源您的整個應用程式,包括專有業務邏輯。 除非購買昂貴的商業許可證,否則此限制使得 iTextSharp 不適用於大多數商業項目。

IronPDF 為單一開發者提供簡單易懂的商業許可,起價為 $799。 該許可包含一年的更新和支援,但您無需開源您的應用程式。 這種透明的定價模式符合典型的軟體開發預算,使得各種規模的企業在將 MVC 視圖轉換為 C# 中的 PDF 時,都能輕鬆獲得專業的 PDF 生成功能。

IronPDF 與 iTextSharp MVC 在 C# 中建立 PDF 文件視圖的比較:圖 9 - 許可

輸出品質分析

比較輸出結果,渲染品質的差異會立即顯現出來。 iTextSharp 產生的 PDF 檔案基本上類似於 2000 年代初期的文件。 表格缺乏適當的樣式,字體預設使用系統標準,現代設計元素完全消失。 生成的 PDF 文件看起來不夠專業,與您的應用程式品牌形像不符。

IronPDF 可產生與您的網頁設計完美匹配、像素級精準的 PDF 檔案。 漸層效果能精準呈現,自訂字型顯示正確,且複雜的版面配置能維持其結構。 Chromium 引擎確保您的 PDF 文件與網頁檢視效果完全一致,使所有生成的文件都能保持品牌識別度與 Professional 外觀。

PDF Renderer 提供了哪些進階功能?

除了基本的 HTML 轉換功能外,IronPDF 還提供 Enterprise 級功能,可加速 Professional PDF 的生成。 完整的 API 文件展示了該函式庫在將 MVC 檢視轉換為 PDF 方面的廣泛功能。

頁首和頁尾

添加帶有動態內容的專業頁首和頁尾:

var renderer = new ChromePdfRenderer
{
    RenderingOptions = new ChromePdfRenderOptions
    {
        HtmlHeader = new HtmlHeaderFooter
        {
            MaxHeight = 25,
            HtmlFragment = "<div style='text-align: center'>Company Name</div>"
        },
        HtmlFooter = new HtmlHeaderFooter
        {
            MaxHeight = 20,
            HtmlFragment = "<center>Page {page} of {total-pages}</center>"
        }
    }
};
var renderer = new ChromePdfRenderer
{
    RenderingOptions = new ChromePdfRenderOptions
    {
        HtmlHeader = new HtmlHeaderFooter
        {
            MaxHeight = 25,
            HtmlFragment = "<div style='text-align: center'>Company Name</div>"
        },
        HtmlFooter = new HtmlHeaderFooter
        {
            MaxHeight = 20,
            HtmlFragment = "<center>Page {page} of {total-pages}</center>"
        }
    }
};
Dim renderer = New ChromePdfRenderer With {
    .RenderingOptions = New ChromePdfRenderOptions With {
        .HtmlHeader = New HtmlHeaderFooter With {
            .MaxHeight = 25,
            .HtmlFragment = "<div style='text-align: center'>Company Name</div>"
        },
        .HtmlFooter = New HtmlHeaderFooter With {
            .MaxHeight = 20,
            .HtmlFragment = "<center>Page {page} of {total-pages}</center>"
        }
    }
}
$vbLabelText   $csharpLabel

此配置可使每個頁面呈現一致的品牌形象。 佔位符 {page}{total-pages} 會自動填入正確的值,確保文件中的分頁準確無誤。 如需更多設定選項,請參閱頁首與頁尾指南

安全與加密

利用 IronPDF 的安全功能,透過密碼和權限保護敏感文件:

var pdf = renderer.RenderHtmlAsPdf(html);

// Add password protection
pdf.SecuritySettings.UserPassword = "user123";
pdf.SecuritySettings.OwnerPassword = "owner456";

// Set permissions
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.完整PrintRights;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
var pdf = renderer.RenderHtmlAsPdf(html);

// Add password protection
pdf.SecuritySettings.UserPassword = "user123";
pdf.SecuritySettings.OwnerPassword = "owner456";

// Set permissions
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.完整PrintRights;
pdf.SecuritySettings.AllowUserCopyPasteContent = false;
Dim pdf = renderer.RenderHtmlAsPdf(html)

' Add password protection
pdf.SecuritySettings.UserPassword = "user123"
pdf.SecuritySettings.OwnerPassword = "owner456"

' Set permissions
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.完整PrintRights
pdf.SecuritySettings.AllowUserCopyPasteContent = False
$vbLabelText   $csharpLabel

這些安全設定可防止未經授權的訪問,並控制在將 MVC 視圖轉換為 PDF 時,收件人如何與您的 PDF 互動。 您可以限制列印、複製和編輯功能,同時仍能完全掌控所有權。 請參閱 PDF 安全文件以獲取更多資訊。

輸出

IronPDF 與 iTextSharp MVC 在 C# 中將視圖轉換為 PDF 檔案:圖 10 - 安全性 PDF 輸出

表單欄位處理

IronPDF 能將 HTML 表單轉換為互動式 PDF 表單,無需額外工具:

string formHtml = @"
    <form>
        <label>Name:</label>
        <input type='checkbox'> Accept Terms
        <select name='country'>
            <option>USA</option>
            <option>Canada</option>
        </select>
    </form>";

var pdf = renderer.RenderHtmlAsPdf(formHtml);
string formHtml = @"
    <form>
        <label>Name:</label>
        <input type='checkbox'> Accept Terms
        <select name='country'>
            <option>USA</option>
            <option>Canada</option>
        </select>
    </form>";

var pdf = renderer.RenderHtmlAsPdf(formHtml);
Imports System

Dim formHtml As String = "
    <form>
        <label>Name:</label>
        <input type='checkbox'> Accept Terms
        <select name='country'>
            <option>USA</option>
            <option>Canada</option>
        </select>
    </form>"

Dim pdf = renderer.RenderHtmlAsPdf(formHtml)
$vbLabelText   $csharpLabel

產生的 PDF 文件保留了表單互動性,允許使用者直接在 PDF 閱讀器中填寫欄位。 此功能讓您無需另行使用表單建立工具,從而簡化您的文件工作流程。 請參閱表單文件以了解更多選項。

輸出

IronPDF 與 iTextSharp MVC 在 C# 中將視圖轉換為 PDF 檔案:圖 11 - 表單輸出

如何解決常見的 PDF 生成問題?

即使使用現代化函式庫,某些挑戰仍需特定解決方案,方能確保產生最佳的 PDF 輸出。 以下範例針對開發人員在將 MVC 視圖轉換為 PDF 時最常遇到的問題提供解決方案。

CSS渲染優化

為了在將 HTML 檔案轉換為 PDF 格式時獲得最佳的複雜 CSS 效果,請使用列印媒體查詢:

renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print
$vbLabelText   $csharpLabel

此設定會套用專用於列印的 CSS 規則,以優化 PDF 輸出佈局,而非螢幕顯示效果。 結合針對列印優化的樣式表,您便能精確控制分頁、邊距及排版。 CSS 媒體類型指南說明了其他渲染選項。

JavaScript 執行時間

在轉換動態內容時,請預留足夠時間讓 JavaScript 執行完畢,再進行 PDF 擷取。 這可確保所有 AJAX 呼叫和 DOM 操作均能順利完成:

renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.RenderDelay = 500; // milliseconds
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.WaitFor.RenderDelay = 500; // milliseconds
$vbLabelText   $csharpLabel

此設定可確保在開始生成 PDF 之前,AJAX 呼叫已完成且 DOM 操作已結束,從而擷取 MVC 視圖的完整渲染狀態。 針對圖表函式庫及其他大量使用非同步操作的內容,您可能需要增加延遲值。

輸出

IronPDF 與 iTextSharp MVC 在 C# 中將視圖轉換為 PDF 檔案比較:圖 12 - JavaScript 執行輸出

字型嵌入和文件格式設定

請透過將自訂字型直接嵌入 HTML 中(使用 base64 編碼的資料 URI),確保字型能正確顯示:

@font-face {
    font-family: 'CustomFont';
    src: url(data:font/woff2;base64,[base64-encoded-font]) format('woff2');
}

將字體直接嵌入 HTML 中,可確保在所有環境下呈現一致的效果,從而消除生成的 PDF 文件中字體缺失的問題。 無論您使用基於 URL 的渲染(透過RenderUrlAsPdf還是基於字串的渲染(透過 RenderHtmlAsPdf),此方法都同樣有效。

如需更多設定範例(包括頁面大小、方向及邊距控制),請參閱 IronPDF 渲染選項文件。 您亦可參閱 GitHub 上的範例專案,這些專案展示了常見的 MVC 整合模式。

下一步計劃是什麼?

雖然 iTextSharp 多年來為 .NET 社群提供了良好的服務,但現代網頁開發需要更強大的 PDF 生成功能。 IronPDF 基於 Chrome 的渲染引擎提供像素級精準度,並全面支援當今 ASP.NET MVC 應用程式所需的 CSS3 功能。其高階 API、詳盡的文件以及企業友善的授權方案,使其成為 ASP.NET Core MVC 專案的實用首選。

若要在您的應用程式中進一步實現 PDF 生成功能:

常見問題解答

將 MVC 視圖轉換為 PDF 的目的為何?

將 MVC 視圖轉換為 PDF 可讓開發人員直接從 Web 應用程式產生可列印且易於分享的文件,同時保留原始視圖的版面與設計。

什麼是IronPDF?

IronPDF for .NET 是一個 .NET 函式庫,可協助在 .NET 應用程式中建立、編輯和轉換 PDF 文件,提供整合 PDF 功能的簡易方法。

IronPDF 如何簡化 MVC 視圖轉換為 PDF 的過程?

IronPDF 可讓開發人員直接將 HTML 和 MVC 視圖渲染為 PDF 格式,而無需大量的編碼要求,並保留原始的佈局和設計,從而簡化了流程。

iTextSharp在MVC PDF轉換方面的限制是什麼?

iTextSharp的XMLWorkerHelper不支援現代CSS3、flexbox、grid布局、JavaScript執行或Bootstrap樣式。其AGPL授權也要求如果您使用免費版本,必須開源您的應用程式。

IronPDF能在生成PDF之前渲染JavaScript嗎?

是的,IronPDF在渲染過程中支援JavaScript執行。您可以通過EnableJavaScript啟用它,並使用WaitFor.RenderDelay控制渲染延遲以確保動態內容完全加載。

使用 IronPDF 有哪些系統要求?

IronPDF支援.NET 6、.NET 7、.NET 8、.NET Core和.NET Framework 4.6.2+。它在Windows、Linux和macOS上運行。

轉換 MVC 視圖時,是否可以自訂 PDF 輸出?

是的,使用IronPDF,開發者可以通過調整設定來自訂PDF輸出,如頁面大小、方向、邊距、頁首、頁尾、安全設置和CSS媒體類型。

IronPDF 是否支持 PDF 轉换的 CSS 样式?

IronPDF通過其Chromium渲染引擎支援完整的CSS3樣式,確保轉換後的PDF保持原始HTML或MVC視圖的視覺外觀,包括字型、顏色、漸變和佈局。

IronPDF的性能如何與iTextSharp相比?

IronPDF使用Chromium引擎進行渲染,這帶來了啟動開銷,但提供了更優越的渲染準確性。對於高容量場景,IronPDF支持異步渲染並可以通過線程安全的渲染器實例進行優化。

在哪裡可以找到 IronPDF 的說明文件?

IronPDF的完整文件可在https://ironpdf.com/how-to/獲得,包括指導、教程和API參考,以協助開發人員實施。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我