我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
当前回答
尝试这个PDF Duo . net转换组件将HTML从ASP转换为PDF。NET应用程序,而不使用额外的dll。
您可以传递HTML字符串或文件或流来生成PDF。 使用下面的代码(示例c#):
string file_html = @"K:\hdoc.html";
string file_pdf = @"K:\new.pdf";
try
{
DuoDimension.HtmlToPdf conv = new DuoDimension.HtmlToPdf();
conv.OpenHTML(file_html);
conv.SavePDF(file_pdf);
textBox4.Text = "C# Example: Converting succeeded";
}
Info + c# /VB的例子,你可以在:http://www.duodimension.com/html_pdf_asp.net/component_html_pdf.aspx找到
其他回答
PDFmyURL最近也发布了一个。net组件,用于网页/ HTML到PDF的转换。它有一个非常友好的用户界面,例如:
PDFmyURL pdf = new PDFmyURL("yourlicensekey");
pdf.ConvertURL("http://www.example.com", Application.StartupPath + @"\example.pdf");
文档:PDFmyURL .NET组件文档
免责声明:我为拥有PDFmyURL的公司工作
如果你需要完美的html pdf渲染,你需要使用商业库。
ExpertPdf Html To Pdf Converter非常容易使用,它支持最新的html5/css3。您可以将整个url转换为pdf:
using ExpertPdf.HtmlToPdf;
byte[] pdfBytes = new PdfConverter().GetPdfBytesFromUrl(url);
或者HTML字符串:
using ExpertPdf.HtmlToPdf;
byte[] pdfBytes = new PdfConverter().GetPdfBytesFromHtmlString(html, baseUrl);
您还可以选择直接将生成的pdf文档保存到磁盘上的文件流。
另一个建议是尝试https://grabz.it的解决方案。
他们提供了一个很好的。net API来捕捉屏幕截图,并以一种简单灵活的方式进行操作。
要在你的应用中使用它,你首先需要获得key + secret并下载。net SDK(它是免费的)。
下面是一个简短的例子。
要使用这个API,你首先需要创建一个GrabzItClient类的实例,将你的应用密钥和应用秘密从你的GrabzIt账户传递给构造函数,如下面的例子所示:
//Create the GrabzItClient class
//Replace "APPLICATION KEY", "APPLICATION SECRET" with the values from your account!
private GrabzItClient grabzIt = GrabzItClient.Create("Sign in to view your Application Key", "Sign in to view your Application Secret");
现在,要将HTML转换为PDF,你需要做的是:
grabzIt.HTMLToPDF("<html><body><h1>Hello World!</h1></body></html>");
你也可以转换为图像:
grabzIt.HTMLToImage("<html><body><h1>Hello World!</h1></body></html>");
接下来需要保存图像。你可以使用两个可用的保存方法之一,如果公共可访问的回调句柄可用,则保存,如果没有SaveTo。详细信息请查看文档。
很可能大多数项目将包装C/ c++引擎,而不是从头开始实现c#解决方案。试试哥德堡计划。
为了测试它
docker run --rm -p 3000:3000 thecodingmachine/gotenberg:6
旋度样本
curl --request POST \
--url http://localhost:3000/convert/url \
--header 'Content-Type: multipart/form-data' \
--form remoteURL=https://brave.com \
--form marginTop=0 \
--form marginBottom=0 \
--form marginLeft=0 \
--form marginRight=0 \
-o result.pdf
c# sample.cs
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.IO;
using static System.Console;
namespace Gotenberg
{
class Program
{
public static async Task Main(string[] args)
{
try
{
var client = new HttpClient();
var formContent = new MultipartFormDataContent
{
{new StringContent("https://brave.com/"), "remoteURL"},
{new StringContent("0"), "marginTop" }
};
var result = await client.PostAsync(new Uri("http://localhost:3000/convert/url"), formContent);
await File.WriteAllBytesAsync("brave.com.pdf", await result.Content.ReadAsByteArrayAsync());
}
catch (Exception ex)
{
WriteLine(ex);
}
}
}
}
要编译
csc sample.cs -langversion:latest -reference:System.Net.Http.dll && mono ./sample.exe
你也可以检查Spire,它允许你用这段简单的代码创建HTML到PDF
string htmlCode = "<p>This is a p tag</p>";
//use single thread to generate the pdf from above html code
Thread thread = new Thread(() =>
{ pdf.LoadFromHTML(htmlCode, false, setting, htmlLayoutFormat); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
// Save the file to PDF and preview it.
pdf.SaveToFile("output.pdf");
System.Diagnostics.Process.Start("output.pdf");