我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
当前回答
作为HiQPdf软件的代表,我认为最好的解决方案是HiQPdf HTML到PDF转换器。net。它包含了市场上最先进的HTML5, CSS3, SVG和JavaScript渲染引擎。还有一个免费版本的HTML到PDF库,您可以使用它来免费生成最多3个PDF页面。从HTML页面生成以字节[]为单位的PDF的最小c#代码是:
HtmlToPdf htmlToPdfConverter = new HtmlToPdf();
// set PDF page size, orientation and margins
htmlToPdfConverter.Document.PageSize = PdfPageSize.A4;
htmlToPdfConverter.Document.PageOrientation = PdfPageOrientation.Portrait;
htmlToPdfConverter.Document.Margins = new PdfMargins(0);
// convert HTML to PDF
byte[] pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url);
你可以找到更详细的例子。NET和MVC在HiQPdf HTML到PDF转换器的例子库。
其他回答
你可以使用谷歌Chrome打印到pdf功能从它的无头模式。我发现这是最简单但最健壮的方法。
var url = "https://stackoverflow.com/questions/564650/convert-html-to-pdf-in-net";
var chromePath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
var output = Path.Combine(Environment.CurrentDirectory, "printout.pdf");
using (var p = new Process())
{
p.StartInfo.FileName = chromePath;
p.StartInfo.Arguments = $"--headless --disable-gpu --print-to-pdf={output} {url}";
p.Start();
p.WaitForExit();
}
最后更新:2020年10月
这是我整理的。net中HTML到PDF转换的选项列表(有些是免费的,有些是付费的)
GemBox.Document https://www.nuget.org/packages/GemBox.Document/ Free (up to 20 paragraphs) $680 - https://www.gemboxsoftware.com/document/pricelist https://www.gemboxsoftware.com/document/examples/c-sharp-convert-html-to-pdf/307 PDF Metamorphosis .Net https://www.nuget.org/packages/sautinsoft.pdfmetamorphosis/ $539 - $1078 - https://www.sautinsoft.com/products/pdf-metamorphosis/order.php https://www.sautinsoft.com/products/pdf-metamorphosis/convert-html-to-pdf-dotnet-csharp.php HtmlRenderer.PdfSharp https://www.nuget.org/packages/HtmlRenderer.PdfSharp/1.5.1-beta1 BSD-UNSPECIFIED License PuppeteerSharp https://www.puppeteersharp.com/examples/index.html MIT License https://github.com/kblok/puppeteer-sharp EO.Pdf https://www.nuget.org/packages/EO.Pdf/ $799 - https://www.essentialobjects.com/Purchase.aspx?f=3 WnvHtmlToPdf_x64 https://www.nuget.org/packages/WnvHtmlToPdf_x64/ $750 - $1600 - http://www.winnovative-software.com/Buy.aspx demo - http://www.winnovative-software.com/demo/default.aspx IronPdf https://www.nuget.org/packages/IronPdf/ $399 - $1599 - https://ironpdf.com/licensing/ https://ironpdf.com/examples/using-html-to-create-a-pdf/ Spire.PDF https://www.nuget.org/packages/Spire.PDF/ Free (up to 10 pages) $599 - $1799 - https://www.e-iceblue.com/Buy/Spire.PDF.html https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-HTML-to-PDF-Customize-HTML-to-PDF-Conversion-by-Yourself.html Aspose.Html https://www.nuget.org/packages/Aspose.Html/ $599 - $1797 - https://purchase.aspose.com/pricing/html/net https://docs.aspose.com/html/net/html-to-pdf-conversion/ EvoPDF https://www.nuget.org/packages/EvoPDF/ $450 - $1200 - http://www.evopdf.com/buy.aspx ExpertPdfHtmlToPdf https://www.nuget.org/packages/ExpertPdfHtmlToPdf/ $550 - $1200 - https://www.html-to-pdf.net/Pricing.aspx Zetpdf https://zetpdf.com $299 - $599 - https://zetpdf.com/pricing/ Is not a well know or supported library - ZetPDF - Does anyone know the background of this Product? PDFtron https://www.pdftron.com/documentation/samples/cs/HTML2PDFTes $4000/year - https://www.pdftron.com/licensing/ WkHtmlToXSharp https://github.com/pruiz/WkHtmlToXSharp Free Concurrent conversion is implemented as processing queue. SelectPDF https://www.nuget.org/packages/Select.HtmlToPdf/ Free (up to 5 pages) $499 - $799 - https://selectpdf.com/pricing/ https://selectpdf.com/pdf-library-for-net/
如果上面的选项都帮不了你,你可以搜索NuGet包: https://www.nuget.org/packages?q=html+pdf
尝试这个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找到
到目前为止,似乎最好的免费。net解决方案是TuesPechkin库,它是wkhtmltopdf本机库的包装。
我现在已经使用单线程版本将几千个HTML字符串转换为PDF文件,它似乎工作得很好。它应该也可以在多线程环境中工作(例如IIS),但我还没有对此进行测试。
另外,因为我想使用最新版本的wkhtmltopdf(在编写时为0.12.5),我从官方网站下载了DLL,复制到我的项目根目录,设置copy to output为true,并像这样初始化库:
var dllDir = AppDomain.CurrentDomain.BaseDirectory;
Converter = new StandardConverter(new PdfToolset(new StaticDeployment(dllDir)));
上面的代码看起来完全是“wkhtmltox.dll”,所以不要重命名文件。我使用的是64位版本的DLL。
确保你阅读了多线程环境的说明,因为你只需要在每个应用生命周期中初始化它一次,所以你需要把它放在一个单例或其他东西中。
下面是一个使用iTextSharp将html + css转换为PDF的示例(iTextSharp + iTextSharp .xmlworker)
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
byte[] pdf; // result will be here
var cssText = File.ReadAllText(MapPath("~/css/test.css"));
var html = File.ReadAllText(MapPath("~/css/test.html"));
using (var memoryStream = new MemoryStream())
{
var document = new Document(PageSize.A4, 50, 50, 60, 60);
var writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
{
using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
{
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, htmlMemoryStream, cssMemoryStream);
}
}
document.Close();
pdf = memoryStream.ToArray();
}