Skip to main content
 首页 » 编程设计

c#之使用 asp.net 返回 pdf 的 Web 服务

2024年02月24日34三少

我有一个 html 到 pdf 的转换工具,它驻留在我们的服务器上,我可以使用带有查询字符串的 url 访问它。 像 myurl.com/createpdf.aspx?page= http://www.somesite.com 这样的东西. 然后,该工具会转换网页并将其显示为 pdf。

我想以 Web 服务的形式提供此功能,以便我们的客户可以通过编程方式访问此功能。 理想情况是,我们的客户发送他们想要转换的网站,然后网络服务返回 pdf。

这有意义吗? 实现这一点的最佳方式是什么?

干杯,

特里

请您参考如下方法:

使用文件 IO 读取 PDF 并让您的网络方法将其作为 byte[] 数组返回。例如像这样:

byte[] filebytes = null; 
using (FileStream fs = File.OpenRead(fileName)) 
{ 
  filebytes = new byte[fs.Length]; 
  fs.Read(filebytes, (int)0, (int)fs.Length); 
} 
return filebytes;

它将被 Base64 编码,然后您将能够在客户端上保存和查看文件。