如何用 C# 从 PDFmyURL 迁移到 IronPDF
PDFmyURL 是一项基于云的 API 服务,旨在将 URL 和 HTML 内容转换为 PDF 文档。 该服务在外部服务器上处理所有转换,提供了一个直接的集成路径,只需最少的本地基础设施。 然而,对于处理敏感数据、需要离线功能或需要避免持续订阅成本的生产应用程序来说,这种依赖云的架构会带来很大的问题。
本指南提供了从PDFmyURL到IronPDF的完整迁移路径,为评估这一过渡的 .NET 专业开发人员提供了分步说明、代码比较和实用示例。
为什么要从PDFmyURL迁移
PDFmyURL 的云处理模式带来了开发团队必须考虑的几个挑战:
隐私和数据安全:您转换的每个文档都会传输到PDFmyURL的服务器并经过其服务器——敏感合同、财务报告和个人数据都在外部进行处理。
持续订阅费用:每月起价 39 美元,年费用超过 468 美元,且不拥有产品所有权。这种订阅模式意味着无论使用情况如何,都需要持续付费。
互联网依赖性:每次转换都需要网络连接。 应用程序不能离线或在网络中断时处理 PDF。
速率限制和节流:在高峰使用期间,API 调用可能会受到节流,这可能会影响应用程序的性能。
服务可用性:您的应用程序依赖于第三方服务在线且功能正常。
供应商锁定: API 变更可能会在未事先通知的情况下破坏您的集成,需要被动地更新代码。
IronPDF与 PDFmyURL:功能比较
了解架构差异有助于技术决策者评估迁移投资:
| 方面 | PDFmyURL | IronPDF |
|---|---|---|
| 处理地点 | 外部服务器 | 本地(您的服务器) |
| 类型 | 应用程序接口封装 | .NET 库 |
| 身份验证 | 每次请求的 API 密钥 | 一次性许可证密钥 |
| 网络要求 | 每次转换 | 仅初始设置 |
| 定价模式 | 按月订购(39 美元以上) | 提供永久许可证 |
| 费用限制 | 是(取决于计划) | 无 |
| 数据隐私 | 外部发送的数据 | 数据保持本地化 |
| HTML/CSS/JS 支持 | 符合 W3C 标准 | 完整的 Chromium 引擎 |
| 同步模式 | 要求(仅限异步) | 同步和异步选项 |
| 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 (渲染选项) |
|---|---|
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 密钥与许可证密钥
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 期:异步模式
PDFmyURL:需要异步/等待模式。
解决方案: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异常的捕获块:
// 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:使用带有字符串值的设置器方法。
解决方案:使用强类型的 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.将设置器方法转换为 RenderingOptions 属性
- 更新页眉/页脚中的占位符语法(
{page_number}→{page},{total_pages}→{total-pages}) 7.更新IronPDF异常类型的错误处理代码 8.在启动时添加IronPDF许可证初始化功能
迁移后测试
迁移后,验证这些方面:
- 测试 PDF 输出质量是否符合预期
- 验证异步模式是否正常工作
- 将渲染保真度与以前的输出进行比较
- 测试所有模板变体是否能正确呈现
- 验证页面设置(大小、方向、页边距)
- 如果部署到 Linux 服务器,请安装 Linux 依赖项
迁移到IronPDF的主要优势
从PDFmyURL迁移到IronPDF有几个关键优势:
完全隐私:文档绝不会离开您的服务器。 所有处理都在本地进行,消除了敏感内容的数据安全顾虑。
一次性费用:永久授权选项免除定期订阅费用。 无论使用量多少,都不再需要每月付款。
离线功能:完成初始设置后,无需网络连接即可工作。网络中断不会影响 PDF 生成。
无速率限制:处理无限量文档,无需担心限速问题。
更低的延迟:没有网络开销意味着更快的转换速度,尤其适用于高容量应用。
完全控制:您控制处理环境,而不是第三方服务。
现代 Chromium 引擎:完全支持 CSS3 和 JavaScript,采用与 Chrome 浏览器相同的渲染引擎。
积极开发:随着 .NET 10 和 C# 14 的普及,IronPDF 将持续更新,确保与当前和未来的 .NET 版本兼容。

