探索PDFsharp的最佳PDF到圖像替代方案
大多數HTML轉PDF任務中斷的原因相同:頁面只有在使用者登錄後纔看起來正確。 IronPDF驅動了一個Chrome引擎,因此它看到的是未經身份驗證的瀏覽器會看到的登入畫面或空殼,直到您給它正確的cookie。 API中的三個部分解決了這個問題,包括CustomCookies字典。 這裡是每個部件適配的地方,並附上相應的程式碼片段。
在登錄後渲染發票和報表
客戶的發票位於一條受會話保護的路由上,而您的應用已經持有他們的會話。 將該會話ID直接透過CustomCookies傳遞:
using IronPdf;
using System.Collections.Generic;
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global;
renderer.RenderingOptions.CustomCookies = new Dictionary<string, string>
{
{ "ASP.NET_SessionId", sessionId }
};
renderer.RenderUrlAsPdf("https://app.example.com/account/invoices/1042")
.SaveAs("invoice-1042.pdf");Imports IronPdf
Imports System.Collections.Generic
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global
renderer.RenderingOptions.CustomCookies = New Dictionary(Of String, String) From {
{"ASP.NET_SessionId", sessionId}
}
renderer.RenderUrlAsPdf("https://app.example.com/account/invoices/1042").SaveAs("invoice-1042.pdf")對於基本或Windows驗證,改用ApplyCookies。 它在URL上使用提供的憑據登入,並將所得到的cookie捕獲到全域上下文中,然後後續的渲染重用這些cookie:
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global;
var credentials = new ChromeHttpLoginCredentials
{
NetworkUsername = "svc_reports",
NetworkPassword = "your_password"
};
string uri = "https://app.example.com/account/invoices/1042";
renderer.ApplyCookies(uri, credentials);
renderer.RenderUrlAsPdf(uri).SaveAs("invoice-1042.pdf");Imports IronPdf
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global
Dim credentials As New ChromeHttpLoginCredentials With {
.NetworkUsername = "svc_reports",
.NetworkPassword = "your_password"
}
Dim uri As String = "https://app.example.com/account/invoices/1042"
renderer.ApplyCookies(uri, credentials)
renderer.RenderUrlAsPdf(uri).SaveAs("invoice-1042.pdf")基於令牌和第三方身份驗證
現代應用很少依賴簡單的HTTP憑證。 它們使用JWT、OAuth或SAML進行身份驗證,通常將結果儲存在cookie中。 由於CustomCookies接受任何鍵值對,可以直接使用承載令牌或身份提供者的Cookie:
renderer.RenderingOptions.CustomCookies = new Dictionary<string, string>
{
{ "auth_token", jwt },
{ "refresh_token", refreshToken }
};Imports System.Collections.Generic
renderer.RenderingOptions.CustomCookies = New Dictionary(Of String, String) From {
{"auth_token", jwt},
{"refresh_token", refreshToken}
}個性化和本地化文件
cookie通常攜帶地區,貨幣,或功能標誌狀態。 轉發它們,然後PDF匹配該特定使用者看到的內容,以正確的語言和貨幣展示:
renderer.RenderingOptions.CustomCookies = new Dictionary<string, string>
{
{ "locale", "en-AU" },
{ "currency", "AUD" },
{ "feature_newLayout", "on" }
};renderer.RenderingOptions.CustomCookies = New Dictionary(Of String, String) From {
{"locale", "en-AU"},
{"currency", "AUD"},
{"feature_newLayout", "on"}
}共享一個會話的批次生成
要為同一個已驗證使用者依序生成多個PDF,將Global。 瀏覽器狀態,包括cookie,在多次渲染中保持一致,因此您可以只進行一次身份驗證,而不是每個文件進行一次:
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Global;
renderer.RenderingOptions.CustomCookies = new Dictionary<string, string>
{
{ "ASP.NET_SessionId", sessionId }
};
foreach (var id in invoiceIds)
{
renderer.RenderUrlAsPdf($"https://app.example.com/invoices/{id}")
.SaveAs($"invoice-{id}.pdf");
}Imports IronPdf.Rendering
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.RequestContext = RequestContexts.Global
renderer.RenderingOptions.CustomCookies = New Dictionary(Of String, String) From {
{"ASP.NET_SessionId", sessionId}
}
For Each id In invoiceIds
renderer.RenderUrlAsPdf($"https://app.example.com/invoices/{id}").SaveAs($"invoice-{id}.pdf")
Next同時渲染無Cookie洩漏
相反的情境同樣重要。 同時為多名不同使用者進行服務呈現需要Isolated上下文,這可以為每個呈現提供乾淨的狀態,防止一位使用者的會話洩漏到另一個文件中。 給每個執行緒分配自己的渲染器:
using System.Threading.Tasks;
Parallel.ForEach(jobs, job =>
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.RequestContext = IronPdf.Rendering.RequestContexts.Isolated;
renderer.RenderingOptions.CustomCookies = new Dictionary<string, string>
{
{ "ASP.NET_SessionId", job.SessionId }
};
renderer.RenderUrlAsPdf(job.Url).SaveAs(job.OutputPath);
});Imports System.Threading.Tasks
Imports IronPdf.Rendering
Parallel.ForEach(jobs, Sub(job)
Dim renderer = New ChromePdfRenderer()
renderer.RenderingOptions.RequestContext = RequestContexts.Isolated
renderer.RenderingOptions.CustomCookies = New Dictionary(Of String, String) From {
{"ASP.NET_SessionId", job.SessionId}
}
renderer.RenderUrlAsPdf(job.Url).SaveAs(job.OutputPath)
End Sub)將方法與工作匹配
對於基本HTTP身份驗證、Windows身份驗證和簡單會話,使用標準Cookies (ApplyCookies)。 與令牌、多參數會話、第三方提供者和儲存的偏好設定一起使用CustomCookies。 選擇Isolated以保持其分離; ApplyCookies即切換到全球模式。
大多數故障集中於過期的令牌、不匹配的Cookie域、HTTPS URL上的安全Cookie處理,以及跨源請求上的SameSite政策。 在渲染之前確認Cookie的有效性,並在會話必須持續時依賴於Global。
當Cookie是整個工作
在PDF生成中,Cookie並不是一個邊緣情況。 如果正確,它們將會輸出可用的文件; 如果錯誤,它們將輸出登錄表單的截圖。

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。