我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
我想通过将HTML内容传递给函数来生成PDF。我已经为此使用了iTextSharp,但它在遇到表和布局时表现不佳。
有没有更好的办法?
当前回答
你也可以检查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");
其他回答
你也可以检查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");
下面是一个使用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();
}
最后更新: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
如果你已经使用itextsharp dll,不需要添加第三方dll的(插件),我认为你正在使用htmlworker而不是它使用xmlworker,你可以很容易地将你的html转换为pdf。 一些css不能工作,他们是受支持的css 完整的解释与示例参考点击这里
MemoryStream memStream = new MemoryStream();
TextReader xmlString = new StringReader(outXml);
using (Document document = new Document())
{
PdfWriter writer = PdfWriter.GetInstance(document, memStream);
//document.SetPageSize(iTextSharp.text.PageSize.A4);
document.Open();
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(outXml);
MemoryStream ms = new MemoryStream(byteArray);
XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, ms, System.Text.Encoding.UTF8);
document.Close();
}
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(memStream.ToArray());
Response.End();
Response.Flush();
如果你想让用户在浏览器中下载渲染页面的pdf,那么最简单的解决方案是
window.print();
在客户端,它将提示用户保存当前页面的PDF。您还可以通过链接样式自定义pdf的外观
<link rel="stylesheet" type="text/css" href="print.css" media="print">
css在打印时应用于HTML。
限制
不能将文件存储在服务器端。 用户提示打印页面时,必须手动保存页面。 页必须在选项卡中呈现。