如何在 C# 中從 PDFmyURL 轉移到 IronPDF
PDFmyURL 是一項以雲端為基礎的 API 服務,專為將 URL 和 HTML 內容轉換為 PDF 文件而設計。 該服務在外部伺服器上處理所有轉換,提供直接的整合路徑,只需最少的本機基礎架構。 然而,對於處理敏感資料、需要離線功能或需要避免持續訂閱成本的生產應用程式來說,這種依賴雲端的架構造成了很大的疑慮。
本指南提供了從PDFmyURL到IronPDF的完整轉換路徑,為評估此轉換的 .NET 專業開發人員提供了分步說明、程式碼比較以及實用範例。
為何要從PDFmyURL遷移?
PDFmyURL 的雲端處理模式引入了幾項開發團隊必須考慮的挑戰:
隱私和資料安全:您轉換的每個文件都會傳輸到PDFmyURL的伺服器並經過其伺服器——敏感合約、財務報告和個人資料都在外部進行處理。
持續訂閱費用:每月起價 39 美元,年費用超過 468 美元,且不擁有產品所有權。這種訂閱模式意味著無論使用情況如何,都需要持續付費。
網路依賴性:每次轉換都需要網路連線。 應用程式無法在離線或網路中斷時處理 PDF。
速率限制和節流:在高峰使用期間,API 呼叫可能會受到節流,這可能會影響應用程式的效能。
服務可用性:您的應用程式依賴第三方服務在線上且功能正常。
供應商鎖定: API 變更可能會在未事先通知的情況下破壞您的集成,需要被動地更新程式碼。
IronPDFvs PDFmyURL:功能比較
了解架構上的差異有助於技術決策者評估遷移投資:
| 範疇 | PDFmyURL | IronPDF |
|---|---|---|
| 處理地點 | 外部伺服器 | 本地(您的伺服器) |
| 類型 | API 包裝 | .NET 圖書館 |
| 驗證 | 每個請求的 API 金鑰 | 一次性授權金鑰 |
| 網路需求 | 每次轉換 | 只有初始設定 |
| 定價模式 | 按月訂閱 ($39+) | 提供永久授權 |
| 費率限制 | 是 (依計劃而定) | 無 |
| 資料隱私權 | 從外部傳送的資料 | 資料保持在本地 |
| HTML/CSS/JS 支援。 | 符合 W3C 標準 | 完整的 Chromium 引擎 |
| 同步模式 | 必需 (僅限於 async) | 同步與同步選項 |
| PDF 操作 | 限額 | 全套(合併、分割、編輯) |
| 使用個案 | 少量應用程式 | 高產量和企業級 |
快速入門:PDFmyURL 到IronPDF的遷移。
只要完成這些基本步驟,就可以立即開始遷移。
步驟 1:取代 NuGet 套件
移除PDFmyURL套件:
# RemovePDFmyURLpackages
dotnet remove package PdfMyUrl
dotnet remove package Pdfcrowd
# RemovePDFmyURLpackages
dotnet remove package PdfMyUrl
dotnet remove package Pdfcrowd
安裝 IronPDF:
# Install IronPDF
dotnet add package IronPdf
# Install IronPDF
dotnet add package IronPdf
步驟 2:更新命名空間
用IronPDF取代PDFmyURL命名空間:
// Before: PDFmyURL
using PdfMyUrl;
using Pdfcrowd;
// After: IronPDF
using IronPdf;
using IronPdf.Rendering;
// Before: PDFmyURL
using PdfMyUrl;
using Pdfcrowd;
// After: IronPDF
using IronPdf;
using IronPdf.Rendering;
' Before: PDFmyURL
Imports PdfMyUrl
Imports Pdfcrowd
' After: IronPDF
Imports IronPdf
Imports IronPdf.Rendering
步驟 3:初始化授權
在應用程式啟動時加入授權初始化:
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"
程式碼遷移範例
將 URL 轉換為 PDF
URL-to-PDF 操作展示了PDFmyURL與IronPDF之間基本的 API 差異。
PDFmyURL 方法:
// InstallPDFmyURLSDK
using System;
using Pdfcrowd;
class Example
{
static void Main()
{
try
{
var client = new HtmlToPdfClient("username", "apikey");
client.convertUrlToFile("https://example.com", "output.pdf");
}
catch(Error why)
{
Console.WriteLine("Error: " + why);
}
}
}
// InstallPDFmyURLSDK
using System;
using Pdfcrowd;
class Example
{
static void Main()
{
try
{
var client = new HtmlToPdfClient("username", "apikey");
client.convertUrlToFile("https://example.com", "output.pdf");
}
catch(Error why)
{
Console.WriteLine("Error: " + why);
}
}
}
Imports System
Imports Pdfcrowd
Class Example
Shared Sub Main()
Try
Dim client = New HtmlToPdfClient("username", "apikey")
client.convertUrlToFile("https://example.com", "output.pdf")
Catch why As Error
Console.WriteLine("Error: " & why)
End Try
End Sub
End Class
IronPDF 方法:
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Example
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://example.com");
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Example
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://example.com");
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Imports System
Class Example
Shared Sub Main()
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderUrlAsPdf("https://example.com")
pdf.SaveAs("output.pdf")
End Sub
End Class
PDFmyURL 要求為每個轉換請求建立一個 HtmlToPdfClient,並包含使用者名稱和 API 金鑰憑證,然後使用 URL 和輸出路徑呼叫 convertUrlToFile()。 對於PDFmyURL的自訂 Error 類型,整個操作必須用 try-catch 語句包裹起來。
IronPDF 將此簡化為三行:建立一個 ChromePdfRenderer,呼叫 RenderUrlAsPdf(),並使用內建的 SaveAs() 方法。 不需要每次要求的憑證 - 授權會在應用程式啟動時設定一次。
如需進階的 URL-to-PDF 方案,請參閱 URL to PDF 文件。
將 HTML 字串轉換為 PDF
HTML 字串轉換可清楚顯示模式差異。
PDFmyURL 方法:
// InstallPDFmyURLSDK
using System;
using Pdfcrowd;
class Example
{
static void Main()
{
try
{
var client = new HtmlToPdfClient("username", "apikey");
string html = "<html><body><h1>Hello World</h1></body></html>";
client.convertStringToFile(html, "output.pdf");
}
catch(Error why)
{
Console.WriteLine("Error: " + why);
}
}
}
// InstallPDFmyURLSDK
using System;
using Pdfcrowd;
class Example
{
static void Main()
{
try
{
var client = new HtmlToPdfClient("username", "apikey");
string html = "<html><body><h1>Hello World</h1></body></html>";
client.convertStringToFile(html, "output.pdf");
}
catch(Error why)
{
Console.WriteLine("Error: " + why);
}
}
}
Imports System
Imports Pdfcrowd
Class Example
Shared Sub Main()
Try
Dim client = New HtmlToPdfClient("username", "apikey")
Dim html As String = "<html><body><h1>Hello World</h1></body></html>"
client.convertStringToFile(html, "output.pdf")
Catch why As Error
Console.WriteLine("Error: " & why.ToString())
End Try
End Sub
End Class
IronPDF 方法:
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Example
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using System;
class Example
{
static void Main()
{
var renderer = new ChromePdfRenderer();
string html = "<html><body><h1>Hello World</h1></body></html>";
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Imports System
Class Example
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim html As String = "<html><body><h1>Hello World</h1></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(html)
pdf.SaveAs("output.pdf")
End Sub
End Class
PDFmyURL 使用 convertStringToFile() 將 HTML 內容傳送至外部伺服器處理。IronPDF的 RenderHtmlAsPdf() 使用 Chromium 渲染引擎在本地處理所有內容。
探索 HTML 至 PDF 轉換指南 以取得其他選項。
具有頁面設定的 HTML 檔案轉換
配置紙張大小、方向和頁邊需要在每個圖書館採用不同的方法。
PDFmyURL 方法:
// InstallPDFmyURLSDK
using System;
using Pdfcrowd;
class Example
{
static void Main()
{
try
{
var client = new HtmlToPdfClient("username", "apikey");
client.setPageSize("A4");
client.setOrientation("landscape");
client.setMarginTop("10mm");
client.convertFileToFile("input.html", "output.pdf");
}
catch(Error why)
{
Console.WriteLine("Error: " + why);
}
}
}
// InstallPDFmyURLSDK
using System;
using Pdfcrowd;
class Example
{
static void Main()
{
try
{
var client = new HtmlToPdfClient("username", "apikey");
client.setPageSize("A4");
client.setOrientation("landscape");
client.setMarginTop("10mm");
client.convertFileToFile("input.html", "output.pdf");
}
catch(Error why)
{
Console.WriteLine("Error: " + why);
}
}
}
Imports System
Imports Pdfcrowd
Class Example
Shared Sub Main()
Try
Dim client = New HtmlToPdfClient("username", "apikey")
client.setPageSize("A4")
client.setOrientation("landscape")
client.setMarginTop("10mm")
client.convertFileToFile("input.html", "output.pdf")
Catch why As Error
Console.WriteLine("Error: " & why.ToString())
End Try
End Sub
End Class
IronPDF 方法:
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Rendering;
using System;
class Example
{
static void Main()
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape;
renderer.RenderingOptions.MarginTop = 10;
var pdf = renderer.RenderHtmlFileAsPdf("input.html");
pdf.SaveAs("output.pdf");
}
}
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Rendering;
using System;
class Example
{
static void Main()
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape;
renderer.RenderingOptions.MarginTop = 10;
var pdf = renderer.RenderHtmlFileAsPdf("input.html");
pdf.SaveAs("output.pdf");
}
}
Imports IronPdf
Imports IronPdf.Rendering
Imports System
Class Example
Shared Sub Main()
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape
renderer.RenderingOptions.MarginTop = 10
Dim pdf = renderer.RenderHtmlFileAsPdf("input.html")
pdf.SaveAs("output.pdf")
End Sub
End Class
PDFmyURL 使用帶有字串參數的 setter 方法,例如 setPageSize("A4") 和 setMarginTop("10mm")。IronPDF透過 RenderingOptions 提供強型別屬性,枚舉類型如 PdfPaperSize.A4,邊距的整數值以毫米為單位。
PDFmyURLAPI 至IronPDF映射參考。
此對應可透過顯示直接的 API 對應關係來加速遷移:
核心類別
| PDFmyURL | IronPDF |
|---|---|
HtmlToPdfClient |
ChromePdfRenderer |
PdfMyUrlClient |
ChromePdfRenderer |
| API 回應物件 | PdfDocument |
方法
| PDFmyURL | IronPDF |
|---|---|
client.convertUrlToFile(url, file) |
renderer.RenderUrlAsPdf(url).SaveAs(file) |
client.convertStringToFile(html, file) |
renderer.RenderHtmlAsPdf(html).SaveAs(file) |
client.convertFileToFile(input, output) |
renderer.RenderHtmlFileAsPdf(input).SaveAs(output) |
response.GetBytes() |
pdf.BinaryData |
response.GetStream() |
pdf.Stream |
設定選項
| PDFmyURL (setXxx 方法) | IronPDF (RenderingOptions) |
|---|---|
setPageSize("A4") |
.PaperSize = PdfPaperSize.A4 |
setPageSize("Letter") |
.PaperSize = PdfPaperSize.Letter |
setOrientation("landscape") |
.PaperOrientation = PdfPaperOrientation.Landscape |
setOrientation("portrait") |
.PaperOrientation = PdfPaperOrientation.Portrait |
setMarginTop("10mm") |
.MarginTop = 10 |
setMarginBottom("10mm") |
.MarginBottom = 10 |
setMarginLeft("10mm") |
.MarginLeft = 10 |
setMarginRight("10mm") |
.MarginRight = 10 |
setHeaderHtml(html) |
.HtmlHeader = new HtmlHeaderFooter { HtmlFragment = html } |
setFooterHtml(html) |
.HtmlFooter = new HtmlHeaderFooter { HtmlFragment = html } |
setJavascriptDelay(500) |
.RenderDelay = 500 |
setDisableJavascript(true) |
.EnableJavaScript = false |
setUsePrintMedia(true) |
.CssMediaType = PdfCssMediaType.Print |
驗證比較
| PDFmyURL | IronPDF |
|---|---|
new HtmlToPdfClient("username", "apikey") |
IronPdf.License.LicenseKey = "LICENSE-KEY" |
| 每個請求的 API 金鑰 | 啟動時一次性 |
| 每次通話都需要 | 全球設定一次 |
常見的遷移問題與解決方案
問題 1:API 金鑰 vs 授權金鑰
PDFmyURL: 每次轉換請求都需要憑證。
解決方案:在應用程式啟動時設定一次IronPDF授權:
// PDFmyURL: API key per request
var client = new HtmlToPdfClient("username", "apikey");
// IronPDF: One-time license at startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Set once, typically in Program.cs or Startup.cs
// PDFmyURL: API key per request
var client = new HtmlToPdfClient("username", "apikey");
// IronPDF: One-time license at startup
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";
// Set once, typically in Program.cs or Startup.cs
Imports IronPdf
' PDFmyURL: API key per request
Dim client As New HtmlToPdfClient("username", "apikey")
' IronPDF: One-time license at startup
License.LicenseKey = "YOUR-LICENSE-KEY"
' Set once, typically in Program.vb or Startup.vb
問題 2:標題/頁腳中的占位符語法
PDFmyURL:使用 {page_number} 和 {total_pages} 佔位符。
解決方案:更新為IronPDF的占位格式:
// PDFmyURL: "Page {page_number} of {total_pages}"
// IronPDF: "Page {page} of {total-pages}"
// PDFmyURL: "Page {page_number} of {total_pages}"
// IronPDF: "Page {page} of {total-pages}"
' PDFmyURL: "Page {page_number} of {total_pages}"
' IronPDF: "Page {page} of {total-pages}"
第 3 期:Async 模式
PDFmyURL: 需要 async/await 模式。
解決方案:IronPDF預設是同步的; 如有需要,可為 async 包裝:
// PDFmyURL: Native async
var response = await client.ConvertUrlAsync(url);
// IronPDF: Sync by default, wrap for async
var pdf = await Task.Run(() => renderer.RenderUrlAsPdf(url));
// PDFmyURL: Native async
var response = await client.ConvertUrlAsync(url);
// IronPDF: Sync by default, wrap for async
var pdf = await Task.Run(() => renderer.RenderUrlAsPdf(url));
Imports System.Threading.Tasks
' PDFmyURL: Native async
Dim response = Await client.ConvertUrlAsync(url)
' IronPDF: Sync by default, wrap for async
Dim pdf = Await Task.Run(Function() renderer.RenderUrlAsPdf(url))
問題 4:錯誤處理
PDFmyURL:使用自訂 Pdfcrowd.Error 異常類型。
解決方案:更新IronPDF異常的 catch 區塊:
// PDFmyURL: Pdfcrowd.Error
catch (Pdfcrowd.Error e) { ... }
// IronPDF: Standard exceptions
catch (IronPdf.Exceptions.IronPdfRenderingException e) { ... }
// PDFmyURL: Pdfcrowd.Error
catch (Pdfcrowd.Error e) { ... }
// IronPDF: Standard exceptions
catch (IronPdf.Exceptions.IronPdfRenderingException e) { ... }
第 5 期:組態樣式
PDFmyURL: 使用具有字串值的 setter 方法。
解決方案:使用強式類型的 RenderingOptions 屬性:
// PDFmyURL: Setter methods
client.setPageSize("A4");
client.setOrientation("landscape");
// IronPDF: Properties with enums
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape;
// PDFmyURL: Setter methods
client.setPageSize("A4");
client.setOrientation("landscape");
// IronPDF: Properties with enums
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape;
' PDFmyURL: Setter methods
client.setPageSize("A4")
client.setOrientation("landscape")
' IronPDF: Properties with enums
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape
PDFmyURL遷移清單
遷移前的任務
審核您的程式碼庫,找出所有PDFmyURL的使用情況:
# FindPDFmyURLusage
grep -r "PdfMyUrl\|Pdfcrowd\|HtmlToPdfClient" --include="*.cs" .
# Find API key references
grep -r "apikey\|api-key\|api_key" --include="*.cs" --include="*.json" --include="*.config" .
# Find placeholder patterns to migrate
grep -r "{page_number}\|{total_pages}" --include="*.cs" .
# FindPDFmyURLusage
grep -r "PdfMyUrl\|Pdfcrowd\|HtmlToPdfClient" --include="*.cs" .
# Find API key references
grep -r "apikey\|api-key\|api_key" --include="*.cs" --include="*.json" --include="*.config" .
# Find placeholder patterns to migrate
grep -r "{page_number}\|{total_pages}" --include="*.cs" .
記錄目前使用的組態選項。 使用環境變數規劃授權金鑰儲存。
程式碼更新任務
1.移除 PDFmyURL/Pdfcrowd NuGet 套件 2.安裝IronPDFNuGet 套件 3.更新所有命名空間的匯入 4.使用IronPDF授權金鑰取代 API 金鑰驗證 5.將 setter 方法轉換為 RenderingOptions 屬性
- 更新頁首/頁尾中的佔位語法(
{page_number}→{page},{total_pages}→{total-pages}) 7.更新IronPDF異常類型的錯誤處理程式碼 8.在啟動時增加IronPDF授權初始化功能
後遷移測試
轉移後,驗證這些方面:
- 測試 PDF 輸出品質是否符合預期
- 驗證 async 模式是否正常運作
- 比較與先前輸出的渲染保真度
- 測試所有範本變化是否能正確呈現
- 驗證頁面設定(大小、方向、邊界)
- 如果部署到 Linux 伺服器,請安裝 Linux 相依性
遷移到IronPDF的主要優點。
從PDFmyURL轉移到IronPDF提供了幾個關鍵優勢:
完全隱私:文件絕對不會離開您的伺服器。 所有處理都在本機進行,消除敏感內容的資料安全疑慮。
一次性費用:永久授權選項免除定期訂閱費用。 無論使用量多少,都不再需要每月付款。
離線功能:完成初始設定後,無需網路連線即可運作。網路中斷不會影響 PDF 產生。
無速率限制:處理無限量文檔,無需擔心限速問題。
更低的延遲:沒有網路開銷意味著更快的轉換速度,尤其適用於高容量應用。
完全控制:您控制處理環境,而不是第三方服務。
現代 Chromium 引擎:完全支援 CSS3 和 JavaScript,採用與 Chrome 瀏覽器相同的渲染引擎。
積極開發:隨著 .NET 10 和 C# 14 的普及,IronPDF 將持續更新,確保與目前和未來的 .NET 版本相容。

